Skip to content

Commit 1eec84e

Browse files
authored
Merge branch 'v21' into bidi-emulate-vision
2 parents d9bff20 + 2d9d620 commit 1eec84e

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": "[evaluation.spec] *should accept element handle as an argument*",

lib/PuppeteerSharp/Bidi/BidiPage.cs

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

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

797828
/// <inheritdoc />
798829
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)