Skip to content

Commit fb1eb8e

Browse files
authored
Bidi SetOfflineAsync (#3001)
1 parent ba4e1ad commit fb1eb8e

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -945,21 +945,6 @@
945945
"FAIL"
946946
]
947947
},
948-
{
949-
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
950-
"testIdPattern": "[page.spec] *Page.setOfflineMode*",
951-
"platforms": [
952-
"darwin",
953-
"linux",
954-
"win32"
955-
],
956-
"parameters": [
957-
"webDriverBiDi"
958-
],
959-
"expectations": [
960-
"FAIL"
961-
]
962-
},
963948
{
964949
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
965950
"testIdPattern": "[page.spec] *Page.bringToFront*",

lib/PuppeteerSharp/Bidi/BidiPage.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace PuppeteerSharp.Bidi;
3737
public class BidiPage : Page
3838
{
3939
private readonly CdpEmulationManager _cdpEmulationManager;
40+
private InternalNetworkConditions _emulatedNetworkConditions;
4041

4142
internal BidiPage(BidiBrowserContext browserContext, BrowsingContext browsingContext) : base(browserContext.ScreenshotTaskQueue)
4243
{
@@ -410,7 +411,24 @@ await BidiBrowser.Driver.Storage.DeleteCookiesAsync(new WebDriverBiDi.Storage.De
410411
public override Task SetRequestInterceptionAsync(bool value) => throw new NotImplementedException();
411412

412413
/// <inheritdoc />
413-
public override Task SetOfflineModeAsync(bool value) => throw new NotImplementedException();
414+
public override async Task SetOfflineModeAsync(bool value)
415+
{
416+
if (!BidiBrowser.CdpSupported)
417+
{
418+
throw new NotSupportedException();
419+
}
420+
421+
_emulatedNetworkConditions ??= new InternalNetworkConditions
422+
{
423+
Offline = false,
424+
Upload = -1,
425+
Download = -1,
426+
Latency = 0,
427+
};
428+
429+
_emulatedNetworkConditions.Offline = value;
430+
await ApplyNetworkConditionsAsync().ConfigureAwait(false);
431+
}
414432

415433
/// <inheritdoc />
416434
public override Task EmulateNetworkConditionsAsync(NetworkConditions networkConditions) => throw new NotImplementedException();
@@ -545,6 +563,24 @@ private async Task<IResponse> GoAsync(int delta, NavigationOptions options)
545563
return waitForNavigationTask.Result;
546564
}
547565

566+
private async Task ApplyNetworkConditionsAsync()
567+
{
568+
if (_emulatedNetworkConditions == null)
569+
{
570+
return;
571+
}
572+
573+
await BidiMainFrame.Client.SendAsync(
574+
"Network.emulateNetworkConditions",
575+
new Cdp.Messaging.NetworkEmulateNetworkConditionsRequest
576+
{
577+
Offline = _emulatedNetworkConditions.Offline,
578+
Latency = _emulatedNetworkConditions.Latency,
579+
UploadThroughput = _emulatedNetworkConditions.Upload,
580+
DownloadThroughput = _emulatedNetworkConditions.Download,
581+
}).ConfigureAwait(false);
582+
}
583+
548584
private void Initialize()
549585
{
550586
}

0 commit comments

Comments
 (0)