您必须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,您可以取消注释。
现在假设您想转到一个全新的页面,我们可以使用thenOpen
api。
我强烈建议您阅读此博客:http: //blog.newrelic.com/2013/06/04/simpler-ui-testing-with-casperjs-2/