问题
This is my expect:
expect(mandatoryFields[index].getAttribute('class')).toContain('error');
This is the error in console:
Expected['formControl ng-pristine ng-untouched ng-valid ng-empty ng-valid-maxlength error'] to contain 'error'.
Eventhough the class contains ERROR class name, protractor is still throwing error. What could be reason? Any help!!!
回答1:
Instead of toContain
try using toMatch
. toContain is used to check whether the required value is present in an array or not. whereas toMatch uses regex for validating the text present in any value.
回答2:
You could try adding a custom matcher in your beforeEach(), then calling expect(mandatoryFields[index]).toHaveClass('error');
jasmine.addMatchers([
toHaveClass: function () {
return {
compare: function (element, className) {
return {
pass: element.getAttribute('class').then(function (classes) {
return classes.split(' ').indexOf(className) !== -1||classes.split(' ').indexOf(className+"\n") !== -1;
}),
message: "Expected elemenet to have class: "+className
}
},
negativeCompare: function(element, className){
return {
pass: element.getAttribute('class').then(function (classes) {
return classes.split(' ').indexOf(className) !== -1||classes.split(' ').indexOf(className+"\n") === -1;
}),
message: "Expected element not to have class: " + className
}
}
}
}
]);
来源:https://stackoverflow.com/questions/39589630/protractor-tocontain-error