CasperJS 在具有 require 或其他功能的循环中动态包含测试文件
CasperJS include testfile dynamically in a loop with require or other function
我有一个关于循环扩展测试的问题。我有一个 3 级循环结构,其中有 URLs、Testfiles 和 Viewportsizes,如下所示:
var navigation = [
"http://www.url_1.com",
"http://www.url_2.com",
"http://www.url_3.com",
"http://www.url_4.com"
];
var testfiles = [
"/componenttests/atoms/test_dropdown_buttons.js",
"/componenttests/atoms/test_conditional_buttons.js",
"/componenttests/atoms/test_icon_buttons.js"
];
var viewPortsizes = [
[1440, 900],
[320, 480],
[320, 568],
[600, 1024],
[1024, 768],
[1280, 800]
];
现在我想根据以下策略对此进行测试:
运行 对具有所有视口大小的所有 URL 的所有测试
在以下结构中实现:
casper.start().then(function(){
/* Loop through all URLs so that all are visited */
casper.eachThen(navigation, (function(response){
var actUrl = response.data;
/* Test different viewport resolutions for every URL */
casper.eachThen(viewportSizes, function (responseView) {
var actViewport = responseView.data;
/* Set the viewport */
casper.then(function () {
casper.viewport(actViewport[0], actViewport[1]);
});
/* Open the respective page and wait until its opened */
casper.thenOpen(actUrl).waitForUrl(actUrl, function () {
/* Single tests for every resolution and link */
casper.each(testfiles, function (self, actTest, i) {
/* AND HERE THE PROBLEM IS LOCATED, REQUIRE() ONLY WORKS ONCE */
casper.then(function(){
require('.' + testfiles[i]);
});
});
});
}));
})
.run(function() {
this.test.done();
});
如代码中所述,问题是我只能使用 require 一次包含/加载这些测试文件。
那么我在这里能做什么,我需要在最内层循环中多次加载测试文件。
测试文件只是像
这样的片段
casper.then(function () {
casper.waitForSelector(x("//a[normalize-space(text())='Bla']"),
function success() {
DO GOOD STUFF
},
function fail() {
BAD THIGNS HAPPENED
});
});
目前在第一个 运行 中包含文件,在所有其他 运行s > 1 中没有包含任何内容,循环 运行 正确但 require 不起作用.
这绝对是 require 功能,因为当我将测试代码从文件直接复制到循环中时,它也工作了多次。
我看到两个选项:
- 将您的组件编写为适当的模块并在脚本开头要求它们或
- 读取组件测试文件并
eval
它。
适当的模块
您可以将测试组件定义为
exports.test = function(){
casper.then(function () {
...
});
};
那么你可以在开头要求它们:
testfiles = testfiles.map(function(path){
return {
path: path,
test: require("." + path).test
}
});
并直接在测试工具中使用它们:
casper.then(function(){
testfiles[i].test();
});
每回合评估
或者您可以在不更改测试组件的情况下简单地在测试工具中使用它:
var fs = require("fs");
...
casper.then(function(){
eval(fs.read("."+testfiles[i]));
});
我有一个关于循环扩展测试的问题。我有一个 3 级循环结构,其中有 URLs、Testfiles 和 Viewportsizes,如下所示:
var navigation = [
"http://www.url_1.com",
"http://www.url_2.com",
"http://www.url_3.com",
"http://www.url_4.com"
];
var testfiles = [
"/componenttests/atoms/test_dropdown_buttons.js",
"/componenttests/atoms/test_conditional_buttons.js",
"/componenttests/atoms/test_icon_buttons.js"
];
var viewPortsizes = [
[1440, 900],
[320, 480],
[320, 568],
[600, 1024],
[1024, 768],
[1280, 800]
];
现在我想根据以下策略对此进行测试:
运行 对具有所有视口大小的所有 URL 的所有测试
在以下结构中实现:
casper.start().then(function(){
/* Loop through all URLs so that all are visited */
casper.eachThen(navigation, (function(response){
var actUrl = response.data;
/* Test different viewport resolutions for every URL */
casper.eachThen(viewportSizes, function (responseView) {
var actViewport = responseView.data;
/* Set the viewport */
casper.then(function () {
casper.viewport(actViewport[0], actViewport[1]);
});
/* Open the respective page and wait until its opened */
casper.thenOpen(actUrl).waitForUrl(actUrl, function () {
/* Single tests for every resolution and link */
casper.each(testfiles, function (self, actTest, i) {
/* AND HERE THE PROBLEM IS LOCATED, REQUIRE() ONLY WORKS ONCE */
casper.then(function(){
require('.' + testfiles[i]);
});
});
});
}));
})
.run(function() {
this.test.done();
});
如代码中所述,问题是我只能使用 require 一次包含/加载这些测试文件。
那么我在这里能做什么,我需要在最内层循环中多次加载测试文件。
测试文件只是像
这样的片段casper.then(function () {
casper.waitForSelector(x("//a[normalize-space(text())='Bla']"),
function success() {
DO GOOD STUFF
},
function fail() {
BAD THIGNS HAPPENED
});
});
目前在第一个 运行 中包含文件,在所有其他 运行s > 1 中没有包含任何内容,循环 运行 正确但 require 不起作用.
这绝对是 require 功能,因为当我将测试代码从文件直接复制到循环中时,它也工作了多次。
我看到两个选项:
- 将您的组件编写为适当的模块并在脚本开头要求它们或
- 读取组件测试文件并
eval
它。
适当的模块
您可以将测试组件定义为
exports.test = function(){
casper.then(function () {
...
});
};
那么你可以在开头要求它们:
testfiles = testfiles.map(function(path){
return {
path: path,
test: require("." + path).test
}
});
并直接在测试工具中使用它们:
casper.then(function(){
testfiles[i].test();
});
每回合评估
或者您可以在不更改测试组件的情况下简单地在测试工具中使用它:
var fs = require("fs");
...
casper.then(function(){
eval(fs.read("."+testfiles[i]));
});