playwright-network-metrics 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +102 -0
- package/dist/index.d.mts +133 -0
- package/dist/index.d.ts +133 -0
- package/dist/index.js +692 -0
- package/dist/index.mjs +654 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Denis Bratchikov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# playwright-network-metrics
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/playwright-network-metrics)
|
|
4
|
+
|
|
5
|
+
A Playwright instrumentation package that collects and aggregates network performance metrics across your entire test run.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Native Timings**: Uses standard Playwright `request.timing()` for accurate measurement.
|
|
10
|
+
- **Interactive HTML Report**: Visualizes network performance with charts and detailed tables.
|
|
11
|
+
- **Granular Aggregation**: Groups by normalized URL, exact URL, resource type, and custom route groups.
|
|
12
|
+
- **Low Overhead**: Captures only necessary metadata, avoiding request/response body storage.
|
|
13
|
+
- **Flexible Filtering**: Use glob patterns to focus on specific APIs or resource types.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install --save-dev playwright-network-metrics
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### 1. Add the Reporter
|
|
24
|
+
|
|
25
|
+
Update your `playwright.config.ts` to include the `NetworkMetricsReporter`. This "plugin" entry point will aggregate metrics from all workers and generate the final report.
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { defineConfig } from "@playwright/test";
|
|
29
|
+
|
|
30
|
+
export default defineConfig({
|
|
31
|
+
reporter: [
|
|
32
|
+
["list"],
|
|
33
|
+
[
|
|
34
|
+
"playwright-network-metrics",
|
|
35
|
+
{
|
|
36
|
+
html: true, // Generate an interactive HTML report
|
|
37
|
+
outDir: "network-metrics-report",
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
],
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 2. Collect Metrics in Tests
|
|
45
|
+
|
|
46
|
+
Extend your Playwright `test` with the `networkMetrics` fixture. It is **automatic** by default.
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { test as base } from "@playwright/test";
|
|
50
|
+
import { defineNetworkMetricsFixture } from "playwright-network-metrics";
|
|
51
|
+
|
|
52
|
+
// Extend the test with our fixture
|
|
53
|
+
export const test = base.extend({
|
|
54
|
+
networkMetrics: defineNetworkMetricsFixture({
|
|
55
|
+
urlMatch: "**/api/**",
|
|
56
|
+
}),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Use it in your tests
|
|
60
|
+
test("my performance test", async ({ page }) => {
|
|
61
|
+
await page.goto("/dashboard");
|
|
62
|
+
// metrics are automatically collected and attached to the report
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Configuration
|
|
67
|
+
|
|
68
|
+
The `defineNetworkMetricsFixture` function and `NetworkMetricsReporter` accept a configuration object:
|
|
69
|
+
|
|
70
|
+
| Option | Type | Default | Description |
|
|
71
|
+
| ------------------- | -------------------- | ------------------------------------- | ---------------------------------------------------- |
|
|
72
|
+
| `outDir` | `string` | `"playwright-report/network-metrics"` | Directory where the reports will be written. |
|
|
73
|
+
| `html` | `boolean` | `false` | Whether to generate an interactive HTML report. |
|
|
74
|
+
| `urlMatch` | `string \| string[]` | `"**"` | Glob pattern(s) to match URLs (e.g., `**/api/**`). |
|
|
75
|
+
| `resourceTypes` | `string[]` | `[...]` | List of resource types to track. |
|
|
76
|
+
| `redactQueryParams` | `string[]` | `[]` | List of query parameters to redact from stored URLs. |
|
|
77
|
+
| `redactUrl` | `function` | `(url) => url` | Custom hook to modify the URL before storage. |
|
|
78
|
+
| `routeGroupRules` | `array` | `[]` | Rules for grouping URLs into logical categories. |
|
|
79
|
+
| `routeGroupFn` | `function` | `undefined` | Custom hook for route grouping. |
|
|
80
|
+
|
|
81
|
+
### Custom Route Grouping
|
|
82
|
+
|
|
83
|
+
You can provide custom rules to group similar URLs together:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
{
|
|
87
|
+
routeGroupRules: [
|
|
88
|
+
{ match: "**/api/v1/users/*", group: "/api/users/:id" },
|
|
89
|
+
{ match: "**/subscriptions/**", group: "Subscriptions" },
|
|
90
|
+
],
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Performance Notes
|
|
95
|
+
|
|
96
|
+
- **Low Overhead**: Does not store request/response bodies.
|
|
97
|
+
- **Memory Efficient**: Uses incremental aggregation and caps memory for percentile calculation.
|
|
98
|
+
- **Atomic Writing**: Writes reports only once at the end of the entire test run.
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Request, Fixtures } from '@playwright/test';
|
|
2
|
+
import { Reporter, TestCase, TestResult } from '@playwright/test/reporter';
|
|
3
|
+
|
|
4
|
+
declare const RESOURCE_TYPES: string[];
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for the network metrics collector.
|
|
8
|
+
*/
|
|
9
|
+
interface NetworkMetricsConfig {
|
|
10
|
+
/**
|
|
11
|
+
* Filter requests by URL pattern using glob strings or an array of glob strings.
|
|
12
|
+
* Wildcards like `** /api/ **` are supported.
|
|
13
|
+
* @default "**"
|
|
14
|
+
* @see https://www.npmjs.com/package/micromatch
|
|
15
|
+
*/
|
|
16
|
+
urlMatch?: string | string[];
|
|
17
|
+
/**
|
|
18
|
+
* List of Playwright resource types to track.
|
|
19
|
+
* @default ["fetch", "xhr", "document", "script", "style", "image", "font", "other"]
|
|
20
|
+
*/
|
|
21
|
+
resourceTypes?: string[];
|
|
22
|
+
/**
|
|
23
|
+
* Custom hook to determine if a specific request should be tracked.
|
|
24
|
+
* Returning false will skip this request.
|
|
25
|
+
*/
|
|
26
|
+
shouldTrackRequest?: (request: Request) => boolean;
|
|
27
|
+
/**
|
|
28
|
+
* List of query parameter names to redact from the stored URLs.
|
|
29
|
+
* The values will be replaced with "[REDACTED]".
|
|
30
|
+
* @default []
|
|
31
|
+
*/
|
|
32
|
+
redactQueryParams?: string[];
|
|
33
|
+
/**
|
|
34
|
+
* Custom hook to redact or modify the entire URL string before it is stored.
|
|
35
|
+
* This is called before redactQueryParams.
|
|
36
|
+
* @default (url) => url
|
|
37
|
+
*/
|
|
38
|
+
redactUrl?: (url: string) => string;
|
|
39
|
+
/**
|
|
40
|
+
* Rules for grouping URLs into logical categories (e.g., "/api/users/:id").
|
|
41
|
+
* Supports glob patterns for matching.
|
|
42
|
+
*/
|
|
43
|
+
routeGroupRules?: Array<{
|
|
44
|
+
/**
|
|
45
|
+
* Glob pattern or array of patterns to match the URL against.
|
|
46
|
+
*/
|
|
47
|
+
match: string | string[];
|
|
48
|
+
/**
|
|
49
|
+
* The name of the group to assign if matched.
|
|
50
|
+
*/
|
|
51
|
+
group: string;
|
|
52
|
+
}>;
|
|
53
|
+
/**
|
|
54
|
+
* Custom hook for route grouping. Takes precedence over routeGroupRules.
|
|
55
|
+
* Should return the group name or undefined to use default rules.
|
|
56
|
+
*/
|
|
57
|
+
routeGroupFn?: (url: string) => string | undefined;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Configuration for the Playwright reporter plugin.
|
|
61
|
+
*/
|
|
62
|
+
interface NetworkMetricsReporterConfig {
|
|
63
|
+
/**
|
|
64
|
+
* Directory where the metrics reports (JSON/HTML) will be saved.
|
|
65
|
+
* @default "playwright-report/network-metrics"
|
|
66
|
+
*/
|
|
67
|
+
outDir?: string;
|
|
68
|
+
/**
|
|
69
|
+
* Whether to generate an interactive HTML report.
|
|
70
|
+
* @default false
|
|
71
|
+
*/
|
|
72
|
+
html?: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Returns a Playwright fixture tuple with automatic instrumentation.
|
|
77
|
+
*
|
|
78
|
+
* When used as a fixture, it automatically attaches to the current page,
|
|
79
|
+
* collects network metrics throughout the test, and attaches the results
|
|
80
|
+
* as a JSON file named "network-metrics" to the test report upon completion.
|
|
81
|
+
*
|
|
82
|
+
* @param config Optional configuration for the collector (filtering, redaction, grouping).
|
|
83
|
+
* @returns A Playwright fixture tuple containing the collector and setup/teardown logic.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* import { test as base } from '@playwright/test';
|
|
88
|
+
* import { defineNetworkMetricsFixture } from 'playwright-network-metrics';
|
|
89
|
+
*
|
|
90
|
+
* export const test = base.extend({
|
|
91
|
+
* networkMetrics: defineNetworkMetricsFixture({
|
|
92
|
+
* urlMatch: "**" + "/api/" + "**",
|
|
93
|
+
* })
|
|
94
|
+
* });
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
declare const defineNetworkMetricsFixture: (config?: NetworkMetricsConfig) => Fixtures;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* A Playwright Reporter that aggregates network metrics from all test runs and generates reports.
|
|
101
|
+
*
|
|
102
|
+
* It collects metrics from worker processes via test attachments and produces
|
|
103
|
+
* JSON and optional HTML summaries.
|
|
104
|
+
*/
|
|
105
|
+
declare class NetworkMetricsReporter implements Reporter {
|
|
106
|
+
/**
|
|
107
|
+
* Internal store for all collected request metrics from all tests.
|
|
108
|
+
*/
|
|
109
|
+
private allMetrics;
|
|
110
|
+
private config;
|
|
111
|
+
/**
|
|
112
|
+
* Initializes the reporter with configuration for output and format.
|
|
113
|
+
*
|
|
114
|
+
* @param config Configuration for output directory and HTML report generation.
|
|
115
|
+
*/
|
|
116
|
+
constructor(config?: NetworkMetricsReporterConfig);
|
|
117
|
+
/**
|
|
118
|
+
* Playwright lifecycle hook called after each test finishes.
|
|
119
|
+
* Extracts "network-metrics" attachments and stores them for end-of-run aggregation.
|
|
120
|
+
*/
|
|
121
|
+
onTestEnd(test: TestCase, result: TestResult): void;
|
|
122
|
+
/**
|
|
123
|
+
* Playwright lifecycle hook called after all tests have finished.
|
|
124
|
+
* Aggregates all collected metrics and writes the final reports to disk.
|
|
125
|
+
*/
|
|
126
|
+
onEnd(): Promise<void>;
|
|
127
|
+
/**
|
|
128
|
+
* Internal helper to trigger HTML report generation.
|
|
129
|
+
*/
|
|
130
|
+
private generateHtml;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export { RESOURCE_TYPES, NetworkMetricsReporter as default, defineNetworkMetricsFixture };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Request, Fixtures } from '@playwright/test';
|
|
2
|
+
import { Reporter, TestCase, TestResult } from '@playwright/test/reporter';
|
|
3
|
+
|
|
4
|
+
declare const RESOURCE_TYPES: string[];
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for the network metrics collector.
|
|
8
|
+
*/
|
|
9
|
+
interface NetworkMetricsConfig {
|
|
10
|
+
/**
|
|
11
|
+
* Filter requests by URL pattern using glob strings or an array of glob strings.
|
|
12
|
+
* Wildcards like `** /api/ **` are supported.
|
|
13
|
+
* @default "**"
|
|
14
|
+
* @see https://www.npmjs.com/package/micromatch
|
|
15
|
+
*/
|
|
16
|
+
urlMatch?: string | string[];
|
|
17
|
+
/**
|
|
18
|
+
* List of Playwright resource types to track.
|
|
19
|
+
* @default ["fetch", "xhr", "document", "script", "style", "image", "font", "other"]
|
|
20
|
+
*/
|
|
21
|
+
resourceTypes?: string[];
|
|
22
|
+
/**
|
|
23
|
+
* Custom hook to determine if a specific request should be tracked.
|
|
24
|
+
* Returning false will skip this request.
|
|
25
|
+
*/
|
|
26
|
+
shouldTrackRequest?: (request: Request) => boolean;
|
|
27
|
+
/**
|
|
28
|
+
* List of query parameter names to redact from the stored URLs.
|
|
29
|
+
* The values will be replaced with "[REDACTED]".
|
|
30
|
+
* @default []
|
|
31
|
+
*/
|
|
32
|
+
redactQueryParams?: string[];
|
|
33
|
+
/**
|
|
34
|
+
* Custom hook to redact or modify the entire URL string before it is stored.
|
|
35
|
+
* This is called before redactQueryParams.
|
|
36
|
+
* @default (url) => url
|
|
37
|
+
*/
|
|
38
|
+
redactUrl?: (url: string) => string;
|
|
39
|
+
/**
|
|
40
|
+
* Rules for grouping URLs into logical categories (e.g., "/api/users/:id").
|
|
41
|
+
* Supports glob patterns for matching.
|
|
42
|
+
*/
|
|
43
|
+
routeGroupRules?: Array<{
|
|
44
|
+
/**
|
|
45
|
+
* Glob pattern or array of patterns to match the URL against.
|
|
46
|
+
*/
|
|
47
|
+
match: string | string[];
|
|
48
|
+
/**
|
|
49
|
+
* The name of the group to assign if matched.
|
|
50
|
+
*/
|
|
51
|
+
group: string;
|
|
52
|
+
}>;
|
|
53
|
+
/**
|
|
54
|
+
* Custom hook for route grouping. Takes precedence over routeGroupRules.
|
|
55
|
+
* Should return the group name or undefined to use default rules.
|
|
56
|
+
*/
|
|
57
|
+
routeGroupFn?: (url: string) => string | undefined;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Configuration for the Playwright reporter plugin.
|
|
61
|
+
*/
|
|
62
|
+
interface NetworkMetricsReporterConfig {
|
|
63
|
+
/**
|
|
64
|
+
* Directory where the metrics reports (JSON/HTML) will be saved.
|
|
65
|
+
* @default "playwright-report/network-metrics"
|
|
66
|
+
*/
|
|
67
|
+
outDir?: string;
|
|
68
|
+
/**
|
|
69
|
+
* Whether to generate an interactive HTML report.
|
|
70
|
+
* @default false
|
|
71
|
+
*/
|
|
72
|
+
html?: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Returns a Playwright fixture tuple with automatic instrumentation.
|
|
77
|
+
*
|
|
78
|
+
* When used as a fixture, it automatically attaches to the current page,
|
|
79
|
+
* collects network metrics throughout the test, and attaches the results
|
|
80
|
+
* as a JSON file named "network-metrics" to the test report upon completion.
|
|
81
|
+
*
|
|
82
|
+
* @param config Optional configuration for the collector (filtering, redaction, grouping).
|
|
83
|
+
* @returns A Playwright fixture tuple containing the collector and setup/teardown logic.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* import { test as base } from '@playwright/test';
|
|
88
|
+
* import { defineNetworkMetricsFixture } from 'playwright-network-metrics';
|
|
89
|
+
*
|
|
90
|
+
* export const test = base.extend({
|
|
91
|
+
* networkMetrics: defineNetworkMetricsFixture({
|
|
92
|
+
* urlMatch: "**" + "/api/" + "**",
|
|
93
|
+
* })
|
|
94
|
+
* });
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
declare const defineNetworkMetricsFixture: (config?: NetworkMetricsConfig) => Fixtures;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* A Playwright Reporter that aggregates network metrics from all test runs and generates reports.
|
|
101
|
+
*
|
|
102
|
+
* It collects metrics from worker processes via test attachments and produces
|
|
103
|
+
* JSON and optional HTML summaries.
|
|
104
|
+
*/
|
|
105
|
+
declare class NetworkMetricsReporter implements Reporter {
|
|
106
|
+
/**
|
|
107
|
+
* Internal store for all collected request metrics from all tests.
|
|
108
|
+
*/
|
|
109
|
+
private allMetrics;
|
|
110
|
+
private config;
|
|
111
|
+
/**
|
|
112
|
+
* Initializes the reporter with configuration for output and format.
|
|
113
|
+
*
|
|
114
|
+
* @param config Configuration for output directory and HTML report generation.
|
|
115
|
+
*/
|
|
116
|
+
constructor(config?: NetworkMetricsReporterConfig);
|
|
117
|
+
/**
|
|
118
|
+
* Playwright lifecycle hook called after each test finishes.
|
|
119
|
+
* Extracts "network-metrics" attachments and stores them for end-of-run aggregation.
|
|
120
|
+
*/
|
|
121
|
+
onTestEnd(test: TestCase, result: TestResult): void;
|
|
122
|
+
/**
|
|
123
|
+
* Playwright lifecycle hook called after all tests have finished.
|
|
124
|
+
* Aggregates all collected metrics and writes the final reports to disk.
|
|
125
|
+
*/
|
|
126
|
+
onEnd(): Promise<void>;
|
|
127
|
+
/**
|
|
128
|
+
* Internal helper to trigger HTML report generation.
|
|
129
|
+
*/
|
|
130
|
+
private generateHtml;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export { RESOURCE_TYPES, NetworkMetricsReporter as default, defineNetworkMetricsFixture };
|