Skip to content

Commit c5f3d3d

Browse files
authored
feat(playwright): support HAR replaying (#3990)
1 parent e67c0c0 commit c5f3d3d

File tree

4 files changed

+631
-23
lines changed

4 files changed

+631
-23
lines changed

docs/helpers/Playwright.md

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,22 @@ Type: [object][5]
4646
- `url` **[string][8]?** base url of website to be tested
4747
- `browser` **(`"chromium"` | `"firefox"` | `"webkit"` | `"electron"`)?** a browser to test on, either: `chromium`, `firefox`, `webkit`, `electron`. Default: chromium.
4848
- `show` **[boolean][25]?** show browser window.
49-
- `restart` **([string][8] | [boolean][25])?** restart strategy between tests. Possible values:- 'context' or **false** - restarts [browser context][42] but keeps running browser. Recommended by Playwright team to keep tests isolated.
49+
- `restart` **([string][8] | [boolean][25])?** restart strategy between tests. Possible values:- 'context' or **false** - restarts [browser context][43] but keeps running browser. Recommended by Playwright team to keep tests isolated.
5050
- 'browser' or **true** - closes browser and opens it again between tests.
5151
- 'session' or 'keep' - keeps browser context and session, but cleans up cookies and localStorage between tests. The fastest option when running tests in windowed mode. Works with `keepCookies` and `keepBrowserState` options. This behavior was default before CodeceptJS 3.1
52-
- `timeout` **[number][19]?** - [timeout][43] in ms of all Playwright actions .
52+
- `timeout` **[number][19]?** - [timeout][44] in ms of all Playwright actions .
5353
- `disableScreenshots` **[boolean][25]?** don't save screenshot on failure.
5454
- `emulate` **any?** browser in device emulation mode.
5555
- `video` **[boolean][25]?** enables video recording for failed tests; videos are saved into `output/videos` folder
5656
- `keepVideoForPassedTests` **[boolean][25]?** save videos for passed tests; videos are saved into `output/videos` folder
57-
- `trace` **[boolean][25]?** record [tracing information][44] with screenshots and snapshots.
57+
- `trace` **[boolean][25]?** record [tracing information][45] with screenshots and snapshots.
5858
- `keepTraceForPassedTests` **[boolean][25]?** save trace for passed tests.
5959
- `fullPageScreenshots` **[boolean][25]?** make full page screenshots on failure.
6060
- `uniqueScreenshotNames` **[boolean][25]?** option to prevent screenshot override if you have scenarios with the same name in different suites.
6161
- `keepBrowserState` **[boolean][25]?** keep browser state between tests when `restart` is set to 'session'.
6262
- `keepCookies` **[boolean][25]?** keep cookies between tests when `restart` is set to 'session'.
6363
- `waitForAction` **[number][19]?** how long to wait after click, doubleClick or PressKey actions in ms. Default: 100.
64-
- `waitForNavigation` **(`"load"` | `"domcontentloaded"` | `"commit"`)?** When to consider navigation succeeded. Possible options: `load`, `domcontentloaded`, `commit`. Choose one of those options is possible. See [Playwright API][40].
64+
- `waitForNavigation` **(`"load"` | `"domcontentloaded"` | `"commit"`)?** When to consider navigation succeeded. Possible options: `load`, `domcontentloaded`, `commit`. Choose one of those options is possible. See [Playwright API][41].
6565
- `pressKeyDelay` **[number][19]?** Delay between key presses in ms. Used when calling Playwrights page.type(...) in fillField/appendField
6666
- `getPageTimeout` **[number][19]?** config option to set maximum navigation time in milliseconds.
6767
- `waitForTimeout` **[number][19]?** default wait* timeout in ms. Default: 1000.
@@ -74,8 +74,8 @@ Type: [object][5]
7474
- `chromium` **[object][5]?** pass additional chromium options
7575
- `firefox` **[object][5]?** pass additional firefox options
7676
- `electron` **[object][5]?** (pass additional electron options
77-
- `channel` **any?** (While Playwright can operate against the stock Google Chrome and Microsoft Edge browsers available on the machine. In particular, current Playwright version will support Stable and Beta channels of these browsers. See [Google Chrome & Microsoft Edge][45].
78-
- `ignoreLog` **[Array][9]<[string][8]>?** An array with console message types that are not logged to debug log. Default value is `['warning', 'log']`. E.g. you can set `[]` to log all messages. See all possible [values][46].
77+
- `channel` **any?** (While Playwright can operate against the stock Google Chrome and Microsoft Edge browsers available on the machine. In particular, current Playwright version will support Stable and Beta channels of these browsers. See [Google Chrome & Microsoft Edge][46].
78+
- `ignoreLog` **[Array][9]<[string][8]>?** An array with console message types that are not logged to debug log. Default value is `['warning', 'log']`. E.g. you can set `[]` to log all messages. See all possible [values][47].
7979
- `ignoreHTTPSErrors` **[boolean][25]?** Allows access to untrustworthy pages, e.g. to a page with an expired certificate. Default value is `false`
8080
- `bypassCSP` **[boolean][25]?** bypass Content Security Policy or CSP
8181
- `highlightElement` **[boolean][25]?** highlight the interacting elements. Default: false. Note: only activate under verbose mode (--verbose).
@@ -1606,6 +1606,26 @@ I.refreshPage();
16061606

16071607
Returns **void** automatically synchronized promise through #recorder
16081608

1609+
### replayFromHar
1610+
1611+
Replaying from HAR
1612+
1613+
```js
1614+
// Replay API requests from HAR.
1615+
// Either use a matching response from the HAR,
1616+
// or abort the request if nothing matches.
1617+
I.replayFromHar('./output/har/something.har', { url: "*/**/api/v1/fruits" });
1618+
I.amOnPage('https://demo.playwright.dev/api-mocking');
1619+
I.see('CodeceptJS');
1620+
```
1621+
1622+
#### Parameters
1623+
1624+
- `harFilePath` **[string][8]** Path to recorded HAR file
1625+
- `opts` **[object][5]?** [Options for replaying from HAR][35]
1626+
1627+
Returns **any** Promise<void>
1628+
16091629
### resizeWindow
16101630

16111631
Resize the current window to provided width and height.
@@ -2241,7 +2261,7 @@ I.uncheckOption('agree', '//form');
22412261
- `context` **([string][8]? | [object][5])** (optional, `null` by default) element located by CSS | XPath | strict locator.
22422262
- `options`
22432263

2244-
Returns **void** automatically synchronized promise through #recorder[Additional options][35] for uncheck available as 3rd argument.Examples:```js
2264+
Returns **void** automatically synchronized promise through #recorder[Additional options][36] for uncheck available as 3rd argument.Examples:```js
22452265
// click on element at position
22462266
I.uncheckOption('Agree', '.signup', { position: { x: 5, y: 5 } })
22472267
```> ⚠️ To avoid flakiness, option `force: true` is set by default
@@ -2253,7 +2273,7 @@ Use Playwright API inside a test.
22532273
First argument is a description of an action.
22542274
Second argument is async function that gets this helper as parameter.
22552275

2256-
{ [`page`][36], [`browserContext`][37] [`browser`][38] } objects from Playwright API are available.
2276+
{ [`page`][37], [`browserContext`][38] [`browser`][39] } objects from Playwright API are available.
22572277

22582278
```js
22592279
I.usePlaywrightTo('emulate offline mode', async ({ browserContext }) => {
@@ -2386,7 +2406,7 @@ Returns **void** automatically synchronized promise through #recorder
23862406

23872407
Waits for navigation to finish. By default, it takes configured `waitForNavigation` option.
23882408

2389-
See [Playwright's reference][39]
2409+
See [Playwright's reference][40]
23902410

23912411
#### Parameters
23922412

@@ -2443,7 +2463,7 @@ Returns **void** automatically synchronized promise through #recorder
24432463

24442464
Waits for page navigates to a new URL or reloads. By default, it takes configured `waitForNavigation` option.
24452465

2446-
See [Playwright's reference][40]
2466+
See [Playwright's reference][41]
24472467

24482468
#### Parameters
24492469

@@ -2480,7 +2500,7 @@ I.waitForVisible('#popup');
24802500
- `locator` **([string][8] | [object][5])** element located by CSS|XPath|strict locator.
24812501
- `sec` **[number][19]** (optional, `1` by default) time in seconds to wait
24822502

2483-
Returns **void** automatically synchronized promise through #recorderThis method accepts [React selectors][41].
2503+
Returns **void** automatically synchronized promise through #recorderThis method accepts [React selectors][42].
24842504

24852505
### waitInUrl
24862506

@@ -2613,26 +2633,28 @@ Returns **void** automatically synchronized promise through #recorder
26132633

26142634
[34]: #click
26152635

2616-
[35]: https://playwright.dev/docs/api/class-elementhandle#element-handle-uncheck
2636+
[35]: https://playwright.dev/docs/api/class-page#page-route-from-har
2637+
2638+
[36]: https://playwright.dev/docs/api/class-elementhandle#element-handle-uncheck
26172639

2618-
[36]: https://github.com/microsoft/playwright/blob/main/docs/src/api/class-page.md
2640+
[37]: https://github.com/microsoft/playwright/blob/main/docs/src/api/class-page.md
26192641

2620-
[37]: https://github.com/microsoft/playwright/blob/main/docs/src/api/class-browsercontext.md
2642+
[38]: https://github.com/microsoft/playwright/blob/main/docs/src/api/class-browsercontext.md
26212643

2622-
[38]: https://github.com/microsoft/playwright/blob/main/docs/src/api/class-browser.md
2644+
[39]: https://github.com/microsoft/playwright/blob/main/docs/src/api/class-browser.md
26232645

2624-
[39]: https://playwright.dev/docs/api/class-page?_highlight=waitfornavi#pagewaitfornavigationoptions
2646+
[40]: https://playwright.dev/docs/api/class-page?_highlight=waitfornavi#pagewaitfornavigationoptions
26252647

2626-
[40]: https://playwright.dev/docs/api/class-page#page-wait-for-url
2648+
[41]: https://playwright.dev/docs/api/class-page#page-wait-for-url
26272649

2628-
[41]: https://codecept.io/react
2650+
[42]: https://codecept.io/react
26292651

2630-
[42]: https://playwright.dev/docs/api/class-browsercontext
2652+
[43]: https://playwright.dev/docs/api/class-browsercontext
26312653

2632-
[43]: https://playwright.dev/docs/api/class-page#page-set-default-timeout
2654+
[44]: https://playwright.dev/docs/api/class-page#page-set-default-timeout
26332655

2634-
[44]: https://playwright.dev/docs/trace-viewer
2656+
[45]: https://playwright.dev/docs/trace-viewer
26352657

2636-
[45]: https://playwright.dev/docs/browsers/#google-chrome--microsoft-edge
2658+
[46]: https://playwright.dev/docs/browsers/#google-chrome--microsoft-edge
26372659

2638-
[46]: https://playwright.dev/docs/api/class-consolemessage#console-message-type
2660+
[47]: https://playwright.dev/docs/api/class-consolemessage#console-message-type

lib/helper/Playwright.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,33 @@ class Playwright extends Helper {
10891089
return this.page.reload({ timeout: this.options.getPageTimeout, waitUntil: this.options.waitForNavigation });
10901090
}
10911091

1092+
/**
1093+
* Replaying from HAR
1094+
*
1095+
* ```js
1096+
* // Replay API requests from HAR.
1097+
* // Either use a matching response from the HAR,
1098+
* // or abort the request if nothing matches.
1099+
* I.replayFromHar('./output/har/something.har', { url: "*\/**\/api/v1/fruits" });
1100+
* I.amOnPage('https://demo.playwright.dev/api-mocking');
1101+
* I.see('CodeceptJS');
1102+
* ```
1103+
*
1104+
* @param {string} harFilePath Path to recorded HAR file
1105+
* @param {object} [opts] [Options for replaying from HAR](https://playwright.dev/docs/api/class-page#page-route-from-har)
1106+
*
1107+
* @returns Promise<void>
1108+
*/
1109+
async replayFromHar(harFilePath, opts) {
1110+
const file = path.join(global.codecept_dir, harFilePath);
1111+
1112+
if (!fileExists(file)) {
1113+
throw new Error(`File at ${file} cannot be found on local system`);
1114+
}
1115+
1116+
await this.page.routeFromHAR(harFilePath, opts);
1117+
}
1118+
10921119
/**
10931120
* {{> scrollPageToTop }}
10941121
*/

0 commit comments

Comments
 (0)