繁体   English   中英

Nightmarejs在同一测试中有多个页面

[英]Nightmarejs multiple pages in same test

我正试图从Drupal网站上的两个网页中提取标题标签文本。 我想用Nightmarejs。

到目前为止,这是我的代码:

// get the <title> text from inside the Drupal site
var Nightmare = require('nightmare');
var user = 'foobar@example.com';
var pass = 'foobar';

new Nightmare()
  .goto('http://example.com/user')
    .type('#edit-name', user)
    .type('#edit-pass', pass)
    .click('.searchsubmit')
    .wait()
    .evaluate(function () {
       return document.getElementsByTagName("TITLE")[0];
       }, function (res) {
      console.log('Homepage title: '+res.text);
    })
    .run(function(err, nightmare){
      console.log('Done1.');

      // step 2
      nightmare
        .goto('http://example.com/admin')
        .wait()
        .evaluate(function () {
          return document.getElementsByTagName("TITLE")[0];
          }, function (res) {
         console.log('Admin page title: '+res.text);
        })   
        .run(function(err, nightmare){
          console.log('Done2.');
        })
      ;
    })
 ;

当我运行它时,使用: node app.js我能够成功登录到第一页。 不幸的是,当我尝试打开第二页时,我看到第二页呼叫拒绝访问( http://example.com/admin )。 会话未被带入第二个“goto”命令。

如果能够使用相同的nightmarejs会话打开许多页面,我该怎么办?

你试过链接goto方法吗?

 new Nightmare()
  .goto('http://example.com/user')
    .type('#edit-name', user)
    .type('#edit-pass', pass)
    .click('.searchsubmit')
    .wait()
    .evaluate(function () {
       return document.getElementsByTagName("TITLE")[0];
       }, function (res) {
      console.log('Homepage title: '+res.text);
    })
    .goto('http://example.com/admin')
        .wait()
        .evaluate(function () {
          return document.getElementsByTagName("TITLE")[0];
          }, function (res) {
         console.log('Admin page title: '+res.text);
        })   
        .run(function(err, nightmare){
          console.log('Done2.');
        })
      ;
    }).run();

通过阅读api文档, run只执行前面的命令。

经过一些测试,我发现似乎goto()应该只使用一次。 为了切换到新页面,我使用click()而不是额外的goto()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM