如何添加等待测试?

时间:2017-03-28 23:57:02

标签: webdriver-io

我正在使用WebdriverIO进行以下测试

webdriverio
    .remote(options)
    .init()
    .url('http://www.google.com')
    .getTitle().then(function(title) {
        console.log('Title was: ' + title);
    })
    .end(); 

但是,我的ISP在google.com之间提供了一个网络病毒检查页面,因此测试始终返回病毒检查页面标题。如何确保结果始终返回Google?

2 个答案:

答案 0 :(得分:4)

在测试中使用暂停是一种不好的做法。最好使用显式等待。

E.g。您可以使用Wdio API提供的waitUntil方法。因此,您可以创建辅助函数来等待预期的URL。

以下是ES6中的示例:

function waitForUrl(url, timeout) {
  browser.waitUntil(() => browser.getUrl().includes(url)
  }, timeout, `Expected url must be ${url}`)
}

您可以简单地将函数(或promise)作为waitUntil条件以及timeout(以ms为单位),错误消息和间隔(也以ms为单位,默认值为500ms)传递。因此,waitUntil将等待,直到该条件满足truthy值。否则,将抛出错误。

答案 1 :(得分:0)

您可以使用pause(time)功能暂停测试一段时间,直到您被重定向到Google。

webdriverio
.remote(options)
.init()
.url('http://www.google.com')
.pause(5000)
.getTitle().then(function(title) {
    console.log('Title was: ' + title);
})
.end();