Nightmare JS返回页面状态代码

时间:2016-07-21 19:59:05

标签: javascript nightmare

我想使用Nightmare JS通过检查状态代码200来确定页面是否正在加载。我查看了goto选项,但一直无法弄明白。有人有什么想法吗?

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

2 个答案:

答案 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);
        });

});

答案 1 :(得分: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();
        });
    });
  });
});
相关问题