diff --git a/docs/helpers/Playwright.md b/docs/helpers/Playwright.md index 3ad1e44ed..99bf03983 100644 --- a/docs/helpers/Playwright.md +++ b/docs/helpers/Playwright.md @@ -1371,6 +1371,19 @@ let title = await I.grabTitle(); Returns **[Promise][22]<[string][9]>** title +### grabTrafficUrl + +Returns full URL of request matching parameter "urlMatch". + +#### Parameters + +- `urlMatch` **([string][9] | [RegExp][11])** Expected URL of request in network traffic. Can be a string or a regular expression.Examples:```js + I.grabTrafficUrl('https://api.example.com/session'); + I.grabTrafficUrl(/session.*start/); + ``` + +Returns **[Promise][22]<any>** + ### grabValueFrom Retrieves a value from a form element located by CSS or XPath and returns it to test. diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index 5e070a659..44c61ba9e 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -3102,6 +3102,39 @@ class Playwright extends Helper { stopRecordingTraffic.call(this); } + /** + * Returns full URL of request matching parameter "urlMatch". + * + * @param {string|RegExp} urlMatch Expected URL of request in network traffic. Can be a string or a regular expression. + * + * Examples: + * + * ```js + * I.grabTrafficUrl('https://api.example.com/session'); + * I.grabTrafficUrl(/session.*start/); + * ``` + * + * @return {Promise<*>} + */ + grabTrafficUrl(urlMatch) { + if (!this.recordedAtLeastOnce) { + throw new Error('Failure in test automation. You use "I.grabTrafficUrl", but "I.startRecordingTraffic" was never called before.'); + } + + for (const i in this.requests) { + // eslint-disable-next-line no-prototype-builtins + if (this.requests.hasOwnProperty(i)) { + const request = this.requests[i]; + + if (request.url && request.url.match(new RegExp(urlMatch))) { + return request.url; + } + } + } + + assert.fail(`Method "getTrafficUrl" failed: No request found in traffic that matches ${urlMatch}`); + } + /** * * {{> grabRecordedNetworkTraffics }}