小编典典

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

selenium

我不想 无休止 地测试,但我不能这样做。

下面的代码 启动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();

下面的代码( 摩卡 测试代码) 不启动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();
  })
});

测试结果如下:

  WebdriverIO Sample Test
    ✓ should return "Google"

  1 passing (4ms)

阅读 366

收藏
2020-06-26

共1个答案

小编典典

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

没有这个,Mocha会认为您的测试是同步的,因此它只是在完成webdriverio工作之前就完成了测试。

2020-06-26