3

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
1
  • Whats wrong with just expect(hasClass(element(by.name('your-element')), 'ng-invalid')).toBe(true);? Maybe check for ng-dirty too. Commented Mar 26, 2015 at 13:49

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, wait... one more thing... I use Typescript to write my tests and it seems that it does not know where to find hasClass... Do you know from which library that was? It is not in the Protractor Api doku nor in the one from Jasmine...
@waXve you can read about the hasClass method here: stackoverflow.com/questions/5085567/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.