在CasperJS中与弹出窗口共享cookie

Sharing cookies with popup window in CasperJS

本文关键字:窗口 共享 cookie CasperJS      更新时间:2023-09-26

我有一个CasperJS项目,需要一个新窗口(单击"#view_item"按钮时打开)才能访问父窗口的cookie和会话。当前,新窗口显示错误,因为它认为没有用户登录。

this.click("#view_item");
this.waitForPopup(/.+/, function() {
    this.capture("parent_window.jpg");
});
this.withPopup(/.+/, function()
{
    // Delay for testing
    this.wait(2000);
    this.capture("new_window.jpg");
});

在打开窗口,在弹出窗口中加载它们并刷新页面之前,除了将cookie转储到文件之外,我还能做些什么吗?

您可以尝试覆盖来自父窗口的cookie(这只比将其写入文件短一点)。

var cookie = this.evaluate(function(){
    return window.document.cookie; // just a string
});
this.click("#view_item");
this.waitForPopup(/.+/, function() {
    this.capture("parent_window.jpg");
});
this.withPopup(/.+/, function()
{
    // Delay for testing
    this.wait(2000);
    this.evaluate(function(cookie){
        window.document.cookie = cookie;
    }, cookie);
    this.wait(100); // little time to actually change the cookie
    this.reload();
    this.wait(2000, function(){  // only necessary if page is built with JS
        this.capture("new_window.jpg");
    });
});

我不认为有一个更简单的方法,如果子窗口还没有拿起饼干。也许casperjs的bug报告是合适的。


问题可能是你捕获的截图太早了。wait是阶跃异步阶跃函数,而capture是同步阶跃函数。在capture之前调用wait意味着waitcapture之后执行

this.wait(2000, function(){
    this.capture("new_window.jpg");
});