Search code examples
javascriptphantomjscasperjs

Prevent CasperJS from exiting when all steps are complete


I have a dynamic CasperJS suite, that works around WebServer built-in in PhantomJS. New steps are added dynamically to the suite.

However, right now, Casper exists as soon as all pending steps are completed.

How do I prevent it from automatically closing and wait for more steps to be added dynamically?


Solution

  • You can pass a onComplete function to casper.run(), if the onComplete function never ends, the casper won't exit. Try this code:

    var casper = require('casper').create({
        verbose: true,
        logLevel: "debug",
    });
    
    casper.start()
    
    casper.then(function () {
        casper.echo("the first step")
    })
    
    casper.then(function () {
        casper.echo("the second step")
    })
    
    casper.then(function () {
        casper.echo("the third step")
    })
    
    casper.run(function () {
        setInterval(function () {
            casper.echo('step: ' + casper.step)
        }, 1000)
    })