Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

# issue-4250 #4301

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
};