Protractor 使用量角器检查元素是否缺失或不匹配?

Protractor 使用量角器检查元素是否缺失或不匹配?,protractor,Protractor,我正在使用量角器(在非角度页面上),希望没有特别的警告消息 所以我需要检查元素是否存在,然后执行toMatch()检查 请注意,我希望很快自己能回答这个问题,但如果你是一名持枪量角器测试撰稿人,你可以帮助我:-) 我需要将这两者结合起来: // utility to test expection of an element to match a regex: this.expectByCssToMatch = function(css, pattern) { browser.drive

我正在使用量角器(在非角度页面上),希望没有特别的警告消息

所以我需要检查元素是否存在,然后执行toMatch()检查

请注意,我希望很快自己能回答这个问题,但如果你是一名持枪量角器测试撰稿人,你可以帮助我:-)

我需要将这两者结合起来:

 // 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();
     });
 };

好的,这是我能想到的最好的了-看起来有点不整洁,但也许这就是方法?(似乎有效)

 // 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();
       }
   })
 };