Protractor 'toContain' error

让人想犯罪 __ 提交于 2020-01-05 06:47:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!