How to echo the missing selector in a CasperJS onWaitTimeout event handler?

497 Views Asked by At

I have many casper.waitForSelector parts in my script. Anyway I would like to know whenever it times out. Therefore I added:

casper.options.onWaitTimeout = function() {
    //how to echo Selector which timed out here
};

Furthermore I would like to echo the selector which timed out.

1

There are 1 best solutions below

0
On BEST ANSWER

The signature of onWaitTimeout is:

onWaitTimeout(Integer timeout, Object details)

Which is sadly not correctly documented. The details object that is passed into the function contains a selector property that represents the selector. If it is an XPath selector, then you will need to get the path property of that.

casper.options.onWaitTimeout = function(timeout, details) {
    var selector = details.selector.type === 'xpath' ? 
            details.selector.path : details.selector;
    this.echo("Wait timed out after " + timeout + " msec with selector: " + selector);
};

Please note that this function will catch all wait timeouts and you will have to exit the script yourself if you so choose.

It's always a good idea to look at the source code: 1 & 2.