2016-07-21 71 views
0

我想使用Nightmare JS通過檢查狀態碼200來確定頁面是否正在加載。我查看了goto選項,但一直未能弄清楚。有人有主意嗎?Nightmare JS返回頁面狀態碼

var Nightmare = require('nightmare'); 
var should = require('chai').should(); 

describe('PageLoad Test', function() { 
var url = 'http://www.yahoo.com'; 
    describe('Browse Page', function() { 
    it('should return 200 status', function (done) { 
     this.timeout(15000); 
     new Nightmare() 
      .goto(url) 
      .wait(1000) 
      .evaluate(function() { 
       return document.querySelector('div.items').innerHTML; 
      }) 
     .then(function (element) { 
      element.should.equal(element); 
      done(); 
     }) 
     .catch(function (error) { 
      console.error('page failed to load', error); 
      done('epic failure') 
     }) 
    }); 
    }); 
}); 

回答

0

.goto()無極決議中包含的信息,包括codeheadersurlreferrers

所以,如果你想檢查200狀態,你可以做這樣的事情:

var Nightmare = require('nightmare'); 
var should = require('chai').should(); 

describe('PageLoad Test', function() { 
    var url = 'http://www.yahoo.com'; 
    describe('Browse Page', function() { 
    it('should return 200 status', function (done) { 
     new Nightmare() 
     .goto(url) 
     .then(function (response) { 
      response.code.should.equal(200); 
      done(); 
     }); 
    }); 
    }); 
}); 
+0

這也行,謝謝! –

1

這工作讓我檢查200狀態。

var expect = require('chai').expect; 
    require('mocha-generators').install(); 
    var Nightmare = require('nightmare'); 
    var nightmare = Nightmare({ 
     show: false, 
     ignoreSslErrors: true, 
     webSecurity: false 
    }); 

    describe('NightmareJS', function() { 
     this.timeout(15000); 
     it('should not be a nightmare', function*() { 
      var status; 
      yield nightmare 
       .goto('http://www.google.de') 
       .end() 
       .then((gotoResult) => { 
        status = gotoResult.code; 
       }); 
      expect(status).to.equal(200); 
     }); 

}); 
+0

這很好,謝謝! –