我的 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/