有没有理由说.then()会导致会话在没有活动会话的情况下丢弃/(重新)加载页面?
在下面的示例中,您可以看到如何使用登录建立会话,然后执行搜索并加载结果页面。会话在此导航中幸存,但在使用.then()分隔现在显示的页面上的测试时,DOM不再对应于登录用户将看到的内容。我不明白为什么。
casper.start("http://example.dev", function() {
this.test.assertExists('form[name="login"]', 'login form is shown'); // PASS
this.fill('form[name="login"]', {
user: "username",
password: "Password1"
}, true);
});
casper.then(function() {
this.test.assertExists('body[data-userid="42"]', 'User is logged in'); // PASS
this.sendKeys("input#search", "12345");
this.click("button#submit_search"); // form submit causes redirect
});
casper.then(function() {
this.test.assertExists('body.page_id_12345', 'search for "12345" loads page?id=12345'); // PASS
this.test.assertExists('body[data-userid="42"]', 'User is logged in'); // PASS
});
// PROBLEM BELOW:
casper.then(function() {
this.test.assertExists('body[data-userid="42"]', 'User is logged in'); // FAIL
});
casper.run();
在OS X上使用CasperJS 1.1.0-beta4和PhantomJS
答案 0 :(得分:-1)
您的测试结果是什么?
你可以尝试这样做,看它是否会抛出同样的错误:
casper.waitForSelector('body[data-userid="42"]',
function success() {
test.assertExsists('body[data-userid="42"]', 'User is logged in');
//do something...
}
);