【发布时间】:2015-09-09 11:16:03
【问题描述】:
我收到了一个关于循环扩展测试的问题。我有一个 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]
];
现在我想根据以下策略进行测试:
对所有 VIEWPORT SIZES 的 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 包含/加载这些测试文件一次。
那么我可以在这里做什么,我需要在最内部的循环中多次加载测试文件。
测试文件只是像 sn-ps
casper.then(function () {
casper.waitForSelector(x("//a[normalize-space(text())='Bla']"),
function success() {
DO GOOD STUFF
},
function fail() {
BAD THIGNS HAPPENED
});
});
在第一次运行时包含文件,在所有其他运行中 > 1 不包含任何内容,循环运行正确但 require 不起作用。
这绝对是必需的功能,因为当我将测试代码从文件中直接复制到循环中时,它也会多次工作。
【问题讨论】:
标签: javascript loops casperjs require