Selenium 使用nightwatch检查http状态代码

Selenium 使用nightwatch检查http状态代码,selenium,nightwatch.js,Selenium,Nightwatch.js,如何使用nightwatch.js检查HTTP状态代码?我试过了 browser.url(function (response) { browser.assert.equal(response.statusCode, 200); }); 但这当然不行。试试这个 var http = require("http"); module.exports = { "Check Response Code" : function (client) {

如何使用nightwatch.js检查HTTP状态代码?我试过了

  browser.url(function (response) {
     browser.assert.equal(response.statusCode, 200);
  });
但这当然不行。

试试这个

    var http = require("http");
    module.exports = {
      "Check Response Code" : function (client) {
          var request = http.request({
            host: "www.google.com",
            port: 80,
            path: "/images/srpr/logo11w.png",
            method: "HEAD"
          }, function (response) {
            client
            .assert.equal(response.statusCode, 200, 'Check status');
            client.end();
          }).on("error", function (err) {
            console.log(err);
            client.end();
          }).end();
         }
       };

实际上,还没有办法使用Selenium()获取页面的响应状态

但您可以轻松地执行以下操作:要求“请求”库,向您希望在Selenium测试中打开的网页发出请求,并验证响应状态代码是否等于200:

const request = require('request');

request('http://stackoverflow.com', (error, response, body) => {
    browser.assert.equal(response.statusCode, 200);
});

补充Hilarion Galushka的答案:您可以从nightwatch使用perform()命令将请求和断言集成到您的nightwatch测试中。

例如:

module.exports = {
    'test response code': function (browser) {
        browser.perform(done => {
            request('http://stackoverflow.com', function (error, response, body) {
                browser.assert.equal(response.statusCode, 200);
                done()
            });
        })
    }
}

可能重复的不是重复的,这个问题与夜视JS有关。这是最好的答案。