如何在独立模式下调试webdriverio? [英] How to debug webdriverio in standalone mode?

查看:262
本文介绍了如何在独立模式下调试webdriverio?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我很难尝试使用Chromedriver调试webdriverio测试。您根本无法单步执行代码,因为webdriverio命令是异步的,并且浏览器会话与测试不同步。

I'm going absolutely nuts trying to debug a webdriverio test using Chromedriver. You simply cannot step through the code because webdriverio commands are asynchronous and the browser session is out of sync with the test.

这很令人沮丧,因为阅读文档,似乎你需要一个像Chai或wdio这样的测试框架来生成测试,但这似乎只是为了获得程序化的同步命令。

This is frustrating because reading the docs, it seems you need a testing framework like Chai or wdio to generate tests, but this seems like a lot of work just to have procedural synchronous commands.

我只需要抓取一些网站使用webdriverio但这个异步命令很难使用Chrome devtools进行调试。

I just need to crawl some websites using webdriverio but this asynchronous commands are far too difficult to debug using Chrome devtools.

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

Is there any way to force webdriverio to behave synchronously?

ex)

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

除了 loadedPage 尚未就绪且未定义调试时间移到下一行。

except loadedPage is not ready and is undefined by the time debug moves to next line.

推荐答案

好吧, WebdriverIO 只是一个宝石自动化框架和全面的文档是其强大的功能之一。正如您正确指出的那样,一切都是异步的,但是对于WDIO,如果您来自传统的顺序编程背景,您还可以选择完全同步。

Well, WebdriverIO is just a jewel of an automation framework and the comprehensive documentation is one of its strong suits. As you correctly pointed out, everything is asynchronous, but with WDIO you also have the option to go full-synchronous if you come from a traditional sequential programming background.

  • Asynchronous approach (without using the WDIO test-runner):

首先,你将不得不阅读一些关于 JavaScript承诺 ,尤其是 .then()函数。

First off, you will have to read-up a bit about JavaScript Promises, especially the .then() function.

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()语句中使用同步命令。

Using the above approach, you will always have to chain your commands, but you can also use synchronous commands inside the .then() statement.

出于调试目的,WebdriverIO开箱即用,设计精美的Read-Eval-Print-Loop( REPL曲面)以 .debug()的形式 命令 。只需将其添加到您希望执行停止的测试用例中,以便在所选终端内进行调试。

For debug purposes, WebdriverIO comes out-of-the-box with a beautifully designed Read-Eval-Print-Loop (REPL inferface) in the form of the .debug() command. Just add it into your test-case prior to where you want the execution to stop so you can debug inside your terminal of choice.

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

!!! Note: The default timeout for the .debug() command is short. Make sure you increase it.

  • Synchronous approach (using the WDIO test-runner):

如果您发现上述方法是痛苦的屁股,那么为什么不使用WDIO测试运行器让您的生活更轻松?您可以从运行向导开始:

If you find the above approach a pain-in-the-arse, then why not use the WDIO test-runner to make your life easier? You can start by running the wizard:

// 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 文件。测试运行程序将使用它来运行测试用例。测试运行器还抽象了 .client()的初始化,你不会再处理它了。只需选择一个框架来运行测试用例(Mocha,Cucumber或Jasmine)并开始编写测试。

The above will spawn the wdio.conf.js file in your project root. It will be used by the test-runner to run your test-cases. The test-runner also abstracts the initialization of your .client(), you you won't been to deal with it anymore. Just pick a framework to run your test-cases (Mocha, Cucumber, or Jasmine) and start writing your tests.

!!!注意:从现在开始,浏览器将成为您的驱动程序对象。
此外,请确保配置 wdio.conf.js 文件以支持这种运行测试用例的方式:设置sync-flag以支持此方法: sync:true 。您可以通过 wdio wdio.conf.js 命令运行测试。

!!! Note: From now on, browser will be your driver object. Also, make sure you have the wdio.conf.js file configured to support this way of running your test cases: Set the sync-flag to support this approach: sync: true. You can run your tests via the wdio wdio.conf.js command.

您的测试应如下所示(使用摩卡):

Your tests should look like this (using Mocha):

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 <==
    });
});




  • 异步方法(使用 WDIO test-runner ):

    • Asynchronous approach (using the WDIO test-runner):
    • 这是我的首选方法。它为您提供了对测试用例执行的最佳控制,但如果刚刚开始,我不推荐它。基本上它就是上面的例子,但是所有命令都是链接的。

      This is my go-to approach. It gives you the best control possible over your test-case execution, but I don't recommend it if your just starting out. Basically it's the above example, but all the commands are chained.

      !!!注意:请确保您拥有 sync:false 标记设置。

      !!! Note: Make sure you have the sync: false flag setup for this.

      告诉我如果这有帮助。干杯!

      Let me know if this helps. Cheers!

      这篇关于如何在独立模式下调试webdriverio?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆