【问题标题】:How to run webdriver.io synchronously?如何同步运行 webdriver.io?
【发布时间】:2018-08-11 01:02:51
【问题描述】:

webdriver.io's what's new in v4.0 docs 说“这都是同步的......现在所有命令都会阻止测试过程的执行,直到它们解决为止。”

我能找到的同步 WebDriver 代码的唯一示例是:

browser.url('/');
var title = browser.getTitle();

当我执行类似的操作时(通过note test.js,而不是wdio):

var webdriverio = require('webdriverio');
var options = {
  desiredCapabilities: {
    browserName: 'chrome',
    logLevel: 'silent'
  }
};

const driver = webdriverio.remote(options)
driver.url('http://www.google.com')
const title = driver.getTitle()
console.log('title', title)

...title 是title { state: 'pending' },表示这是一个promise。我怎样才能说服它以同步方式运行,理想情况下不必使用 async / await?

【问题讨论】:

  • 我可能弄错了,但看起来文章说都是同步的,仅供测试

标签: node.js google-chrome selenium-webdriver webdriver webdriver-io


【解决方案1】:

启动浏览器后

const client = webdriverio.remote(options).init()

webdriver.io 和 chrome 浏览器存在一个众所周知的问题,它不属于此 awnser,但结果也可以解释您的问题。 Chrome 在低硬件电脑上冻结,如果您运行 webdriver.io 命令直到 chrome 完全加载您的脚本崩溃。一种解决方法是以此为例:

 client
 .url('http://localhost/dashboard')
 .waitForVisible('body', 20000000).then(function(isExisitingLOCALHOST){
    //.. you may add here aswell a timeout if your hardware is really low and has really long freezing.

              client
              .url('http://yourwebsite.com') // <-- webdriver.io will wait until your loading icon from the tab is ready. This means at ajax websites you must build aswell a workaround with the waitForVisible() endpoint. As example waiting for a specific item to load logo, text etc.
              .click('#sample')

  })

通过这个小解决方法,您可以确保在启动过程中避免浏览器冻结并等待它完成。这也解释了与 webdriver.io 的同步编写方式

您可以无限同步您的 webdriver.io API 端点:

client
.click()
.pause(1000)
.rightClick()

同样重要的是要知道 pause() 在某些时候确实有问题,它不会同步,有时它不工作。你应该改用 JavaScript 中的基本 setTimeout。

你也可以这样写

client.getText('#sample1');
client.getText('#sample2');

处理多个相邻的 api 端点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多