Users click this link:
<span onclick="slow_function_that_fills_the_panel(); $('#panel').show();">
Now I'm simulating the click in phantomjs:
page.evaluate(
function() { $("#panel").click(); }
);
console.log('SUCCESS');
phantom.exit();
Phantom exits before the slow function ends its execution and the DIV becomes visible. How can I implement waiting?
Heres a spin of Cybermaxs's answer:
Example of use:
It's a little easier when you use a config variable.
Inside page.evaluate(), use the self.loading property to test for doneness ....
My approach for this scenario is to wait until "something" is done or true. I highly suggest you to test waitfor.js.
demo.html
demoscript.js
This script evaluate
$('#thediv').is(':visible')
(classic Jquery code) every 500 ms to check if the div is visible.PhantomJS runs asynchronously by default, causing problems like the one you describe above (where the script finishes before your results are ready)
However there is nothing to stop you using it in a synchronous way.
Just use
phantom.page.sendEvent('mousemove')
in a while loop. This will keep looping through the event pump until the webkit engine loads your page or processes any necessary browser events.Note that
page.loading
can also be any other boolean condition, for example:I discovered this approach while working on the triflejs.org project (the Internet Explorer version of phantom) trying to emulate calls to
trifle.wait(ms)
inside the PhantomJS environment.