【问题标题】:Proper way to limit wait time on selenium element search限制硒元素搜索等待时间的正确方法
【发布时间】:2016-01-28 14:08:25
【问题描述】:

我的 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')
})

如果找到元素,回调会被快速调用(1000ms)。我必须这样做数百次,并且只有少数元素与搜索条件匹配,因此它会为测试运行增加大量时间。

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

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

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

【问题讨论】:

    标签: javascript selenium selenium-webdriver nightwatch.js


    【解决方案1】:

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

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

    【讨论】:

    • waitForElementPresent 的默认行为在搜索失败时中止,从而终止整个测试。我只想测试是否存在并在没有匹配的情况下继续在其他地方,所以我必须将它的 abortOnFailure 参数设置为 false 并且必须为等待函数提供回调以记录有效结果。此外,我需要使用 selenium 元素引用作为 xpath 选择器的当前节点,并且看不到任何方法可以将该引用传递给等待函数。
    【解决方案2】:

    以下代码用于检查可见性并在没有匹配的情况下继续

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

    此为存在:

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

    根据nightwatch api,

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

    更详细的解释请看这里: http://nightwatchjs.org/api/#waitForElementVisible

    【讨论】:

      猜你喜欢
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多