当Casperjs在另一个页面上时,它会加载任何URL

Cem*_*mir 1 phantomjs casperjs

我有两个网址X和Y.当casperjs在X页面上时,它必须调用Y页面,在得到响应之后它应该继续.

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.
Run Code Online (Sandbox Code Playgroud)

我能怎么做?

Saa*_*aad 8

您必须casper.then在点击后使用.这是一些代码:

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();
});
Run Code Online (Sandbox Code Playgroud)

基本上它去了stackoverflow.com,等到Questions按钮加载,点击它并检查重定向的URL是否有效.

//console.log(this.getCurrentUrl());如果要查看特定网址,可以取消注释.

现在让我们说你想要一个全新的页面,我们可以使用thenOpenapi.

我强烈建议你阅读这篇博客:http://blog.newrelic.com/2013/06/04/simpler-ui-testing-with-casperjs-2/