Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 811
waXve Avatar asked Dec 01 '25 16:12

waXve


1 Answers

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.

like image 72
David says Reinstate Monica Avatar answered Dec 03 '25 19:12

David says Reinstate Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!