【问题标题】:Nightmare JS Return Page Status CodeNightmare JS 返回页面状态码
【发布时间】:2016-07-22 19:09:21
【问题描述】:

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

【问题讨论】:

    标签: javascript nightmare


    【解决方案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);
            });
    
    });
    

    【讨论】:

      【解决方案2】:

      .goto() Promise 解析包含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();
              });
          });
        });
      });
      

      【讨论】:

        猜你喜欢
        • 2019-07-15
        • 1970-01-01
        • 1970-01-01
        • 2021-06-09
        • 2014-01-02
        • 1970-01-01
        • 2011-03-16
        • 2018-10-11
        • 2011-11-22
        相关资源
        最近更新 更多