【问题标题】:Simple protractor test for isElementPresent failing with unsupported locator strategyisElementPresent 的简单量角器测试因不支持的定位器策略而失败
【发布时间】:2014-01-20 22:24:42
【问题描述】:

我的测试:

it('should allow login', function() {
  browser.get('index.html');

  $('#username').sendKeys('administrator');
  $('#password').sendKeys('password');
  $('#login').click();

  var logout = $('#logout');
  expect($p.isElementPresent(logout)).to.eventually.be.true;
}); 

但这会出错:

Error: Unsupported locator strategy: click
  at Error (<anonymous>)
  at Function.webdriver.Locator.createFromObj (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/locators.js:97:9)
  at Function.webdriver.Locator.checkLocator (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/locators.js:111:33)
  at webdriver.WebDriver.findElements (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:805:31)
  at webdriver.WebDriver.isElementPresent (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:787:29)
  at Protractor.isElementPresent (/usr/local/lib/node_modules/protractor/lib/protractor.js:476:22)
  at /Users/pschuegr/wt/client/e2e/login_test.js:26:15

奇怪的是,它指向的是 isElementPresent 线,而不是点击的那条线。我对 webdriver 很陌生,如果我错过了一些明显的东西,我深表歉意。我正在使用 mocha 框架(这意味着量角器的金丝雀版本)运行,fwiw。

任何想法表示赞赏。

【问题讨论】:

  • 什么是 $p,它来自哪里?
  • $p 是量角器实例(即 ptor)
  • 现在$pptor 已被browser 有效取代

标签: angularjs mocha.js protractor


【解决方案1】:

使用最新的 Protractor 构建,您可以将上述答案缩短为以下内容:

expect(element(by.css('#logout')).isPresent()).toBeTruthy();

这样您不必执行 browser.wait 并减少对 isElementPresent 的调用次数。

【讨论】:

  • 这里在哪里等待,为什么?我不明白为什么您的任何更改都会使browser.wait 调用变得不必要,所以我认为量角器内部发生了变化,这使得这成为可能而无需明确等待?如果是这样,那是什么变化?
  • @MarkAmery 在我看来似乎还没有,这可能适用于已经可用的元素,而不是您正在等待的元素。
  • @MarkAmery protractor 在它的所有功能中都使用了 Promise,并且还在解析之前等待 $http 请求完成(如果有的话)。因此,有了这里的答案,期望将 1)获取元素 2)当它解决时,它将检查元素是否存在 3)当它解决时,将触发对真实结果的期望。等待发生在幕后,您不需要手动等待,步骤 1、2 和 3 中的每个承诺都会根据需要处理等待每个承诺得到解决。
【解决方案2】:

$('#logout') 是一个 WebElement。 isElementPresent 采用定位器,例如 by.css

$('#username').sendKeys('administrator');
$('#password').sendKeys('password');
$('#login').click();

var logout = by.css('#logout');
browser.wait(function() { return $p.isElementPresent(logout); }, 8000);
expect($p.isElementPresent(logout)).toBeTruthy();

【讨论】:

    【解决方案3】:

    下面的代码 sn-p 描述了我会采取的最安全的方法:

    it('should return true when element is present', function () {
     var logout;
     logout = $('#logout');
    
      browser.driver.isElementPresent(logout).then(function (isPresent) {
        isPresent = (isPresent) ? true : browser.wait(function () {
          return browser.driver.isElementPresent(logout );
        }, 15000); //timeout after 15s 
        expect(isPresent).toBeTruthy();
      });
    });
    

    上面的代码以一个检查元素是否存在的promise开始,如果为真则分配它true,否则等待并继续池化接下来的15秒以查看元素是否存在,在这两种情况下我们都期望它是真实的。

    【讨论】:

      【解决方案4】:

      这应该可行:

      var logout = $('#logout');
      expect(logout.isPresent()).to.eventually.be.true;
      

      【讨论】:

      • isPresent() 不返回承诺,因此您不能使用“最终”!我也犯了同样的错误... -.-
      • 谢谢@Sentenza 这个评论很有帮助。
      猜你喜欢
      • 2015-03-17
      • 2022-12-22
      • 2016-08-09
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      相关资源
      最近更新 更多