0

Ok, so I'm looking to test the https response code of a URL using Nightwatch.js.

I've install the npm 'request' package, and successfully tested the response of the URL under test using the following code;

var request = require('request');
  request('http://www.google.com', function (error, response, body) {
    if (response.statusCode != 200)
      console.log("error!!!")    
});

My problem is 'converting' this working (request) code into a standard nightwatch.js setup, so that if the response code is not a 200 then the test will fail?

At the moment my nightwatch.js scripts begin with, for example;

module.exports = { 
  'test response code': function (browser) {
    browser

and I would like to keep it in a similar format.

Any help would be greatly appreciated.

Darren Harley
  • 75
  • 1
  • 18
  • Possible duplicate of 'check http status code using nightwatch' - https://stackoverflow.com/questions/36142331/check-http-status-code-using-nightwatch? – Asen Arizanov Jan 14 '19 at 14:23

1 Answers1

2

You can use the perform() method from nightwatch: http://nightwatchjs.org/api/perform.html

In the perform block you can write every code you want, you can also youse every assertion library. A simple example, based on yours, could look like this:

var request = require('request');
var assert = require('assert')

module.exports = {
    'test response code': function (browser) {
        browser.perform(done => {
            request('http://www.google.com', function (error, response, body) {
                assert.ok(response.statusCode == 200)
                done()
            }) 
        })
    }
}
mibemerami
  • 61
  • 1
  • 2