I\'m a beginner & am trying to loop through all the fields in an Angular form, and test that the input validation is working, using Protractor. So far I\'m failing mise
There are a few issues in your code:
it
block will never be executed because you are not calling the testRequired
functionsendkeys()
should be sendKeys()
, and you are not sending any text, it's emptyYou could try achieve what you described with this:
describe('Sample form', function() {
// helper function to check class of a specified element --> ng-valid / ng-invalid etc.
var hasClass = function (element, cls) {
return element.getAttribute('class').then(function (classes) {
return classes.split(' ').indexOf(cls) !== -1;
});
};
var fields = {
'userName': element(by.model('user.name')),
'userSurname': element(by.model('user.surname')),
'userId': element(by.model('user.id'))
};
for(var field in fields) {
(function(field) {
it('should fail validation when ' + field + ' is missing', function () {
browser.get('http://sometestlink.html');
fields[field].sendKeys('dummy_data');
expect(hasClass(fields[field], 'ng-valid')).toEqual(false);
});
})(field);
};
});