正确的方法来限制硒元素搜索的等待时间

时间:2016-01-28 14:08:26

标签: javascript selenium selenium-webdriver nightwatch.js

我的夜班/硒测试代码使用

等代码查找页面中可能不存在的元素
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)
{...})

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

2 个答案:

答案 0 :(得分:0)

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

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

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

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/";
  }
};

答案 1 :(得分:0)

以下代码用于检查可见性,即使没有匹配也会继续

browser.waitForElementVisible('selector',timeout,false);
存在的

browser.waitForElementPresent('selector',timeout,false);

根据nightwatch api,

  

默认情况下,如果找不到该元素,则测试将失败。如果您希望测试继续进行,即使断言失败,也要将其设置为false。要全局设置,您可以在全局变量中定义属性abortOnAssertionFailure

有关详细说明,请点击此处: http://nightwatchjs.org/api/#waitForElementVisible

相关问题