I'd like to check with PhantomJS whether my script correctly opens a new window/tab on click. The open is triggerd by a js event listener and opened through window.open(url, "_blank")
.
How can I listen for the new window with PhantomJS?
I'd like to check with PhantomJS whether my script correctly opens a new window/tab on click. The open is triggerd by a js event listener and opened through window.open(url, "_blank")
.
How can I listen for the new window with PhantomJS?
There seem to be three ways to do this:
onPageCreated
CasperJS solves this by using
page.onPageCreated
. So whenwindow.open
is called in the page, a new page is created andpage.onPageCreated
is triggered with the newly created page.pages
PhantomJS'
page
has apages
property which handles of the child pages. So, when youopen
a new page/tab, a newwebpage
object is created for that page. You need to try to add anonLoadFinished
event listener to the page before it fires (no promises). It can be hard and when thewindow.open
is called with an unknown delay from the page context.This can be fixed by using something like
waitFor
to wait for the new page to appear and attach the event handler before the page is loaded. Here is the complete code with a small adjustment. The retry interval is reduced to 50ms from 250ms.Proxy solution
The
window.open
function can be proxied and after the opening the page in the page context, an event handler can be registered in phantom context signaled throughwindow.callPhantom
and caught inonCallback
.