Looping through fields in an Angular form and testing input validations using Protractor?

前端 未结 1 1457
故里飘歌
故里飘歌 2021-01-16 04:06

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

1条回答
  •  悲哀的现实
    2021-01-16 04:50

    There are a few issues in your code:

    1. Your it block will never be executed because you are not calling the testRequired function
    2. sendkeys() should be sendKeys(), and you are not sending any text, it's empty

    You 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);
        };
    });
    

    0 讨论(0)
提交回复
热议问题