1

我有两个网址 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.

我能怎么做?

4

1 回答 1

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();
});

基本上它会转到stackoverflow.com,等待Questions按钮加载,单击它并检查重定向的url是否有效。

//console.log(this.getCurrentUrl());如果您想查看特定的 url,您可以取消注释。

现在假设您想转到一个全新的页面,我们可以使用thenOpenapi

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

于 2013-08-12T17:33:48.373 回答