如何在独立模式下调试Webdriverio?

4

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

我正在尝试使用Chromedriver调试webdriverio测试,但是由于webdriverio命令是异步的,因此无法通过代码单步调试,浏览器会话与测试不同步。

阅读文档后,发现需要像Chai或wdio这样的测试框架来生成测试,但是为了实现过程式同步命令,这似乎需要大量工作。

我只需要使用webdriverio爬取一些网站,但是使用Chrome开发工具调试这些异步命令非常困难。

是否有任何方法可以强制webdriverio表现为同步?

例如)

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

但是loadedPage没有准备好,并且在调试移动到下一行时变成未定义。

1个回答

5

正如您所指出的,一切都是异步的,但使用WDIO,如果您来自传统的顺序编程背景,您也可以选择全同步。

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

    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();
    

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

    For debug purposes, WebdriverIO comes out-of-the-box with a beautifully designed Read-Eval-Print-Loop (REPL) interface 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.

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

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

    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
    

    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.

    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 <==
         });
     });
    
  • 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 you're just starting out. Basically it's the above example, but all the commands are chained.

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


1
你的注释中有几个错误,例如同步执行应该是sync: true而不是sync: false,你需要使用expect而不是assert,并且使用afterEach进行清理而不是beforeEach - M. Herold
@M.Herold 非常感谢!你在错误方面的判断非常准确。那天匆忙地从一个项目中复制粘贴过来的。干杯,伙计! - iamdanchiv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接