Skip to content

Commit 1e8662f

Browse files
fix(emulation): correctly report info for selected page (#63)
Previously if one page was setting emulation all were reporting it.
1 parent 54efe95 commit 1e8662f

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

src/McpContext.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export class McpContext implements Context {
4949
#consoleCollector: PageCollector<ConsoleMessage | Error>;
5050

5151
#isRunningTrace = false;
52-
#networkConditions: string | null = null;
53-
#cpuThrottlingRate = 1;
52+
#networkConditionsMap = new WeakMap<Page, string>();
53+
#cpuThrottlingRateMap = new WeakMap<Page, number>();
5454
#dialog?: Dialog;
5555

5656
#nextSnapshotId = 1;
@@ -130,19 +130,27 @@ export class McpContext implements Context {
130130
}
131131

132132
setNetworkConditions(conditions: string | null): void {
133-
this.#networkConditions = conditions;
133+
const page = this.getSelectedPage();
134+
if (conditions === null) {
135+
this.#networkConditionsMap.delete(page);
136+
} else {
137+
this.#networkConditionsMap.set(page, conditions);
138+
}
134139
}
135140

136141
getNetworkConditions(): string | null {
137-
return this.#networkConditions;
142+
const page = this.getSelectedPage();
143+
return this.#networkConditionsMap.get(page) ?? null;
138144
}
139145

140146
setCpuThrottlingRate(rate: number): void {
141-
this.#cpuThrottlingRate = rate;
147+
const page = this.getSelectedPage();
148+
this.#cpuThrottlingRateMap.set(page, rate);
142149
}
143150

144151
getCpuThrottlingRate(): number {
145-
return this.#cpuThrottlingRate;
152+
const page = this.getSelectedPage();
153+
return this.#cpuThrottlingRateMap.get(page) ?? 1;
146154
}
147155

148156
setIsRunningPerformanceTrace(x: boolean): void {

tests/tools/emulation.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ describe('emulation', () => {
5757
assert.strictEqual(context.getNetworkConditions(), null);
5858
});
5959
});
60+
61+
it('report correctly for the currently selected page', async () => {
62+
await withBrowser(async (response, context) => {
63+
await context.newPage();
64+
await emulateNetwork.handler(
65+
{
66+
params: {
67+
throttlingOption: 'Slow 3G',
68+
},
69+
},
70+
response,
71+
context,
72+
);
73+
74+
assert.strictEqual(context.getNetworkConditions(), 'Slow 3G');
75+
76+
context.setSelectedPageIdx(0);
77+
78+
assert.strictEqual(context.getNetworkConditions(), null);
79+
});
80+
});
6081
});
6182

6283
describe('cpu', () => {
@@ -92,5 +113,26 @@ describe('emulation', () => {
92113
assert.strictEqual(context.getCpuThrottlingRate(), 1);
93114
});
94115
});
116+
117+
it('report correctly for the currently selected page', async () => {
118+
await withBrowser(async (response, context) => {
119+
await context.newPage();
120+
await emulateCpu.handler(
121+
{
122+
params: {
123+
throttlingRate: 4,
124+
},
125+
},
126+
response,
127+
context,
128+
);
129+
130+
assert.strictEqual(context.getCpuThrottlingRate(), 4);
131+
132+
context.setSelectedPageIdx(0);
133+
134+
assert.strictEqual(context.getCpuThrottlingRate(), 1);
135+
});
136+
});
95137
});
96138
});

0 commit comments

Comments
 (0)