0

我想使用 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')
        })
    });
  });
});
4

2 回答 2

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

});
于 2016-07-22T11:09:21.913 回答
0

.goto()Promise 解析包含的信息包括, code, headers,urlreferrers

因此,如果您想检查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();
        });
    });
  });
});
于 2016-07-21T20:22:42.000 回答