Reputation: 867
Hello fellow developers,
I would like to create my own custom matcher based on a Protractor/WebDriverJS element. Is anybody able to improve my current code ?
Here is what I would like to write in the specs files
var button = element(by.tagName('button'))
expect(button).toBeEnabled();
So here is my custom Jasmine matcher :
'use strict';
function matcher(util, customEqualityTesters) {
return {
compare : function (actual, expected) {
var result = {};
expect(actual.isEnabled()).toBeTruthy()
result.pass = true;
if (result.pass) {
result.message = 'Expected element to be disabled';
}
else {
result.message = 'Expected element to be enabled';
}
return result;
}
};
}
module.exports = matcher;
Is there a better way to write it ? Because at the moment, if I have an error, I have the following message: Expected false to be true
. However I would like to get Expected element to be enabled.
Thank you for your help.
Upvotes: 0
Views: 498
Reputation: 6962
Not an expert, but i feel that Jasmine custom matchers are objects which have the matchers as keys in them. So you have to call it using the object key. However, you are hard coding the result.pass = true;
value, which causes your else
to never be executed in case of a failure and your statement 'Expected element to be enabled.'
will never be printed. Here's how you can improve it -
'use strict';
var customMatchers = {
toBeEnabled: function (util, customEqualityTesters) {
return {
compare : function (actual, expected) {
var result = {};
result.pass = actual.isEnabled();
if (result.pass) {
result.message = 'Expected element to be enabled';
}
else {
result.message = 'Expected element to be disabled';
}
return result;
}
};
}
}
Here's how you can use it -
var button = element(by.tagName('button'))
expect(button).toBeEnabled();
Hope it helps.
Upvotes: 2