Cem Demir
Cem Demir

Reputation: 142

Casperjs load any url, when it's on another page

I have two urls X and Y. When casperjs is on X page, it have to call Y page and after getting the response it should continue.

casper.start("X URL",function() {
   var casper2 = require('casper').create();

   casper2.start();
   casper2.open("Y URL", function() {
        RESPONSE
   });
   casper2.run();

}).then... >> It have to wait for response.

How can i do?

Upvotes: 1

Views: 9649

Answers (1)

Saad
Saad

Reputation: 28486

You have to use casper.then after the click. Here is some code:

var x = require('casper').selectXPath;
var url = 'http://stackoverflow.com/';

var casper = require('casper').create({
    verbose: false,
    logLevel: 'debug'
});

casper.test.comment('Starting Testing');

casper.start(url, function() {

    //console.log(this.getCurrentUrl());

    this.test.assert(
        this.getCurrentUrl() === url, 'url is the one expected'
    );

    this.test.assertHttpStatus(200, + 'site is up');

    casper.waitForSelector(x("//a[normalize-space(text())='Questions']"),
        function success() {
            this.test.assertExists(x("//a[normalize-space(text())='Questions']"));
            this.click(x("//a[normalize-space(text())='Questions']"));
        },
        function fail() {
            this.test.assertExists(x("//a[normalize-space(text())='Questions']"));
        }
    );   

    casper.then(function() {

            //console.log(this.getCurrentUrl());
            this.test.assertUrlMatches("http://stackoverflow.com/questions",
                "clicked through to questions page");
    });

    casper.thenOpen('http://reddit.com', function() {
        this.test.assertUrlMatches("http://www.reddit.com/",
                "On Reddit");
    });


});

casper.run(function() {
    this.echo('finished');
    this.exit();
});

Basically it goes to stackoverflow.com, waits until the Questions button is loaded, clicks on it and checks whether the redirected url is valid.

You can uncomment //console.log(this.getCurrentUrl()); if you want to see the specific url.

Now lets say you want to go to to an entirely new page, we can do use thenOpen api.

I highly recommend you read this blog: http://blog.newrelic.com/2013/06/04/simpler-ui-testing-with-casperjs-2/

Upvotes: 8

Related Questions