waXve
waXve

Reputation: 862

protractor check if element is valid

I am wondering if there is a simpler way to check if an input field is valid using protractor. I wrote a helper function which looks like this:

isValid(css:string) {
    var deferred = protractor.promise.defer();

    expect(element(by.css(css)).isPresent()).toBe(true);

    element(by.css(css)).getAttribute('class').then(function (attributes) {
        var matches:string[] = attributes.match('ng-invalid');

        deferred.fulfill(matches === null || matches.length === 0);
    });

    return deferred.promise;
}

That works great but it seems not to be the way you use protractor. It seems to be to complicated...

Do any one of you have a simpler way? Something like

expect(element(by.css(css)).isValid()).toBeTruthy

Upvotes: 3

Views: 4477

Answers (1)

If your angular code has logic for form validation and properly updates ng-valid/ng-invalid, then you can just do

expect(hasClass(element(by.name('your-element')), 'ng-invalid')).toBe(true);
expect(hasClass(element(by.name('your-element')), 'ng-dirty')).toBe(true);

And if you want pass your-element as a parameter.

Upvotes: 3

Related Questions