I am trying to open 2 angular pages in an application to get their screenshots using phantomjs. Page 1 needs to be opened before page 2, as it prepares some data for page 2. I use two nested functions setTimeout()
as follows:
var page = require('webpage').create(),
t, url;
phantom.addCookie({
'name': 'token',
'value': '<authentication-token-goes-here>',
'domain': 'localhost'
});
t = Date.now();
url = "http://localhost:8000/#/page1";
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
page.viewportSize = {
width: 1366,
height: 768
};
page.clipRect = {
top: 0,
left: 0,
width: 1366,
height: 768
};
page.open(url, function(status) {
setTimeout(function() {
console.log('page 1 status: ', status);
page.render("page1.png");
var url = "http://localhost:8000/#/page2";
page.open(url, function(status) {
console.log('page 2 status: ', status);
setTimeout(function() {
page.render("page2.png");
phantom.exit();
}, 5000);
});
}, 5000);
});
The first console instruction: it console.log('page 1 status: ', status);
prints, and I get application logs and a screenshot of page 1, but the second console log (page 2 status) is not printed because the internal call is page.open()
not being called, It also hangs the console itself, because it is phantom.exit()
not being called due to not being called callback.
page.open()
, 2 (, XHR)! , 2 .
- ( Google facebook), . angular. ?