A question has come up regarding extended testing within loops. The scenario involves a 3-level loop structure, incorporating URLs, Testfiles, and Viewportsizes as displayed below:
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]
];
The objective is to execute the following strategy:
Run all TESTS on ALL URLs with ALL VIEWPORT SIZES
This will be implemented in the following structure:
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) {
/* THE ISSUE LIES HERE - REQUIRE() ONLY WORKS ONCE */
casper.then(function(){
require('.' + testfiles[i]);
});
});
});
}));
})
.run(function() {
this.test.done();
});
As indicated in the code, there is a limitation where the testfiles can only be included/loaded once using require.
So, the challenge remains: how can these testfiles be loaded multiple times within the inner loop?
The testfiles consist of snippets such as
casper.then(function () {
casper.waitForSelector(x("//a[normalize-space(text())='Bla']"),
function success() {
DO GOOD STUFF
},
function fail() {
BAD THINGS HAPPENED
});
});
Currently, the file is included in the first run, but subsequent runs beyond 1 do not include it. Although the loops operate correctly, the require functionality does not persist.
It is evident that the issue lies with the require functionality because copying the test code directly into the loop allows it to work multiple times.