【问题标题】:A simple WebdriverIO - Mocha test doesn't display browser一个简单的 WebdriverIO - Mocha 测试不显示浏览器
【发布时间】:2014-11-26 07:47:21
【问题描述】:

我想不盲目地进行测试,但我做不到。

下面的代码启动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 测试代码)不启动 chrome 浏览器 by $ mocha test.js

无头。吴。

但是测试通过了!我无法理解这。

我检查了 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) 

【问题讨论】:

    标签: javascript selenium webdriver mocha.js webdriver-io


    【解决方案1】:

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

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

    【讨论】:

    • 虽然这看起来很明显,但所有测试都成功完成让我很困惑。非常感谢您的提问和回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多