Skip to content

Commit c5a4af8

Browse files
authored
Merge branch 'main' into add-test-coverage
2 parents 163b78f + 2f448e8 commit c5a4af8

File tree

8 files changed

+76
-122
lines changed

8 files changed

+76
-122
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,8 @@ If you run into any issues, checkout our [troubleshooting guide](./docs/troubles
260260
- [`new_page`](docs/tool-reference.md#new_page)
261261
- [`select_page`](docs/tool-reference.md#select_page)
262262
- [`wait_for`](docs/tool-reference.md#wait_for)
263-
- **Emulation** (3 tools)
264-
- [`emulate_cpu`](docs/tool-reference.md#emulate_cpu)
265-
- [`emulate_network`](docs/tool-reference.md#emulate_network)
263+
- **Emulation** (2 tools)
264+
- [`emulate`](docs/tool-reference.md#emulate)
266265
- [`resize_page`](docs/tool-reference.md#resize_page)
267266
- **Performance** (3 tools)
268267
- [`performance_analyze_insight`](docs/tool-reference.md#performance_analyze_insight)

docs/tool-reference.md

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
- [`new_page`](#new_page)
1919
- [`select_page`](#select_page)
2020
- [`wait_for`](#wait_for)
21-
- **[Emulation](#emulation)** (3 tools)
22-
- [`emulate_cpu`](#emulate_cpu)
23-
- [`emulate_network`](#emulate_network)
21+
- **[Emulation](#emulation)** (2 tools)
22+
- [`emulate`](#emulate)
2423
- [`resize_page`](#resize_page)
2524
- **[Performance](#performance)** (3 tools)
2625
- [`performance_analyze_insight`](#performance_analyze_insight)
@@ -190,23 +189,14 @@
190189

191190
## Emulation
192191

193-
### `emulate_cpu`
192+
### `emulate`
194193

195-
**Description:** Emulates CPU throttling by slowing down the selected page's execution.
194+
**Description:** Emulates various features on the selected page.
196195

197196
**Parameters:**
198197

199-
- **throttlingRate** (number) **(required)**: The CPU throttling rate representing the slowdown factor 1-20x. Set the rate to 1 to disable throttling
200-
201-
---
202-
203-
### `emulate_network`
204-
205-
**Description:** Emulates network conditions such as throttling or offline mode on the selected page.
206-
207-
**Parameters:**
208-
209-
- **throttlingOption** (enum: "No emulation", "Offline", "Slow 3G", "Fast 3G", "Slow 4G", "Fast 4G") **(required)**: The network throttling option to emulate. Available throttling options are: No emulation, Offline, Slow 3G, Fast 3G, Slow 4G, Fast 4G. Set to "No emulation" to disable. Set to "Offline" to simulate offline network conditions.
198+
- **cpuThrottlingRate** (number) _(optional)_: Represents the CPU slowdown factor. Set the rate to 1 to disable throttling. If omitted, throttling remains unchanged.
199+
- **networkConditions** (enum: "No emulation", "Offline", "Slow 3G", "Fast 3G", "Slow 4G", "Fast 4G") _(optional)_: Throttle network. Set to "No emulation" to disable. If omitted, conditions remain unchanged.
210200

211201
---
212202

@@ -256,12 +246,11 @@
256246

257247
### `get_network_request`
258248

259-
**Description:** Gets a network request by reqid. You can get all requests by calling [`list_network_requests`](#list_network_requests).
260-
Get the request currently selected in the DevTools UI by ommitting reqid
249+
**Description:** Gets a network request by an optional reqid, if omitted returns the currently selected request in the DevTools Network panel.
261250

262251
**Parameters:**
263252

264-
- **reqid** (number) _(optional)_: The reqid of the network request. If omitted, looks up the current request selected in DevTools UI.
253+
- **reqid** (number) _(optional)_: The reqid of the network request. If omitted returns the currently selected request in the DevTools Network panel.
265254

266255
---
267256

scripts/prepare.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
import {rm} from 'node:fs/promises';
88
import {resolve} from 'node:path';
99

10-
import {sed} from './sed.ts';
11-
1210
const projectRoot = process.cwd();
1311

1412
const filesToRemove = [
1513
'node_modules/chrome-devtools-frontend/package.json',
1614
'node_modules/chrome-devtools-frontend/front_end/models/trace/lantern/testing',
1715
'node_modules/chrome-devtools-frontend/front_end/third_party/intl-messageformat/package/package.json',
18-
'node_modules/chrome-devtools-frontend/front_end/third_party/codemirror.next/codemirror.next.js',
1916
];
2017

2118
async function main() {
@@ -30,28 +27,6 @@ async function main() {
3027
process.exit(1);
3128
}
3229
}
33-
// TODO: remove once https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/7072054 is available.
34-
sed(
35-
'node_modules/chrome-devtools-frontend/front_end/core/sdk/NetworkManager.ts',
36-
`declare global {
37-
// TS typedefs are not up to date
38-
interface URLPattern {
39-
hash: string;
40-
hostname: string;
41-
password: string;
42-
pathname: string;
43-
port: string;
44-
protocol: string;
45-
search: string;
46-
username: string;
47-
hasRegExpGroups: boolean;
48-
test(url: string): boolean;
49-
}
50-
/* eslint-disable-next-line @typescript-eslint/naming-convention */
51-
var URLPattern: {prototype: URLPattern, new (input: string): URLPattern};
52-
}`,
53-
'',
54-
);
5530
console.log('Clean up of chrome-devtools-frontend complete.');
5631
}
5732

src/formatters/networkFormatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function getShortDescriptionForRequest(
1616
selectedInDevToolsUI = false,
1717
): string {
1818
// TODO truncate the URL
19-
return `reqid=${id} ${request.method()} ${request.url()} ${getStatusFromRequest(request)}${selectedInDevToolsUI ? ` [selected in DevTools UI]` : ''}`;
19+
return `reqid=${id} ${request.method()} ${request.url()} ${getStatusFromRequest(request)}${selectedInDevToolsUI ? ` [selected in the DevTools Network panel]` : ''}`;
2020
}
2121

2222
export function getStatusFromRequest(request: HTTPRequest): string {

src/tools/emulation.ts

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,73 +15,65 @@ const throttlingOptions: [string, ...string[]] = [
1515
...Object.keys(PredefinedNetworkConditions),
1616
];
1717

18-
export const emulateNetwork = defineTool({
19-
name: 'emulate_network',
20-
description: `Emulates network conditions such as throttling or offline mode on the selected page.`,
18+
export const emulate = defineTool({
19+
name: 'emulate',
20+
description: `Emulates various features on the selected page.`,
2121
annotations: {
2222
category: ToolCategory.EMULATION,
2323
readOnlyHint: false,
2424
},
2525
schema: {
26-
throttlingOption: zod
26+
networkConditions: zod
2727
.enum(throttlingOptions)
28+
.optional()
2829
.describe(
29-
`The network throttling option to emulate. Available throttling options are: ${throttlingOptions.join(', ')}. Set to "No emulation" to disable. Set to "Offline" to simulate offline network conditions.`,
30+
`Throttle network. Set to "No emulation" to disable. If omitted, conditions remain unchanged.`,
3031
),
31-
},
32-
handler: async (request, _response, context) => {
33-
const page = context.getSelectedPage();
34-
const conditions = request.params.throttlingOption;
35-
36-
if (conditions === 'No emulation') {
37-
await page.emulateNetworkConditions(null);
38-
context.setNetworkConditions(null);
39-
return;
40-
}
41-
42-
if (conditions === 'Offline') {
43-
await page.emulateNetworkConditions({
44-
offline: true,
45-
download: 0,
46-
upload: 0,
47-
latency: 0,
48-
});
49-
context.setNetworkConditions('Offline');
50-
return;
51-
}
52-
53-
if (conditions in PredefinedNetworkConditions) {
54-
const networkCondition =
55-
PredefinedNetworkConditions[
56-
conditions as keyof typeof PredefinedNetworkConditions
57-
];
58-
await page.emulateNetworkConditions(networkCondition);
59-
context.setNetworkConditions(conditions);
60-
}
61-
},
62-
});
63-
64-
export const emulateCpu = defineTool({
65-
name: 'emulate_cpu',
66-
description: `Emulates CPU throttling by slowing down the selected page's execution.`,
67-
annotations: {
68-
category: ToolCategory.EMULATION,
69-
readOnlyHint: false,
70-
},
71-
schema: {
72-
throttlingRate: zod
32+
cpuThrottlingRate: zod
7333
.number()
7434
.min(1)
7535
.max(20)
36+
.optional()
7637
.describe(
77-
'The CPU throttling rate representing the slowdown factor 1-20x. Set the rate to 1 to disable throttling',
38+
'Represents the CPU slowdown factor. Set the rate to 1 to disable throttling. If omitted, throttling remains unchanged.',
7839
),
7940
},
8041
handler: async (request, _response, context) => {
8142
const page = context.getSelectedPage();
82-
const {throttlingRate} = request.params;
43+
const networkConditions = request.params.networkConditions;
44+
const cpuThrottlingRate = request.params.cpuThrottlingRate;
8345

84-
await page.emulateCPUThrottling(throttlingRate);
85-
context.setCpuThrottlingRate(throttlingRate);
46+
if (networkConditions) {
47+
if (networkConditions === 'No emulation') {
48+
await page.emulateNetworkConditions(null);
49+
context.setNetworkConditions(null);
50+
return;
51+
}
52+
53+
if (networkConditions === 'Offline') {
54+
await page.emulateNetworkConditions({
55+
offline: true,
56+
download: 0,
57+
upload: 0,
58+
latency: 0,
59+
});
60+
context.setNetworkConditions('Offline');
61+
return;
62+
}
63+
64+
if (networkConditions in PredefinedNetworkConditions) {
65+
const networkCondition =
66+
PredefinedNetworkConditions[
67+
networkConditions as keyof typeof PredefinedNetworkConditions
68+
];
69+
await page.emulateNetworkConditions(networkCondition);
70+
context.setNetworkConditions(networkConditions);
71+
}
72+
}
73+
74+
if (cpuThrottlingRate) {
75+
await page.emulateCPUThrottling(cpuThrottlingRate);
76+
context.setCpuThrottlingRate(cpuThrottlingRate);
77+
}
8678
},
8779
});

src/tools/network.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ export const listNetworkRequests = defineTool({
8484

8585
export const getNetworkRequest = defineTool({
8686
name: 'get_network_request',
87-
description: `Gets a network request by reqid. You can get all requests by calling ${listNetworkRequests.name}.
88-
Get the request currently selected in the DevTools UI by ommitting reqid`,
87+
description: `Gets a network request by an optional reqid, if omitted returns the currently selected request in the DevTools Network panel.`,
8988
annotations: {
9089
category: ToolCategory.NETWORK,
9190
readOnlyHint: true,
@@ -95,7 +94,7 @@ Get the request currently selected in the DevTools UI by ommitting reqid`,
9594
.number()
9695
.optional()
9796
.describe(
98-
'The reqid of the network request. If omitted, looks up the current request selected in DevTools UI.',
97+
'The reqid of the network request. If omitted returns the currently selected request in the DevTools Network panel.',
9998
),
10099
},
101100
handler: async (request, response, context) => {
@@ -107,7 +106,7 @@ Get the request currently selected in the DevTools UI by ommitting reqid`,
107106
response.attachNetworkRequest(data?.requestId);
108107
} else {
109108
response.appendResponseLine(
110-
`Nothing is currently selected in DevTools UI.`,
109+
`Nothing is currently selected in the DevTools Network panel.`,
111110
);
112111
}
113112
}

tests/formatters/networkFormatter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('networkFormatter', () => {
7878

7979
assert.equal(
8080
result,
81-
'reqid=1 GET http://example.com [pending] [selected in DevTools UI]',
81+
'reqid=1 GET http://example.com [pending] [selected in the DevTools Network panel]',
8282
);
8383
});
8484
});

tests/tools/emulation.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
import assert from 'node:assert';
77
import {describe, it} from 'node:test';
88

9-
import {emulateCpu, emulateNetwork} from '../../src/tools/emulation.js';
9+
import {emulate} from '../../src/tools/emulation.js';
1010
import {withBrowser} from '../utils.js';
1111

1212
describe('emulation', () => {
1313
describe('network', () => {
1414
it('emulates offline network conditions', async () => {
1515
await withBrowser(async (response, context) => {
16-
await emulateNetwork.handler(
16+
await emulate.handler(
1717
{
1818
params: {
19-
throttlingOption: 'Offline',
19+
networkConditions: 'Offline',
2020
},
2121
},
2222
response,
@@ -26,12 +26,12 @@ describe('emulation', () => {
2626
assert.strictEqual(context.getNetworkConditions(), 'Offline');
2727
});
2828
});
29-
it('emulates network throttling when the throttling option is valid ', async () => {
29+
it('emulates network throttling when the throttling option is valid', async () => {
3030
await withBrowser(async (response, context) => {
31-
await emulateNetwork.handler(
31+
await emulate.handler(
3232
{
3333
params: {
34-
throttlingOption: 'Slow 3G',
34+
networkConditions: 'Slow 3G',
3535
},
3636
},
3737
response,
@@ -44,10 +44,10 @@ describe('emulation', () => {
4444

4545
it('disables network emulation', async () => {
4646
await withBrowser(async (response, context) => {
47-
await emulateNetwork.handler(
47+
await emulate.handler(
4848
{
4949
params: {
50-
throttlingOption: 'No emulation',
50+
networkConditions: 'No emulation',
5151
},
5252
},
5353
response,
@@ -60,10 +60,10 @@ describe('emulation', () => {
6060

6161
it('does not set throttling when the network throttling is not one of the predefined options', async () => {
6262
await withBrowser(async (response, context) => {
63-
await emulateNetwork.handler(
63+
await emulate.handler(
6464
{
6565
params: {
66-
throttlingOption: 'Slow 11G',
66+
networkConditions: 'Slow 11G',
6767
},
6868
},
6969
response,
@@ -77,10 +77,10 @@ describe('emulation', () => {
7777
it('report correctly for the currently selected page', async () => {
7878
await withBrowser(async (response, context) => {
7979
await context.newPage();
80-
await emulateNetwork.handler(
80+
await emulate.handler(
8181
{
8282
params: {
83-
throttlingOption: 'Slow 3G',
83+
networkConditions: 'Slow 3G',
8484
},
8585
},
8686
response,
@@ -99,10 +99,10 @@ describe('emulation', () => {
9999
describe('cpu', () => {
100100
it('emulates cpu throttling when the rate is valid (1-20x)', async () => {
101101
await withBrowser(async (response, context) => {
102-
await emulateCpu.handler(
102+
await emulate.handler(
103103
{
104104
params: {
105-
throttlingRate: 4,
105+
cpuThrottlingRate: 4,
106106
},
107107
},
108108
response,
@@ -116,10 +116,10 @@ describe('emulation', () => {
116116
it('disables cpu throttling', async () => {
117117
await withBrowser(async (response, context) => {
118118
context.setCpuThrottlingRate(4); // Set it to something first.
119-
await emulateCpu.handler(
119+
await emulate.handler(
120120
{
121121
params: {
122-
throttlingRate: 1,
122+
cpuThrottlingRate: 1,
123123
},
124124
},
125125
response,
@@ -133,10 +133,10 @@ describe('emulation', () => {
133133
it('report correctly for the currently selected page', async () => {
134134
await withBrowser(async (response, context) => {
135135
await context.newPage();
136-
await emulateCpu.handler(
136+
await emulate.handler(
137137
{
138138
params: {
139-
throttlingRate: 4,
139+
cpuThrottlingRate: 4,
140140
},
141141
},
142142
response,

0 commit comments

Comments
 (0)