I am trying to do some testing with phantomjs... basically I want to:
It seems difficult for phantomjs to notice when the page has loaded though.
I could use phantomjs to set a "test" variable on the window or something so that the page js could check that and then call a callback once it's done. The trouble is the callback can only be a page callback, so it can't do anything that a page couldn't do.
That might be ok for the tess but the last step is not possible.
I came up with this:
page.onConsoleMessage = function(msg) {
if (msg == "__quit__") {
phantom.exit();
}
else {
console.log("page: " + msg);
}
};
page.evaluate(function () {
window.quit = function () { console.log("__quit__"); };
});
So the page code can call window.quit() and the console monitor can then kill phantom. This seems a bit hacky though. Does anyone have a better way of doing it?
Disclaimer: this is untested code
You might be able to attach to the onLoadFinished callback of the webpage object. This executes in the scope of PhantomJS so you'll have all your variables available.
Something like this should suffice:
page.onLoadFinished = function(){
page.evaluate(function() {
//call your testing code here
});
phantom.exit();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With