NodeJS:Traocha失败的Mocha + Selenium测试失败(在本地取得成功)

时间:2017-03-14 13:14:22

标签: node.js selenium-webdriver mocha travis-ci

我在Travis上使用Selenium WebDriver进行自动Mocha测试。如果我只是同步运行测试并在结束时调用done(),那么测试会很好地通过。一旦我引入了异步行为(即。.then(function(){... done()}),Travis测试就会失败,尽管在本地它们仍然按照它们应该运行。

例如:

describe('Map load test', function() {
    let driver = new webdriver.Builder()
        .forBrowser('firefox')
        .build();
    this.timeout(60000);

    beforeEach(function(done) {
        console.log('beforeEach');
        driver.get(index).then(function() {
            console.log('Loaded index');
            done();
        });
    });

    after(function() {
        console.log('after');
        driver.quit();
    });

    it('Should load the choropleth demo without issues', function(done) {
        console.log('Starting Choropleth test');
        driver.getCurrentUrl().then(function(url) {
            assert.equal(url, index, 'Initial url did not match');
            console.log('loaded' + url);
            driver.findElement(By.css('#demo0')).click();
            driver.findElement(By.css('.loadDemoButton')).click();
            driver.sleep(3000);
            console.log('slept for 3000');
            driver.getCurrentUrl().then(function(url) {
                assert.equal(url, index + '#edit', 'File loaded url did not match');
                done();
            });
        });

    });

结果(注意 - 甚至没有进入实际测试,但在每次之前失败)

Map load test

beforeEach

    1) "before each" hook for "Should load the choropleth demo without issues"

after

  0 passing (1m)

  1 failing

  1) Map load test "before each" hook for "Should load the choropleth demo without issues":

     Error: Timeout of 60000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

但是在做

beforeEach(function(done) {
        console.log('beforeEach');
        driver.get(index).then(done());
    });

继续进行下一次测试。

Travis.yml:

language: node_js
env:
  - CXX=g++-5
addons:
  apt:
    sources:
      - ubuntu-toolchain-r-test
    packages:
      - g++-5
node_js:
 - "7.6.0"
before_install:
  - wget https://github.com/mozilla/geckodriver/releases/download/v0.14.0/geckodriver-v0.14.0-linux64.tar.gz
  - mkdir geckodriver
  - tar -xzf geckodriver-v0.14.0-linux64.tar.gz -C geckodriver
  - export PATH=$PATH:$PWD/geckodriver

The test file

Failing Travis build logs

感谢您的任何建议。调试Travis失败就是这样的PITA ......

1 个答案:

答案 0 :(得分:0)

嗯,经过一些帮助后,我设法让它工作,但我仍然不确定为什么。关键是将xvfb添加到travis.yml:

language: node_js

addons:
  firefox: "51.0.1"

node_js:
 - "7.6.0"

before_script:
  - "export DISPLAY=:99.0"
  - "sh -e /etc/init.d/xvfb start"
  - sleep 3 # give xvfb some time to start