Webdriver.io - 如何在配置中使用beforeEach挂钩

时间:2016-01-12 17:58:25

标签: node.js database selenium webdriver mean

我正在使用MEAN堆栈和Webdriver构建应用程序进行测试。

目前我正在使用Mocha的beforeEach和afterEach挂钩在测试之间清理数据库,例如:

describe('Links', function() {
  // drop DB collections

  beforeEach(function(done){
    //create database objects
  });

  afterEach(function(done){
    // drop DB collections
  });
});

有没有办法设置wdio.conf.js,以便在我的每个测试之前和之后自动进行?配置的before:after: function() {}作为beforeAll / afterAll而不是每次测试运行。

1 个答案:

答案 0 :(得分:0)

是的,请参阅此网址:http://webdriver.io/guide/testrunner/gettingstarted.html

如果你启动了wdio config,会生成一个名为wdio.conf.js的文件,在这个文件中存在函数,在测试之后或之前启动脚本,我展示的函数示例包含这个文件:

// =====
// Hooks
// =====
// Run functions before or after the test. If one of them returns with a promise, WebdriverIO
// will wait until that promise got resolved to continue.
//
// Gets executed before all workers get launched.
onPrepare: function() {
    // do something
},
//
// Gets executed before test execution begins. At this point you will have access to all global
// variables like `browser`. It is the perfect place to define custom commands.
before: function() {
    // do something
},
//
// Gets executed after all tests are done. You still have access to all global variables from
// the test.
after: function(failures, pid) {
    // do something
},
//
// Gets executed after all workers got shut down and the process is about to exit. It is not
// possible to defer the end of the process using a promise.
onComplete: function() {

    // do something
}

重要的是,如果你以异步方式启动脚本,并且在清理数据库的那一刻等待回调,那么你需要一个promise,否则下一步钩子将启动而不是等待前一个函数钩子,例如:

    onPrepare: function() {
    return new Promise(function(resolve, reject) {
        sauceConnectLauncher({
          username: 'the_pianist2',
          accessKey: '67224e83a-1cf7440d-8c88-857c4d3cde49',
        }, function (err, sauceConnectProcess) {
            if (err) {
                return reject(err);
            }
            console.log('success');
            resolve();
        });
    });


},