我有几个测试来测试 UI,并同时用于创建数据。
一组单独的测试依赖于此数据,这意味着这些测试必须仅在第一组运行后运行。
我知道如何运行一组它们,或者使用标签运行它们,但如何才能按特定顺序运行它们?
Nightwatch 将按顺序在特定文件中运行每个测试,因此一种(天真的)解决方案是将每个测试按照您希望它们运行的顺序放在同一个文件中。
如果您对单个文件进行太多测试,这将变得很麻烦。为了解决这个问题,您可以利用 Nightwatch 按字母顺序运行每个测试文件。一种方法是在每个测试文件前添加一个数字作为前缀,指示您希望它们运行的顺序。例如,如果您有两个测试文件
before.js
和 after.js
,并且您想要 before.js
要首先运行,您只需将文件名更改为 01before.js
和 02after.js
即可。这将使 Nightwatch 按您想要的顺序读取文件。
这不是一个很好的答案,但它有效:对测试文件进行数字排序。
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.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
}
}
//第二个.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文件