Casperjs:打开一个新的 window/tab

Casperjs: Open a new window/tab

我正在尝试创建一个将通过我的网站的测试脚本,单击 link 并打开一个新选项卡到另一个网站,填写表格提交然后返回原始网站,但是我看过并尝试过的每个例子都不适合我。该页面一直运行到它打开新的 window,然后新的 window 在那里停留大约 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']");
   });