Javascript 如何在CasperJS中打开新选项卡
我使用CasperJS测试框架来制作一些测试套件已经快一个月了,但是我在其中一个测试套件中遇到了一个问题 下面是我想做的:我正在浏览一个url(第1页),我必须从另一个url(模拟图形浏览器上的新选项卡)执行另一个操作,而不必退出第一个url(第1页)。来自第二个url的操作将更改我的第一个url。希望这足够清楚:) 所以现在,当我到达第一个url时,我通过执行Javascript 如何在CasperJS中打开新选项卡,javascript,node.js,casperjs,Javascript,Node.js,Casperjs,我使用CasperJS测试框架来制作一些测试套件已经快一个月了,但是我在其中一个测试套件中遇到了一个问题 下面是我想做的:我正在浏览一个url(第1页),我必须从另一个url(模拟图形浏览器上的新选项卡)执行另一个操作,而不必退出第一个url(第1页)。来自第二个url的操作将更改我的第一个url。希望这足够清楚:) 所以现在,当我到达第一个url时,我通过执行然后open()打开了第二个url,因此它正在执行一个新的导航步骤,我正在丢失当前会话,我无法返回。我尝试了很多方法,比如使用历史记录、
然后open()
打开了第二个url,因此它正在执行一个新的导航步骤,我正在丢失当前会话,我无法返回。我尝试了很多方法,比如使用历史记录、重新打开页面、使用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来实现这一点,但我开始认为这是不可能的,我开始寻找不同的解决方案,比如这篇文章:
. 但是我以前从未使用过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();
});
这里我使用承诺链接/构建器模式。您甚至可以创建自己的函数来隐藏复杂性并使其可重复使用:
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实例中使用多个步骤,但不要忘记指定一个好的超时。可能重复使用casperjs test子命令可以做到这一点吗?因为我们无法在测试环境中覆盖预配置的casper实例。这是不可能的。然后,您必须通过模块生成一个子模块。可以像编写一个将被沙盒处理的求值调用一样编写它。