Skip to content

Commit 2d9d620

Browse files
authored
Bidi Emulate network conditions (#3045)
1 parent b53fcea commit 2d9d620

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,21 +333,6 @@
333333
"FAIL"
334334
]
335335
},
336-
{
337-
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
338-
"testIdPattern": "[emulation.spec] *Page.emulateNetworkConditions*",
339-
"platforms": [
340-
"darwin",
341-
"linux",
342-
"win32"
343-
],
344-
"parameters": [
345-
"webDriverBiDi"
346-
],
347-
"expectations": [
348-
"FAIL"
349-
]
350-
},
351336
{
352337
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
353338
"testIdPattern": "[emulation.spec] *Page.emulateVision*",

lib/PuppeteerSharp/Bidi/BidiPage.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,38 @@ public override async Task SetOfflineModeAsync(bool value)
791791
}
792792

793793
/// <inheritdoc />
794-
public override Task EmulateNetworkConditionsAsync(NetworkConditions networkConditions) => throw new NotImplementedException();
794+
public override async Task EmulateNetworkConditionsAsync(NetworkConditions networkConditions)
795+
{
796+
if (!BidiBrowser.CdpSupported)
797+
{
798+
// WebDriver BiDi supports only offline mode
799+
if (networkConditions != null &&
800+
!networkConditions.Offline &&
801+
(networkConditions.Upload >= 0 ||
802+
networkConditions.Download >= 0 ||
803+
networkConditions.Latency > 0))
804+
{
805+
throw new PuppeteerException("Network throttling is not supported in this browser with WebDriver BiDi");
806+
}
807+
808+
await BidiMainFrame.BrowsingContext.SetOfflineModeAsync(networkConditions?.Offline ?? false).ConfigureAwait(false);
809+
return;
810+
}
811+
812+
_emulatedNetworkConditions ??= new InternalNetworkConditions
813+
{
814+
Offline = networkConditions?.Offline ?? false,
815+
Upload = -1,
816+
Download = -1,
817+
Latency = 0,
818+
};
819+
820+
_emulatedNetworkConditions.Upload = networkConditions?.Upload ?? -1;
821+
_emulatedNetworkConditions.Download = networkConditions?.Download ?? -1;
822+
_emulatedNetworkConditions.Latency = networkConditions?.Latency ?? 0;
823+
_emulatedNetworkConditions.Offline = networkConditions?.Offline ?? false;
824+
await ApplyNetworkConditionsAsync().ConfigureAwait(false);
825+
}
795826

796827
/// <inheritdoc />
797828
public override async Task<CookieParam[]> GetCookiesAsync(params string[] urls)

lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ internal async Task SetUserAgentAsync(string userAgent)
224224
await Session.Driver.Emulation.SetUserAgentOverrideAsync(parameters).ConfigureAwait(false);
225225
}
226226

227+
internal async Task SetOfflineModeAsync(bool enabled)
228+
{
229+
var parameters = new SetNetworkConditionsCommandParameters
230+
{
231+
NetworkConditions = enabled ? new NetworkConditionsOffline() : null,
232+
Contexts = [Id],
233+
};
234+
await Session.Driver.Emulation.SetNetworkConditions(parameters).ConfigureAwait(false);
235+
}
236+
227237
protected virtual void OnBrowsingContextCreated(BidiBrowsingContextEventArgs e) => BrowsingContextCreated?.Invoke(this, e);
228238

229239
private void Initialize()

lib/PuppeteerSharp/InternalNetworkConditions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ namespace PuppeteerSharp
22
{
33
internal class InternalNetworkConditions : NetworkConditions
44
{
5-
public bool Offline { get; set; }
65
}
76
}

lib/PuppeteerSharp/NetworkConditions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public class NetworkConditions
2525
/// </summary>
2626
public const string Fast4G = "Fast 4G";
2727

28+
/// <summary>
29+
/// Emulates the offline mode. Shortcut for <see cref="IPage.SetOfflineModeAsync(bool)"/>.
30+
/// </summary>
31+
public bool Offline { get; set; }
32+
2833
/// <summary>
2934
/// Download speed (bytes/s), `-1` to disable.
3035
/// </summary>

0 commit comments

Comments
 (0)