ErichBSchulz
ErichBSchulz

Reputation: 15669

check an element is either absent or does not match using protractor?

I'm using protractor (on non-angular pages) and want to expect that particular warning messages are absent.

so I need to check for presence of the element, then do a toMatch() check...

be warned I'm expecting to answer this myself shortly - but if you're a gun protractor test writer you can help me :-)

I need to combine these two:

 // utility to test expection of an element to match a regex:
 this.expectByCssToMatch = function(css, pattern) {
   browser.driver.findElement(
     by.css(css)).getText().then(function(text) {
       expect(text).toMatch(pattern);
     });
 };
 // utility to test expection an element is missing:
 this.expectByCssToBeAbsent = function(css) {
   browser.driver.isElementPresent(
     by.css(css)).then(function(present) {
       expect(present).toBeFalsy();
     });
 };

Upvotes: 1

Views: 2557

Answers (1)

ErichBSchulz
ErichBSchulz

Reputation: 15669

ok this is the best I can come up with - seems a bit untidy but maybe this is the way? (it seems to work)

 // utility to test expection of an element to not match a regex:
 this.expectByCssNotToMatch = function(css, pattern) {
   browser.driver.isElementPresent(by.css(css)).
     then(function(present) {
       if (present) {
         browser.driver.findElement(by.css(css)).getText().
         then(function(text) {
           expect(text).not.toMatch(pattern);
         });
       }
       else { // element absent so we have no match (pass)
         expect(present).toBeFalsy();
       }
   })
 };

Upvotes: 1

Related Questions