I would like to fiddle a bit with PhantomJS and in particular with the waitFor script that is bundled with its examples. The purpose of my test is to check whether the English and the French version of the website lipsum.com are available. Here is my script :
var page = require('webpage').create(),
server = 'http://www.lipsum.com',
languages = ['en', 'fr'];
page.open(server, 'get', '', function (status) {
if (status !== 'success') {
console.log('Unable to reach the URL!');
} else {
check(languages.shift());
}
});
function check(currentLanguage) {
console.log('Checking '+currentLanguage);
waitFor(function() {
var classes = page.evaluate(function() {
// Checks if the current language is selected
return document.getElementsByClassName('zz')[0].className;
});
console.log("Classes for the selected element : " + classes);
return classes.indexOf(currentLanguage) === 0;
}, function() {
console.log(currentLanguage+' has been looked up.');
currentLanguage = languages.shift();
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function (language) {
// We click on the link related to the next language of the stack
$('.'+language).trigger('click');
}, currentLanguage);
});
check(currentLanguage);
});
}
What I would want is that the English, then the French versions of the page are displayed. Instead I get the following log :
Checking en
Classes for the selected element : en zz
'waitFor()' finished in 500ms.
en has been looked up.
Checking fr
Classes for the selected element : en zz
Classes for the selected element : en zz
Classes for the selected element : en zz
Classes for the selected element : en zz
Classes for the selected element : en zz
Classes for the selected element : en zz
Classes for the selected element : en zz
Classes for the selected element : en zz
Classes for the selected element : en zz
Classes for the selected element : en zz
Classes for the selected element : en zz
'waitFor()' timeout
It looks like the click on the link is not triggered, but I can't figure why.