Skip to content

Commit

Permalink
# issue-4250
Browse files Browse the repository at this point in the history
Getting HTTP headers of the url page response directly in a Nightwatch test nightwatchjs#4250

github- Harshit-7373
  • Loading branch information
Harshit-7373 committed Nov 17, 2024
1 parent 8f4a330 commit 69534e8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
28 changes: 28 additions & 0 deletions lib/api/expect/assertions/headerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
'Test if headers are captured correctly': function (browser) {
browser.url('https://example.com', {captureHeaders: true}, function (result) {
const headers = browser.getHeaders();

// Ensure headers object is available
if (!headers) {
console.error('Headers could not be retrieved!');
browser.assert.fail('Headers object is undefined or null');

return;
}

// Log headers for debugging purposes
console.log('Captured Headers:', headers); // eslint-disable-line no-console

// Assertions to validate specific headers
browser.assert.ok(headers['content-type'], 'Content-Type header is present');
browser.assert.equal(
headers['content-type'],
'text/html; charset=UTF-8',
'Content-Type header matches expected value'
);
});

browser.end();
}
};
52 changes: 38 additions & 14 deletions lib/api/protocol/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,26 @@ const ProtocolAction = require('./_base-action.js');
const ora = require('ora');

/**
* Retrieve the URL of the current page or navigate to a new URL.
* Retrieve the URL of the current page or navigate to a new URL, with an option to capture HTTP headers.
*
* @example
* module.exports = {
* 'demo Test' : function(browser) {
* browser.url(function(result) {
* // return the current url
* console.log(result);
* });
*
* // navigate to new url:
* browser.url('{URL}');
*
* // navigate to new url:
* browser.url('{URL}', function(result) {
* console.log(result);
* browser.url('{URL}', {captureHeaders: true}, function(result) {
* // access headers after page loads
* const headers = browser.getHeaders();
* console.log(headers);
* });
* }
* }
*
* @method url
* @link /#navigate-to
* @syntax .url([url], [callback])
* @syntax .url([url], [options], [callback])
* @syntax .url(callback)
* @param {string|function} [url] If missing, it will return the URL of the current page as an argument to the supplied callback.
* @param {Object} [options] Options to configure the behavior.
* @param {boolean} [options.captureHeaders] If `true`, captures HTTP response headers for the page.
* @param {Function} [callback]
* @api protocol.navigation
*/
Expand All @@ -35,7 +30,9 @@ module.exports = class Action extends ProtocolAction {
return true;
}

command(url, callback = function(r) {return r}) {
command(url, options = {}, callback = function(r) { return r }) {
const {captureHeaders} = options;

if (typeof url == 'string') {
const startTime = new Date();
let spinner;
Expand All @@ -47,6 +44,10 @@ module.exports = class Action extends ProtocolAction {
}).start();
}

if (captureHeaders) {
this.captureHeaders(url); // Capture headers if the flag is set
}

return this.transportActions.navigateTo(url).then(result => {
if (spinner) {
const ms = new Date() - startTime;
Expand All @@ -65,4 +66,27 @@ module.exports = class Action extends ProtocolAction {
return callback.call(this.api, result);
});
}

// New method to capture headers from the network request
captureHeaders(url) {
const client = this.api.client;

client.send('Network.enable'); // Enable the network domain
let responseHeaders = null;

// Listen for network response events to capture headers
client.on('Network.responseReceived', (params) => {
if (params.response.url === url) {
responseHeaders = params.response.headers;
}
});

// Store headers in the api object
this.api.capturedHeaders = responseHeaders;
}

// New method to retrieve captured headers
getHeaders() {
return this.api.capturedHeaders;
}
};

0 comments on commit 69534e8

Please sign in to comment.