javascript - 如何在 CasperJS 中打开新标签页

标签 javascript node.js casperjs

近一个月以来,我一直在使用 CasperJS 测试框架来制作一些测试套件,但其中一个我遇到了问题。

这是我想要做的:我正在浏览一个 url(第 1 页),我必须从另一个 url 执行另一个操作(模拟我们在图形浏览器上的新选项卡)而不退出第一个(第 1 页) ).来自第二个 url 的操作将改变我的第一个。希望它足够清楚:)

所以现在,当我到达第一个 url 上观察到的步骤时,我通过执行 thenOpen() 打开第二个 url,所以它正在进行一个新的导航步骤,我正在丢失当前 session ,我不能回来了。我尝试了很多方法,例如使用历史记录、重新打开页面、使用来自 CasperJS 的事件,我也尝试使用 PhantomJS 但没有成功。

下面是一些伪代码,以使其更清晰:

casper.test.begin("A random test suite", 0, function testSuite(test) {
    casper.start(url1, function () {
        casper.then(function() {
            // do some action on the first url
        });

        casper.then(function () {
            // open url2 and do some action in a new tab to not lose the session of url1
        });

        casper.then(function () {
            // check url1 (who should be still open)
        });
    });

    casper.run(function () {
        test.done();
    });
});

我真的很想使用 CasperJS 来做到这一点,但我开始认为这是不可能的,我开始寻找不同的解决方案,例如这篇文章: CasperJS, parallel browsing WITH the testing framework .但我以前从未使用过 node.js,所以如果这是唯一的方法,请给我一些例子。

最佳答案

通常,这是不可能的,因为一个 casper 脚本只在一个 phantomjs 运行时内运行。在您的情况下,这似乎是可能的。

注意:因为这依赖于第二个 casper 实例,所以不能在 casper test 环境中使用。

您可以在外部 casper 实例 (casper1) 的一步内创建一个新的 casper 实例 (casper2)。然后您必须指示 casper1 等待 casper2 实例完成,因为 casper 本质上是异步的。请记住,这与新选项卡完全一样,因此实例将共享缓存、cookie 和存储空间。

这是一个示例脚本:

var casper1 = require('casper').create();
var casper2done = false;

casper1.start("http://www.example.com").then(function(){
    casper1.capture("casper1_1.png");
    var casper2 = require('casper').create();
    casper2.start("http://stackoverflow.com/contact").then(function(){
        casper1.echo(casper2.getCurrentUrl(), casper2.getTitle());
        casper2.capture("casper2.png");
    }).run(function(){
        this.echo("DONE 2");
        casper2done = true;
    });
}).waitFor(function check(){
    return casper2done;
}).then(function(){
    casper1.echo(casper1.getCurrentUrl(), casper1.getTitle()); // Comment to fix answer (min 6 chars)
    casper1.capture("casper1_2.png");
}).run(function(){
    this.echo("DONE");
    this.exit();
});

这里我使用了 promise chaining/builder 模式。您甚至可以创建自己的函数来隐藏复杂性并使其可重复使用:

var casper = require('casper').create();

// IIFE to hide casper2done variable
(function(casper){
    var casper2done = false;
    casper.newTab = function(url, then, timeout){
        if (typeof url !== "string" || typeof then !== "function") {
            throw "URL or then callback are missing";
        }
        this.then(function(){
            var casper2 = require('casper').create();
            casper2.start(url).then(then).run(function(){
                casper2done = true;
            });
        }).waitFor(function check(){
            return casper2done;
        }, null, null, timeout).then(function(){
            casper2done = false;
        });
        return this;
    };
})(casper);

casper.start("http://www.example.com").newTab("http://stackoverflow.com/contact", function(){
    // this is casper2
    this.echo(this.getCurrentUrl(), this.getTitle());
    this.capture("casper2_1.png");
    this.thenClick("a#nav-askquestion");
    this.then(function(){
        this.echo(this.getCurrentUrl(), this.getTitle());
        this.capture("casper2_2.png");
    });
}, 15000).then(function(){
    // this is casper
    this.echo(casper.getCurrentUrl(), casper.getTitle());
    this.capture("casper1.png");
}).run(function(){
    this.echo("DONE");
    this.exit();
});

您可以在您的 casper 实例中使用多个步骤,但不要忘记指定一个好的超时时间。

关于javascript - 如何在 CasperJS 中打开新标签页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24880671/

相关文章:

ajax - 如何使用 casperjs 捕获和处理 XHR 响应中的数据?

javascript - 即使在使用 javascript 重新加载后也保留滚动条位置

node.js - 谷歌云+ Node ,grpc错误

javascript - 尝试将 JavaScript 对象转换为数组

node.js - Docker 容器中的 NSolid 应用程序注册了错误的 IP

javascript - 使用 Node js 读取谷歌电子表格数据不起作用..?

javascript - 如何更改 CasperJS 地理定位以进行抓取?

javascript - CasperJS POST AJAX 不起作用

javascript - 有人可以对 socket.io "helloworld"等价物给出具体的评论和解释吗?

javascript - 在图像完全加载之前显示预加载 gif?