有没有办法在Nightwatch.js中动态创建测试用例?
一个用例示例:
我想从Qual-E测试引擎运行“一致性”测试套件,并使用Nightwatch.js从页面中简单地读取测试用例的结果。此时我有一个模块文件,每个测试用例定义为一个单独的函数:
module.exports = {
'AudioContext' : function (browser) {
// test's code
},
...
'MediaList.length' : function (browser) {
// test's code
}
};
当来自Qual-E测试引擎的“一致性”测试套件发生变化(这种情况时有发生)时,我需要更新模块文件中的测试用例列表。我想在这个模块文件中只有一个函数(例如before函数),它将作为第一步读取Qual-E页面并在运行时生成测试用例,因此我将始终拥有最新的测试套件。
事实证明,每个导出的函数都被视为测试用例函数(除了一些保留函数,如before
,after
等)。这是一个示例解决方案:
module.exports = {
...
};
(function() {
var testCasesList = [
// [testCaseID, testCaseName]
];
function testFunction(browser, testCaseID) {
// Generic test case body
}
function createTests(object) {
function createFunction(testCaseID) {
return function(browser) {
testFunction(browser, testCaseID);
};
}
for (var i = 0; i < testCasesList.length; i++) {
testCaseID = testCasesList[i][0];
object[testCasesList[i][1]] = createFunction(testCaseID);
}
}
createTests(module.exports);
})();