如何按特定顺序运行夜巡测试



我有几个测试,用于测试UI并在此过程中创建数据。

另一组测试依赖于此数据,这意味着这些测试必须仅在第一组测试运行之后运行。

我知道如何运行一组它们,或者使用标签运行它们,但是我如何以特定的顺序运行它们?

Nightwatch将按顺序在一个特定的文件中运行每个测试,所以一个(天真的)解决方案是将每个测试按您希望它们运行的顺序放在同一个文件中。

如果一个文件有太多的测试,这将变得笨拙。为了解决这个问题,您可以利用Nightwatch按字母顺序运行每个测试文件。这样做的一种方法是在每个测试文件前面加上一个数字,指示您希望它们运行的顺序。例如,如果您有两个测试文件,before.jsafter.js,并且您希望首先运行before.js,那么您可以将文件的名称更改为01before.js02after.js

这不是一个很好的答案,但它有效:按数字顺序排列您的测试文件。

0001_first_test_I_want_to_run.js
0002_second_test_I_want_to_run.js
...
9999_last_test_I-want_to_run.js

为了控制订单(并使用通用模块进行身份验证),我使用了"main"test模块,并按照我想要的顺序导入测试:

在main.test.js

// import test modules
const first = require('./first.test.js');
const second = require('./second.test.js');
module.exports = {
    before(){
         // login, etc.
    },
    'first': (browser) => {
         first.run(browser);
    },
    'second': (browser) => {
         second.run(browser);
    },
}

和first.test.js

var tests = {
    'google': (browser) => {
        browser.url('https://google.com';
    },
    'cnn': (browser) => {
        browser.url('https://cnn.com';
    }
};
module.exports = {
    // allow nightwatch to run test module only inside _main
    '@disabled': true,
    'run': (browser) => {
        // call all functions inside tests
        Object.values(tests)
            .filter(f => typeof f === 'function')
            .forEach(f => f(browser));
    }
};

如果你有first.js和second.js文件,那么创建一个新的文件main.js,并将这些文件中存在的所有函数导入到main.js中。

first.js:

module.exports = {
 'function1' function(browser){
    //test code
 },
 'function11' function(browser){
    //test code
  }
}

second.js:

module.exports = {
 'function2' function(browser){
    //test code
}
}

main.js:

const { function1,function11 } = require('./path/to/first.js')
const { function2 } = require('./path/to/second.js')
module.exports = {
    //run the functions mentioned in a order which you want
    run: function (browser) {
     funtion1(browser)
     function11(browser)
     function2(browser)
    }
 }

现在执行main.js文件

最新更新