测试没有执行 - 带有标准摩卡和幻影的守夜人

时间:2016-09-05 13:27:52

标签: phantomjs mocha nightwatch.js

所以我有一个nodejs服务器,我想用nightwatch开始一些功能测试。

如果我手动启动selenium并使用mocha运行它(命令:mocha ./test/functional/*.spec.js --compilers js:babel-register)它启动节点服务器并在firefox中正常运行(默认 - 顺便使用标准的mocha如何将其从默认的firefox更改为其他浏览器?):

import nightwatch from 'nightwatch';

require('../../server');

describe('Functional', function wrapIt() {
  const client = nightwatch.initClient({
    silent: true,
  });

  const browser = client.api();

  this.timeout(99999999);

  beforeEach((done) => {
    client.start(done);
  });

  it('should login', (done) => {
    browser
      .url('http://localhost:8441/')
      .waitForElementVisible('button[name=login]', 1000)
      .click('button[name=login]')
      .pause(1000);

    browser.expect.element('button').to.have.attribute('class').which.contains('theme__button');
    browser.end();
    client.start(done);
  });

  after((done) => {
    browser.end();
    client.start(done);
  });
});

现在我想把它放在Bamboo上并使用phantomjs,selenium服务器启动,nodejs服务器启动但是测试没有执行。 我有 nightwatch.json

{
  "src_folders" : ["test/night"],
  "output_folder": "reports",
  "test_runner" : "mocha",

  "selenium" : {
    "start_process": true,
    "server_path": ".//bin/selenium-server-standalone-2.53.1.jar",
    "log_path": "./reports",
    "host": "127.0.0.1",
    "port": 4444
  },

  "test_settings" : {
    "default" : {
      "desiredCapabilities": {
        "browserName": "phantomjs",
        "phantomjs.cli.args" : ["--ignore-ssl-errors=true"],
        "version":"latest",
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    }
  }
}

nightwatch.conf.js 是:

require('babel-core/register');

module.exports = require('./nightwatch.json');

和夜间文件夹中的测试脚本:

require('../../server');

describe('Functional', function wrapIt() {
  const client = this.client;
  const browser = client.api();

  this.timeout(99999999);

  beforeEach((done) => {
    client.start(done);
  });

  it('should login', (done) => {
    browser
      .url('http://localhost:8441/')
      .waitForElementVisible('button[name=login]', 1000)
      .click('button[name=login]')
      .pause(1000);

    browser.expect.element('button').to.have.attribute('class').which.contains('theme__button');
    browser.end();
    client.start(done);
  });

  after((done) => {
    browser.end();
    client.start(done);
  });
});

所以用nightwatch运行它会启动selenium服务器,nodejs服务器启动也没问题。

1 个答案:

答案 0 :(得分:0)

I changed it using webdriver:

export const client = require('webdriverjs').remote({ // eslint-disable-line
  // Settings
  desiredCapabilities: {
    // You may choose other browsers
    // http://code.google.com/p/selenium/wiki/DesiredCapabilities
    browserName: 'phantomjs',
  },
  // webdriverjs has a lot of output which is generally useless
  // However, if anything goes wrong, remove this to see more details
  logLevel: 'silent',
});

client.init();

and:

require('../../server');

const client = require('./client').client;

describe('Functional testing', function wrapIt() {
  this.timeout(99999999);

  before((done) => {
    client.init().url('http://localhost:8441/', done);
  });

  describe('Check homepage', () => {
    it('should login', (done) => {
      client.waitFor('button[name=login]', 100, () => {
        client.click('button[name=login]', () => {
          client.element('.theme__button*');
          client.waitForVisible(1000);
          client.end();
        });
      });
      done();
    });
  });

  after((done) => {
    client.end();
    done();
  });
});

and it works running mocha './test/funcTest.js' --compilers js:babel-register -t 10000

PS: selenium is already started local and on bamboo I added a commmand to start it