Skip to content

Commit adf9028

Browse files
authored
[dotnet] [bidi] SetNetworkConditions command in Emulation module (#17047)
1 parent 9160cb2 commit adf9028

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ public async Task<SetTouchOverrideResult> SetTouchOverrideAsync(long? maxTouchPo
108108
return await ExecuteCommandAsync(new SetTouchOverrideCommand(@params), options, _jsonContext.SetTouchOverrideCommand, _jsonContext.SetTouchOverrideResult, cancellationToken).ConfigureAwait(false);
109109
}
110110

111+
public async Task<SetNetworkConditionsResult> SetNetworkConditionsAsync(NetworkConditions? networkConditions, SetNetworkConditionsOptions? options = null, CancellationToken cancellationToken = default)
112+
{
113+
var @params = new SetNetworkConditionsParameters(networkConditions, options?.Contexts, options?.UserContexts);
114+
115+
return await ExecuteCommandAsync(new SetNetworkConditionsCommand(@params), options, _jsonContext.SetNetworkConditionsCommand, _jsonContext.SetNetworkConditionsResult, cancellationToken).ConfigureAwait(false);
116+
}
117+
111118
protected override void Initialize(BiDi bidi, JsonSerializerOptions jsonSerializerOptions)
112119
{
113120
jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(bidi));
@@ -135,5 +142,7 @@ protected override void Initialize(BiDi bidi, JsonSerializerOptions jsonSerializ
135142
[JsonSerializable(typeof(SetGeolocationOverrideResult))]
136143
[JsonSerializable(typeof(SetTouchOverrideCommand))]
137144
[JsonSerializable(typeof(SetTouchOverrideResult))]
145+
[JsonSerializable(typeof(SetNetworkConditionsCommand))]
146+
[JsonSerializable(typeof(SetNetworkConditionsResult))]
138147

139148
internal partial class EmulationJsonSerializerContext : JsonSerializerContext;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// <copyright file="SetNetworkConditionsCommand.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using System.Collections.Generic;
21+
using System.Text.Json.Serialization;
22+
23+
namespace OpenQA.Selenium.BiDi.Emulation;
24+
25+
internal sealed class SetNetworkConditionsCommand(SetNetworkConditionsParameters @params)
26+
: Command<SetNetworkConditionsParameters, SetNetworkConditionsResult>(@params, "emulation.setNetworkConditions");
27+
28+
internal sealed record SetNetworkConditionsParameters([property: JsonIgnore(Condition = JsonIgnoreCondition.Never)] NetworkConditions? NetworkConditions, IEnumerable<BrowsingContext.BrowsingContext>? Contexts, IEnumerable<Browser.UserContext>? UserContexts) : Parameters;
29+
30+
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
31+
[JsonDerivedType(typeof(NetworkConditionsOffline), "offline")]
32+
public abstract record NetworkConditions;
33+
34+
public sealed record NetworkConditionsOffline : NetworkConditions;
35+
36+
public sealed class SetNetworkConditionsOptions : CommandOptions
37+
{
38+
public IEnumerable<BrowsingContext.BrowsingContext>? Contexts { get; set; }
39+
40+
public IEnumerable<Browser.UserContext>? UserContexts { get; set; }
41+
}
42+
43+
public sealed record SetNetworkConditionsResult : EmptyResult;

dotnet/test/common/BiDi/Emulation/EmulationTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,26 @@ public void CanSetTouchOverrideToDefault()
223223
},
224224
Throws.Nothing);
225225
}
226+
227+
[Test]
228+
[IgnoreBrowser(Selenium.Browser.Firefox, "Not supported yet?")]
229+
public void CanSetNetworkConditionsOffline()
230+
{
231+
Assert.That(async () =>
232+
{
233+
await bidi.Emulation.SetNetworkConditionsAsync(new NetworkConditionsOffline(), new() { Contexts = [context] });
234+
},
235+
Throws.Nothing);
236+
}
237+
238+
[Test]
239+
[IgnoreBrowser(Selenium.Browser.Firefox, "Not supported yet?")]
240+
public void CanSetNetworkConditionsToDefault()
241+
{
242+
Assert.That(async () =>
243+
{
244+
await bidi.Emulation.SetNetworkConditionsAsync(null, new() { Contexts = [context] });
245+
},
246+
Throws.Nothing);
247+
}
226248
}

0 commit comments

Comments
 (0)