如何同步运行webdriver.io?

How to run webdriver.io synchronously?

webdriver.io's what's new in v4.0 文档说 "It's all synchronous...All commands now block the execution of the test process until they’ve resolved."

我能找到的同步 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 { state: 'pending' },表示这是一个承诺。我怎样才能说服它以同步方式运行,理想情况下不必使用异步/等待?

启动浏览器后

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

webdriver.io 和 chrome 浏览器存在一个众所周知的问题,该问题不属于此 awnser,但结果也将解释您的问题。 Chrome 在低硬件 pc 上冻结,如果你 运行 一个 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 端点。