一个简单的WebdriverIO - Mocha测试不显示浏览器

Fee*_*ics 2 javascript selenium webdriver mocha.js webdriver-io

我想要无头地测试,但我不能那样做.

以下代码启动chrome浏览器.不是无头.好.

// test.js

var webdriverio = require('webdriverio');

var options = {
  desiredCapabilities: {
    browserName: 'chrome'
  }
};

webdriverio
  .remote(options)
  .init()
  .url('http://www.google.com')
  .title(function(err, res) {
    console.log('Title was: ' + res.value);
  })
  .end();
Run Code Online (Sandbox Code Playgroud)

下面的代码(摩卡测试代码)不启动Chrome浏览器$ mocha test.js.

无头.NG.

但测试通过了!我不明白这.

我检查了Selenium Server的日志,但它没有显示(左)任何日志.没有踪影.

// test-mocha.js

var expect = require('expect.js');
var webdriverio = require('webdriverio');

var options = {
  desiredCapabilities: {
    browserName: 'chrome'
  }
};

describe('WebdriverIO Sample Test', function () {
  it('should return "Google"', function () {
    webdriverio
      .remote(options)
      .init()
      .url('http://www.google.com')
      .title(function(err, res) {
        var title = res.value;
        expect(title).to.be('Google');
      })
      .end();
  })
});
Run Code Online (Sandbox Code Playgroud)

测试结果如下:

  WebdriverIO Sample Test
    ? should return "Google"

  1 passing (4ms) 
Run Code Online (Sandbox Code Playgroud)

Lou*_*uis 7

webdriver.io是异步的.更改测试以将其标记为异步,并done在完成测试中的所有检查后使用回调.这两个更改是:1.done作为参数添加到您传递给的功能it,2.在done()通话后添加expect通话.

  it('should return "Google"', function (done) { // <- 1
    webdriverio
      .remote(options)
      .init()
      .url('http://www.google.com')
      .title(function(err, res) {
        var title = res.value;
        expect(title).to.be('Google');
        done(); // <- 2
      })
      .end();
  })
Run Code Online (Sandbox Code Playgroud)

如果没有这个,Mocha认为你的测试是同步的,所以它只是在webdriverio它的工作之前完成测试.