如何同步运行webdriver.io?

问题描述 投票:0回答:1

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

node.js google-chrome selenium-webdriver webdriver webdriver-io
1个回答
1
投票

启动浏览器后

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端点。

© www.soinside.com 2019 - 2024. All rights reserved.