【问题标题】:Webdriver.io - how to use beforeEach hooks in the configWebdriver.io - 如何在配置中使用 beforeEach 钩子
【发布时间】:2016-04-17 11:34:36
【问题描述】:

我正在使用 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 运行,而不是针对每个测试。

【问题讨论】:

    标签: node.js database selenium webdriver mean


    【解决方案1】:

    是的,看到这个网址:http://webdriver.io/guide/testrunner/gettingstarted.html

    如果您启动 wdio 配置,将生成一个名为 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();
            });
        });
    
    
    },
    

    【讨论】:

    • 要启动酱汁连接,我建议使用酱汁服​​务webdriver.io/guide/services/sauce.html,这样更方便
    • 在配置文件 afterStep:function() 中发布屏幕截图命令的最佳位置是什么??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    相关资源
    最近更新 更多