Casperjs:打开一个新窗口/标签

时间:2015-07-22 18:24:37

标签: javascript phantomjs casperjs slimerjs

我正在尝试创建一个测试脚本,它将通过我的网站,点击一个链接并打开一个新的标签到另一个网站,填写一个表单提交它然后回到原始网站,但是每个例子我都有看着,试过不适合我。页面一直运行,直到它打开新窗口,然后新窗口在那里停留约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();});
});

1 个答案:

答案 0 :(得分:0)

需要从主流中删除以下代码并将其移入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']");
   });