Skip to content

Commit 693bf64

Browse files
committed
readme
1 parent 4ad9105 commit 693bf64

File tree

1 file changed

+48
-31
lines changed

1 file changed

+48
-31
lines changed

README.md

+48-31
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ Speed up [Playwright](https://playwright.dev/) tests by caching network requests
88

99
#### ✨ Features
1010

11-
- Automatically cache network requests during test execution
12-
- Save responses to the filesystem in a clear, organized structure
13-
- Modify cached responses dynamically during runtime
14-
- Reuse cached data across multiple test runs
15-
- View response bodies in a pretty formatted JSON
16-
- No need for manual mocks management
17-
- No mess with the HAR format — see [motivation](#motivation)
11+
- Automatically cache network requests during test execution
12+
- Save responses to the filesystem in a clear, organized structure
13+
- Modify cached responses dynamically during runtime
14+
- Reuse cached data across multiple test runs
15+
- Configure TTL to automatically refresh the cache and keep responses up-to-date
16+
- View response bodies in a pretty formatted JSON
17+
- No need for manual mocks management
18+
- No mess with the HAR format — see [motivation](#motivation)
1819

1920
Example of cache structure:
2021
```
@@ -91,7 +92,7 @@ test('test', async ({ page, cacheRoute }) => {
9192
});
9293
```
9394

94-
On the first run this test will store the response on the filesystem:
95+
On the first run, the test will hit real API and store the response on the filesystem:
9596
```
9697
.network-cache
9798
└── example.com
@@ -100,42 +101,39 @@ On the first run this test will store the response on the filesystem:
100101
├── headers.json
101102
└── body.json
102103
```
103-
All subsequent test runs will re-use cached response and execute much faster. To invalidate that cache, delete files or provide special [options](#options).
104+
All subsequent test runs will re-use cached response and execute much faster. You can invalidate that cache by manually deleting the files. Or provide `ttlMinutes` option to hit real API once in some period of time.
104105

105-
Default template for cache path is:
106-
```
107-
{baseDir}/{hostname}/{pathname}/{httpMethod}/{extraDir}/{httpStatus}
108-
```
106+
You can call `cacheRoute.GET|POST|PUT|PATCH|DELETE|ALL` to cache routes with respective HTTP method. [Url pattern](https://playwright.dev/docs/api/class-page#page-route-option-url) can contain `*` or `**` to match url segments and query params.
109107

110-
You can call `cacheRoute.GET|POST|PUT|PATCH|DELETE|ALL` to cache routes with respective HTTP method.
111-
112-
To catch requests to own APIs, you can omit hostname in [url pattern](https://playwright.dev/docs/api/class-page#page-route-option-url):
108+
To catch requests targeting your own app APIs, you can omit hostname in url:
113109
```ts
114110
test('test', async ({ page, cacheRoute }) => {
115111
await cacheRoute.GET('/api/cats*');
116112
// ...
117113
});
118114
```
119115

120-
See more examples below or check [API](#api).
116+
Default cache path is:
117+
```
118+
{baseDir}/{hostname}/{pathname}/{httpMethod}/{extraDir}/{httpStatus}
119+
```
120+
121+
See more examples below or check [configuration options](#options).
121122

122123
## Examples
123124

124-
### Cache request for all tests
125+
### Invalidate cache once in a hour
125126

126127
<details>
127128
<summary>Click to expand</summary>
128129

129-
You can share cached response across all tests.
130-
For that, define `cacheRoute` as an **auto** fixture and setup cached routes.
131-
For example, to share cached response of GET `/api/cats`:
130+
To keep response data up-to-date, you can automatically invalidate cache after configured time period. Set `ttlMinutes` option to desired value in minutes:
132131
```ts
133-
export const test = base.extend<{ cacheRoute: CacheRoute }>({
134-
cacheRoute: [async ({ page }, use) => {
135-
const cacheRoute = new CacheRoute(page);
136-
await cacheRoute.GET('/api/cats');
137-
await use(cacheRoute);
138-
}, { auto: true }]
132+
test('test', async ({ page, cacheRoute }) => {
133+
await cacheRoute.GET('/api/cats', {
134+
ttlMinutes: 60 // hit real API once in a hour
135+
});
136+
// ...
139137
});
140138
```
141139
</details>
@@ -227,6 +225,24 @@ export const test = base.extend<{ cacheRoute: CacheRoute }>({
227225
```
228226
</details>
229227

228+
### Auto-cache request for all tests
229+
230+
<details>
231+
<summary>Click to expand</summary>
232+
233+
You can setup caching of some request for all tests. To achieve that, define `cacheRoute` as **auto** fixture and setup cached routes.
234+
For example:
235+
```ts
236+
export const test = base.extend<{ cacheRoute: CacheRoute }>({
237+
cacheRoute: [async ({ page }, use) => {
238+
const cacheRoute = new CacheRoute(page);
239+
await cacheRoute.GET('/api/cats');
240+
await use(cacheRoute);
241+
}, { auto: true }]
242+
});
243+
```
244+
</details>
245+
230246
### Additional match by HTTP status
231247

232248
<details>
@@ -280,12 +296,13 @@ test('test', async ({ page, cacheRoute }) => {
280296
281297
</details>
282298

283-
### Separation of cache files
299+
### Isolate cache for particular test
284300

285301
<details>
286302
<summary>Click to expand</summary>
287303

288-
You may want to store cache files separately for a particular test. For that, you can utilize `cacheRoute.options.extraDir` - an array of extra directories to be inserted into the cache path. You can freely transform that array during the test.
304+
By default, cache files are stored in a shared directory and re-used across tests.
305+
You may want to isolate cache files for a particular test. For that, utilize `cacheRoute.options.extraDir` - an array of extra directories to be inserted into the cache path. You can freely transform that array during the test.
289306

290307
```ts
291308
test('test', async ({ page, cacheRoute }) => {
@@ -305,7 +322,7 @@ Generated cache structure:
305322
└── body.json
306323
```
307324

308-
To store cache files in a separate directory for **all tests**,
325+
To automatically store cache files in a separate directories for **each test**,
309326
you can set `extraDir` option in a fixture setup:
310327
```ts
311328
export const test = base.extend<{ cacheRoute: CacheRoute }>({
@@ -337,7 +354,7 @@ the generated structure is:
337354
<details>
338355
<summary>Click to expand</summary>
339356

340-
By default, cache files are stored in `.network-cache` base directory. To change this location, use `baseDir` option:
357+
By default, cache files are stored in `.network-cache` base directory. To change this location, set `baseDir` option:
341358

342359
```ts
343360
export const test = base.extend<{ cacheRoute: CacheRoute }>({

0 commit comments

Comments
 (0)