javascript - 限制 Selenium 元素搜索等待时间的正确方法

标签 javascript selenium selenium-webdriver nightwatch.js

我的 nightwatch/selenium 测试代码使用诸如

之类的代码查找页面中可能不存在的元素
browser.elementIdElement(ELEMENT,'class name', 'myclass', function(r)
{   if (r.status == 0) console.log('found match')
    else               console.log('did not find match')
})

如果找到该元素,则会快速调用回调(< 50ms),但如果没有元素匹配,则回调需要更长的时间(>1000ms)。我必须执行此操作数百次,并且只有少数元素符合搜索条件,因此它会增加测试运行的大量时间。

我想限制 Selenium 花在搜索元素上的时间。我尝试使用 selenium timeoutsImplicitWait() 函数,例如

browser.timeoutsImplicitWait(250)
       .elementIdElement(ELEMENT,'class name', 'myclass', function(r)
{...})

但不影响性能。限制元素搜索时间的正确方法是什么?

最佳答案

也许我误解了你的问题;这两种模式都适合我:

client
.useXpath().waitForElementPresent(selector, this.timeout)
.useCss().waitForElementPresent(selector, this.timeout)

this.timeout 在基本测试用例的原型(prototype)中设置。

util.inherits(MyExampleBaseClass, Base);

MyExampleBaseClass.prototype = {
  before: function (client) {
    // call super-before
    Base.prototype.before.call(this, client);
    this.timeout = 250;
  },

  after: function (client, callback) {
    // call super-after
    Base.prototype.after.call(this, client, callback);
  },

  // Note: This method will not be mistaken by nightwatch for a step because
  // it is not enumerable (since it's on the prototype)
  getSiteURL: function () {
    return "http://www.urlundertest.com/";
  }
};

关于javascript - 限制 Selenium 元素搜索等待时间的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35063756/

相关文章:

javascript - 如何根据索引删除一行?

javascript - 使用 Sinon 模拟常量/变量?

python - Selenium Chrome 、Python。在网站上找不到该按钮

javascript - 禁用 JavaScript 的 Selenium WebDriver 测试

java - 使用一个java类的变量到另一个java类

c# - Specflow - 完成所有测试后关闭浏览器

javascript - 为什么IE和Chrome中的ajax错误不同?

javascript - 为什么 `void 0` 可能是 “faster” 而不是 `undefined` ?

Python webdriver : driver. get(URL) 无法在单元测试中打开 - 通过控制台工作

java - sendKeys 未在 WebDriver 中发送 '@' 字符