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
waXve
  • 792
  • 2
  • 9
  • 30

1 Answers1

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.

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
  • 1
    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 Mar 26 '15 at 13:59
  • @waXve you can read about the hasClass method here: http://stackoverflow.com/questions/5085567/hasclass-with-javascript-or-site-with-jquery-to-javascript-translation – Misbah Khan Jul 15 '15 at 18:28