Skip to content

Commit 79c47f4

Browse files
authored
feat: support offline parameter in emulateNetworkConditions (#14184)
1 parent 7645421 commit 79c47f4

File tree

8 files changed

+69
-9
lines changed

8 files changed

+69
-9
lines changed

docs/api/puppeteer.networkconditions.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,29 @@ Latency (ms)
6969
</td></tr>
7070
<tr><td>
7171

72+
<span id="offline">offline</span>
73+
74+
</td><td>
75+
76+
`optional`
77+
78+
</td><td>
79+
80+
boolean
81+
82+
</td><td>
83+
84+
Emulates the offline mode.
85+
86+
**Remarks:**
87+
88+
Shortcut for [Page.setOfflineMode()](./puppeteer.page.setofflinemode.md).
89+
90+
</td><td>
91+
92+
</td></tr>
93+
<tr><td>
94+
7295
<span id="upload">upload</span>
7396

7497
</td><td>

docs/api/puppeteer.page.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,9 +1144,9 @@ NOTE: changing this value won't affect scripts that have already been run. It wi
11441144
11451145
</td><td>
11461146
1147-
Sets the network connection to offline.
1147+
Emulates the offline mode.
11481148
1149-
It does not change the parameters used in [Page.emulateNetworkConditions()](./puppeteer.page.emulatenetworkconditions.md)
1149+
It does not change the download/upload/latency parameters set by [Page.emulateNetworkConditions()](./puppeteer.page.emulatenetworkconditions.md)
11501150
11511151
</td></tr>
11521152
<tr><td>

docs/api/puppeteer.page.setofflinemode.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ sidebar_label: Page.setOfflineMode
44

55
# Page.setOfflineMode() method
66

7-
Sets the network connection to offline.
7+
Emulates the offline mode.
88

9-
It does not change the parameters used in [Page.emulateNetworkConditions()](./puppeteer.page.emulatenetworkconditions.md)
9+
It does not change the download/upload/latency parameters set by [Page.emulateNetworkConditions()](./puppeteer.page.emulatenetworkconditions.md)
1010

1111
### Signature
1212

packages/puppeteer-core/src/api/Page.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,9 +987,10 @@ export abstract class Page extends EventEmitter<PageEvents> {
987987
abstract setDragInterception(enabled: boolean): Promise<void>;
988988

989989
/**
990-
* Sets the network connection to offline.
990+
* Emulates the offline mode.
991991
*
992-
* It does not change the parameters used in {@link Page.emulateNetworkConditions}
992+
* It does not change the download/upload/latency parameters set by
993+
* {@link Page.emulateNetworkConditions}
993994
*
994995
* @param enabled - When `true`, enables offline mode for the page.
995996
*/

packages/puppeteer-core/src/bidi/Page.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ export class BidiPage extends Page {
769769
}
770770
if (!this.#emulatedNetworkConditions) {
771771
this.#emulatedNetworkConditions = {
772-
offline: false,
772+
offline: networkConditions?.offline ?? false,
773773
upload: -1,
774774
download: -1,
775775
latency: 0,
@@ -784,6 +784,8 @@ export class BidiPage extends Page {
784784
this.#emulatedNetworkConditions.latency = networkConditions
785785
? networkConditions.latency
786786
: 0;
787+
this.#emulatedNetworkConditions.offline =
788+
networkConditions?.offline ?? false;
787789
return await this.#applyNetworkConditions();
788790
}
789791

packages/puppeteer-core/src/cdp/NetworkManager.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ import {
3131
* @public
3232
*/
3333
export interface NetworkConditions {
34+
/**
35+
* Emulates the offline mode.
36+
*
37+
* @remarks
38+
*
39+
* Shortcut for {@link Page.setOfflineMode}.
40+
*/
41+
offline?: boolean;
3442
/**
3543
* Download speed (bytes/s)
3644
*/
@@ -207,7 +215,7 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
207215
): Promise<void> {
208216
if (!this.#emulatedNetworkConditions) {
209217
this.#emulatedNetworkConditions = {
210-
offline: false,
218+
offline: networkConditions?.offline ?? false,
211219
upload: -1,
212220
download: -1,
213221
latency: 0,
@@ -222,7 +230,8 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
222230
this.#emulatedNetworkConditions.latency = networkConditions
223231
? networkConditions.latency
224232
: 0;
225-
233+
this.#emulatedNetworkConditions.offline =
234+
networkConditions?.offline ?? false;
226235
await this.#applyToAllClients(this.#applyNetworkConditions.bind(this));
227236
}
228237

test/TestExpectations.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,13 @@
278278
"expectations": ["SKIP"],
279279
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
280280
},
281+
{
282+
"testIdPattern": "[emulation.spec] Emulation Page.emulateNetworkConditions should support offline",
283+
"platforms": ["darwin", "linux", "win32"],
284+
"parameters": ["firefox"],
285+
"expectations": ["FAIL"],
286+
"comment": "Not supported by WebDriver BiDi"
287+
},
281288
{
282289
"testIdPattern": "[emulation.spec] Emulation Page.emulateTimezone should support timezone offset",
283290
"platforms": ["darwin", "linux", "win32"],

test/src/emulation.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,24 @@ describe('Emulation', () => {
567567
});
568568

569569
describe('Page.emulateNetworkConditions', function () {
570+
it('should support offline', async () => {
571+
const {page, server} = await getTestState();
572+
573+
await page.emulateNetworkConditions({
574+
offline: true,
575+
download: 0,
576+
upload: 0,
577+
latency: 0,
578+
});
579+
580+
try {
581+
await page.goto(server.EMPTY_PAGE);
582+
throw new Error('not reached');
583+
} catch (err) {
584+
expect((err as Error).message).toMatch(/ERR_INTERNET_DISCONNECTED/);
585+
}
586+
});
587+
570588
it('should change navigator.connection.effectiveType', async () => {
571589
const {page} = await getTestState();
572590

0 commit comments

Comments
 (0)