一个简单的WebdriverIO-Mocha测试不会显示浏览器 [英] A simple WebdriverIO - Mocha test doesn't display browser

查看:187
本文介绍了一个简单的WebdriverIO-Mocha测试不会显示浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to test NOT headlessly but I cannot do that.

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

以下代码(摩卡测试代码)没有通过$ mocha test.js启动chrome浏览器.

The below code (Mocha test code) doesn't start chrome browser by $ mocha test.js.

无头. NG.

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

But the test pass! I cannot understand this.

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

I checked the log of Selenium Server, but it doesn't show (left) any log. No trace.

// 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) 

推荐答案

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

webdriver.io is asynchronous. Change your test to mark it as asynchronous and use the done callback after all the checks in the test are done. The two changes are: 1. add done as a parameter to the function you pass to it and 2. add the done() call after your expect call.

  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工作之前完成了测试.

Without this, Mocha thinks your test is synchronous so it just completes the test before webdriverio does its work.

这篇关于一个简单的WebdriverIO-Mocha测试不会显示浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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