如何在CasperJS中打开一个新选项卡

How to open a new tab in CasperJS

本文关键字:一个 新选项 选项 CasperJS      更新时间:2023-09-26

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

这是我想做的:我正在浏览一个url (page1),我必须从另一个url(模拟一个新的选项卡,就像我们在我们的图形浏览器)不退出第一个(page1)的另一个动作。第二个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做到这一点,但我开始认为这是不可能的,我开始寻找不同的解决方案,如这篇文章:CasperJS,与测试框架并行浏览。但是我以前从来没有使用过node.js,所以如果这是唯一的方法,请给我一些例子。

一般来说,这是不可能的,因为casper脚本只在一个phantomjs运行时内运行。对你方来说,这似乎是可能的。

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

您可以在外部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 chain/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实例中使用多个步骤,但不要忘记指定一个好的超时时间。