簡體   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