简体   繁体   English

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

[英]Nightmarejs multiple pages in same test

I'm trying to pull the title-tag text from two webpages on a Drupal site. 我正试图从Drupal网站上的两个网页中提取标题标签文本。 I want to use Nightmarejs. 我想用Nightmarejs。

Here is my code so far: 到目前为止,这是我的代码:

// 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.');
        })
      ;
    })
 ;

When I run this, with: node app.js I am able to log in successfully to the first page. 当我运行它时,使用: node app.js我能够成功登录到第一页。 Unfortunately when I try to open the second page I see an access refused on the second page call ( http://example.com/admin ). 不幸的是,当我尝试打开第二页时,我看到第二页呼叫拒绝访问( http://example.com/admin )。 The session is not being carried into the second "goto" command. 会话未被带入第二个“goto”命令。

What can I do to be able to open up many pages with the same nightmarejs session? 如果能够使用相同的nightmarejs会话打开许多页面,我该怎么办?

Have you tried chaining the goto methods? 你试过链接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();

From reading the api docs run only executes the commands that came before it. 通过阅读api文档, run只执行前面的命令。

After some testing, I discovered that it seems goto() should be used only once. 经过一些测试,我发现似乎goto()应该只使用一次。 In order to switch to a new page, I use click() instead of an additional goto(). 为了切换到新页面,我使用click()而不是额外的goto()。

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

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