如何在独立模式下同步运行webdriverio?

http://webdriver.io/guide/getstarted/modes.html

我要绝对坚持使用Chromedriverdebuggingwebdriveriotesting。 由于webdriverio命令是asynchronous的,并且浏览器会话与testing不同步,因此您无法单步执行代码。

这是令人沮丧的,因为阅读文档,看起来你需要像Chai或wdio这样的testing框架来生成testing,但是这似乎有很多工作只是为了有程序上的同步命令。

我只需要使用webdriverio抓取一些网站,但这个asynchronous命令太难以使用Chrome开发工具进行debugging。

有没有办法强制webdriverio行为同步?

EX)

var loadedPage = webdriverio.remote(options).init().url('https://google.com');

除了loadedPage没有准备好,并且在debugging移到下一行时未定义。

那么, WebdriverIO只是一个自动化框架的gem,全面的文档是其强大的function之一。 正如您正确指出的那样, 一切都是asynchronous的 ,但是使用WDIO,如果您来自传统的顺序编程背景,则还可以select完全同步。

  • asynchronous方法(不使用WDIOtesting运行程序 ):

首先,你将不得不阅读一些有关JavaScript Promises的内容 ,特别是.then()函数。

 var webdriverio = require('webdriverio'); var options = { desiredCapabilities: { browserName: 'chrome' } }; var client = webdriverio.remote(options); client .init() .url('https://duckduckgo.com/') .setValue('#search_form_input_homepage', 'WebdriverIO') .click('#search_button_homepage') .getTitle() .then(function(title) { console.log('Title is: ' + title); // outputs: "Title is: WebdriverIO (Software) at DuckDuckGo" }) .end(); 

使用上述方法,您将始终链接您的命令 ,但是您也可以在.then()语句中使用同步命令。

为了进行debugging,WebdriverIO以.debug() 命令的forms出现了一个精美的Read-Eval-Print-Loop( REPL界面 )。 只要将其添加到您的testing案例之前,你想执行停止,所以你可以在你select的terminal内进行debugging。

注意: .debug()命令的默认超时时间很短。 确保你增加它。


  • 同步方法(使用WDIOtesting运行器 ):

如果你发现上面的方法是一个痛苦的屁股 ,那么为什么不使用WDIOtesting运行器让你的生活更轻松呢? 您可以通过运行向导开始:

 // if you installed the package globally, or you have the wdio // binary in your PATH wdio config // or. from the root of your project ./node_nodules/.bin/wdio config 

以上将生成项目根目录下的wdio.conf.js文件。 它将被testing运行者用来运行你的testing用例。 testing运行者也抽象了你的.client()的初始化,你将不会再处​​理它了。 只需select一个框架来运行testing用例(摩卡,黄瓜或茉莉花),然后开始编写testing。

注意:从现在开始, browser将是您的驱动程序对象。 另外,请确保已将wdio.conf.js文件configuration为支持运行testing用例的这种方式:将sync-flag设置为支持以下方法: sync: true 。 您可以通过wdio wdio.conf.js命令运行testing。

你的testing应该是这样的(使用摩卡):

 var expect = require('chai').expect; describe("Testing Robots Emporium Test Suite", function() { beforeEach(function() { // ==> Your setup here <== browser.url('http://www.kevinlamping.com/webdriverio-course-content/index.html') var currentUrl = browser.getUrl(); expect(currentUrl).include("/index.html"); }) it("The FAQs was rendered properly", function() { var height = browser.getCssProperty("ul.accordion", 'height'); // Added a debug step just to show you how easy it is to debug browser.debug(); expect(height.parsed.value).to.be.above(300); // The first element was expanded var firstItemText = browser.getText('ul.accordion li:nth-of-type(1) div'); expect(firstItemText).to.contain('be of the metal type.'); }); afterEach(function() { // ==> Your cleanup here <== }); }); 
  • asynchronous方法(使用WDIOtesting运行程序 ):

这是我的做法。 它为您提供了对您的testing案例执行的最佳控制,但是如果您刚开始,我不会推荐它。 基本上就是上面的例子,但是所有的命令都是链接的。

注意:确保你有这个sync: false标志设置。

让我知道这是否有帮助。 干杯!