小编典典

如何在CasperJS中打开新标签页

node.js

自从将近一个月以来,我一直在使用CasperJS测试框架来制作一些测试套件,但是其中之一面临着一个问题。

这是我要执行的操作:我正在浏览一个url(第1页),并且必须从另一个url(像在图形浏览器中那样模拟新选项卡)执行另一个操作,而不必退出第一个(第1页)。来自第二个URL的操作将更改我的第一个URL。希望它足够清楚:)

因此,现在,当我到达观察第一个URL的步骤时,我通过执行操作打开了第二个URL
thenOpen(),因此,它进入了一个新的导航步骤,而我丢失了当前会话,因此无法返回。我尝试了多种方式,例如使用历史记录,重新打开页面,使用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来做到这一点,但是我开始认为这是不可能的,并且我开始寻找不同的解决方案。


阅读 327

收藏
2020-07-07

共1个答案

小编典典

通常,这是不可能的,因为Casper脚本只能在一个phantomjs运行时中运行。就您而言,这似乎是可能的。

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

您可以casper2在外部Casper实例(casper1)的一个步骤中创建一个新的Casper实例()。然后,您必须指示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链接/构建器模式。您甚至可以创建自己的函数来隐藏复杂性并使其可重复使用:

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实例中使用多个步骤,但不要忘记指定良好的超时时间。

2020-07-07