Search code examples
phantomjscasperjs

Capture Popup page content through CasperJS or PhantomJS


A.html

<input type="submit" name="testLabel" value="Print Test" id="testLabel" onclick="myFunction('<?php echo $dynamic_page;?>')" >

<script>
function myFunction(page) {
    var strWindowFeatures = "location=yes,height=570,width=520,scrollbars=yes,status=yes";
    window.open(page, "_blank",strWindowFeatures);
}
</script>

CasperJS code

var casper = require('casper').create();

casper.start('http://localhost/test/a.html', function() {
    this.echo(this.getTitle());
});

casper.thenClick('#testLabel');

casper.then(function() {
    this.capture('page.png');
});

casper.run();

I have also try with phantomjs but i am not able to capture b.html page in page.png

Note : Popup page name is not fixed.


Solution

  • // this will set the popup DOM as the main active one only for time the

    // step closure being executed

    casper.withPopup(0, function() {
        this.test.assertTitle('Popup title');
    });
    

    Complete code

    var casper = require('casper').create();
    
    casper.start('http://localhost/test/a.html', function () {
        this.echo(this.getTitle());
    });
    
    casper.thenClick('#testLabel');
    
    casper.waitForPopup(0, function () {
        this.echo('Popup');
    });
    
    casper.then(function () {
        this.capture('page.png');
    });
    
    casper.run();
    

    Read more about waitForPopup