Skip to content

Commit 4659e07

Browse files
committed
Bidi SetOfflineAsync
1 parent 4e27d19 commit 4659e07

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
@@ -960,21 +960,6 @@
960960
"FAIL"
961961
]
962962
},
963-
{
964-
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
965-
"testIdPattern": "[page.spec] *Page.setOfflineMode*",
966-
"platforms": [
967-
"darwin",
968-
"linux",
969-
"win32"
970-
],
971-
"parameters": [
972-
"webDriverBiDi"
973-
],
974-
"expectations": [
975-
"FAIL"
976-
]
977-
},
978963
{
979964
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
980965
"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
{
@@ -371,7 +372,24 @@ await BidiBrowser.Driver.Storage.DeleteCookiesAsync(new WebDriverBiDi.Storage.De
371372
public override Task SetRequestInterceptionAsync(bool value) => throw new NotImplementedException();
372373

373374
/// <inheritdoc />
374-
public override Task SetOfflineModeAsync(bool value) => throw new NotImplementedException();
375+
public override async Task SetOfflineModeAsync(bool value)
376+
{
377+
if (!BidiBrowser.CdpSupported)
378+
{
379+
throw new NotSupportedException();
380+
}
381+
382+
_emulatedNetworkConditions ??= new InternalNetworkConditions
383+
{
384+
Offline = false,
385+
Upload = -1,
386+
Download = -1,
387+
Latency = 0,
388+
};
389+
390+
_emulatedNetworkConditions.Offline = value;
391+
await ApplyNetworkConditionsAsync().ConfigureAwait(false);
392+
}
375393

376394
/// <inheritdoc />
377395
public override Task EmulateNetworkConditionsAsync(NetworkConditions networkConditions) => throw new NotImplementedException();
@@ -506,6 +524,24 @@ private async Task<IResponse> GoAsync(int delta, NavigationOptions options)
506524
return waitForNavigationTask.Result;
507525
}
508526

527+
private async Task ApplyNetworkConditionsAsync()
528+
{
529+
if (_emulatedNetworkConditions == null)
530+
{
531+
return;
532+
}
533+
534+
await BidiMainFrame.Client.SendAsync(
535+
"Network.emulateNetworkConditions",
536+
new Cdp.Messaging.NetworkEmulateNetworkConditionsRequest
537+
{
538+
Offline = _emulatedNetworkConditions.Offline,
539+
Latency = _emulatedNetworkConditions.Latency,
540+
UploadThroughput = _emulatedNetworkConditions.Upload,
541+
DownloadThroughput = _emulatedNetworkConditions.Download,
542+
}).ConfigureAwait(false);
543+
}
544+
509545
private void Initialize()
510546
{
511547
}

0 commit comments

Comments
 (0)