diff --git a/lib/api/expect/assertions/headerTest.js b/lib/api/expect/assertions/headerTest.js new file mode 100644 index 0000000000..99a7d6c1d1 --- /dev/null +++ b/lib/api/expect/assertions/headerTest.js @@ -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(); + } +}; diff --git a/lib/api/protocol/url.js b/lib/api/protocol/url.js index 875914c969..51a560637b 100644 --- a/lib/api/protocol/url.js +++ b/lib/api/protocol/url.js @@ -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 */ @@ -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; @@ -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; @@ -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; + } };