打开一个新窗口/选项卡
Casperjs: Open a new window/tab
我试图创建一个测试脚本,将通过我的网站,点击一个链接,打开一个新的选项卡到另一个网站,填写表单提交,然后回到原来的网站,但是我看过的每一个例子,并尝试不适合我。页面一直运行,直到它打开新窗口,然后新窗口在那里停留大约5秒钟,然后一切都关闭了。这是我得到的:
var x = require('casper').selectXPath;
casper.options.viewportSize = {width: 1920, height: 1075};
casper.on('page.error', function(msg, trace) {
this.echo('Error: ' + msg, 'ERROR');
for(var i=0; i<trace.length; i++) {
var step = trace[i];
this.echo(' ' + step.file + ' (line ' + step.line + ')', 'ERROR');
}
});
casper.test.begin('Resurrectio test', function(test) {
casper.start('https://mywebsite1/abc/default.asp');
casper.waitForSelector("form[name=FormSize] input[name='a']",
function success() {
test.assertExists("form[name=FormSize] input[name='Nickname']");
this.click("form[name=FormSize] input[name='Account']");
},
function fail() {
test.assertExists("form[name=FormSize] input[name='Nickname']");
});
casper.waitForSelector("input[name='Nickname']",
function success() {
this.sendKeys("input[name='Nickname']", "abcco40");
},
function fail() {
test.assertExists("input[name='Nickname']");
});
casper.waitForSelector("form[name=FormSize] input[name='Username']",
function success() {
test.assertExists("form[name=FormSize] input[name='Username']");
this.click("form[name=FormSize] input[name='Username']");
},
function fail() {
test.assertExists("form[name=FormSize] input[name='Username']");
});
casper.waitForSelector("input[name='Username']",
function success() {
this.sendKeys("input[name='Username']", "k_csr");
},
function fail() {
test.assertExists("input[name='Username']");
});
casper.waitForSelector("form[name=FormSize] input[name='Password']",
function success() {
test.assertExists("form[name=FormSize] input[name='Password']");
this.click("form[name=FormSize] input[name='Password']");
},
function fail() {
test.assertExists("form[name=FormSize] input[name='Password']");
});
casper.waitForSelector("input[name='Password']",
function success() {
this.sendKeys("input[name='Password']", "kcsr");
},
function fail() {
test.assertExists("input[name='Password']");
});
casper.waitForSelector("form[name=FormSize] input[type=submit][value='Logon']",
function success() {
test.assertExists("form[name=FormSize] input[type=submit][value='Logon']");
this.click("form[name=FormSize] input[type=submit][value='Logon']");
},
function fail() {
test.assertExists("form[name=FormSize] input[type=submit][value='Logon']");
});
/* submit form */
casper.waitForSelector(x("//a[normalize-space(text())='One Time Payment']"),
function success() {
test.assertExists(x("//a[normalize-space(text())='One Time Payment']"));
this.click(x("//a[normalize-space(text())='One Time Payment']"));
},
function fail() {
test.assertExists(x("//a[normalize-space(text())='One Time Payment']"));
});
casper.waitForPopup(/https:'/'/secondwebsite'/home'/three'.aspx/).withPopup(/https:'/'/secondwebsite'/home'/three'.aspx/, function(){
popup.close();
});
casper.then(function() {
});
/* submit form */
casper.waitForSelector("form#aspnetForm input[type=button][value='Back']",
function success() {
test.assertExists("form#aspnetForm input[type=button][value='Back']");
this.click("form#aspnetForm input[type=button][value='Back']");
},
function fail() {
test.assertExists("form#aspnetForm input[type=button][value='Back']");
});
casper.waitForSelector(x("//a[normalize-space(text())='Document Manager']"),
function success() {
test.assertExists(x("//a[normalize-space(text())='Document Manager']"));
this.click(x("//a[normalize-space(text())='Document Manager']"));
},
function fail() {
test.assertExists(x("//a[normalize-space(text())='Document Manager']"));
});
casper.run(function() {test.done();});
});
需要从主流程中删除以下代码并将其移动到withPopup函数中。此外,在withPopup和waitForPopUp传入的参数需要有正则表达式,而不是链接,这是我以前没有完全理解的。当传递这些参数时,你不需要在引号中封装它们,这部分我不完全理解为什么,但它希望参数作为一个没有引号的纯正则表达式。
casper.waitForSelector("form#aspnetForm input[type=button][value='Back']",
function success() {
test.assertExists("form#aspnetForm input[type=button][value='Back']");
this.click("form#aspnetForm input[type=button][value='Back']");
},
function fail() {
test.assertExists("form#aspnetForm input[type=button][value='Back']");
});
相关文章:
- 激活镀铬窗口选项卡
- 检测内容'It’’’’我在窗口选项卡上
- 如何通过单击Highchart图中的列/栏打开新窗口/选项卡
- Javascript表单提交打开新窗口选项卡,然后重定向父页面
- 来自 HREF 的新窗口/选项卡
- 检查锚点打开的窗口/选项卡是否已关闭
- 离开窗口选项卡时堆叠(重叠)的图像
- 如何从JavaScript弹出窗口打开父窗口选项卡
- 将表单提交到新窗口/选项卡
- 如何在新窗口/选项卡中打开文本和 xml 文件
- 检测链接是在新窗口/选项卡还是当前窗口中打开
- 在c中按下一个按钮,打开两个新的窗口/选项卡(不同的外部站点)
- 新窗口/选项卡中的JavaScript确认对话框
- 如何在浏览器窗口/选项卡关闭时删除本地存储项目
- 如何打开新窗口/选项卡,但专注于打开窗口
- 打开一个新窗口/选项卡
- Chrome窗口不会从无限滚动加载内容,除非窗口(选项卡)处于焦点位置
- JQuery发送带有Headers的Ajax GET,并在新窗口/选项卡中打开
- 如何防止firefox内部的javascript关闭窗口/选项卡
- 关闭窗口/选项卡时的Websocket-Chrome