You want to write a custom matcher for Jasmine. You want to include them in the protractor.conf.js file so they are accessible over the entire project
https://jasmine.github.io/2.0/custom_matcher.html
Here is an example of comparing numbers with the message printing off if there is an error. The error if you run this will be Expected 7 to be 8 (Expected Error)
. Note, this is including it in the spec using the beforeEach
.
var customMatchers = {
toBeEqualWithMessage: function(util, customEqualityTesters) {
return {
compare: function(actual, expected, message) {
var result = {};
result.pass = util.equals(actual, expected, customEqualityTesters);
if (!result.pass) {
result.message = "Expected " + actual + " to be " + expected + " (" + message + ")";
}
return result;
}
};
}
};
describe('Custom Matcher', function(){
beforeEach(function() { jasmine.addMatchers(customMatchers);});
it('should use a matcher', function(){
expect(7).toBeEqualWithMessage(7,"No error because they match");
expect(7).not.toBeEqualWithMessage(8,"No error because of the 'not'");
expect(7).toBeEqualWithMessage(8,"Expected Error");
});
});
FYI: The doc has a result.message for a passing case, but I don't know where that is used, maybe a verbose print out.