Skip to content

Commit 480164d

Browse files
authored
[dotnet] [bidi] Context aware network collector (#16866)
1 parent 5f0eb7f commit 480164d

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
using OpenQA.Selenium.BiDi.Network;
2121
using System;
22+
using System.Collections.Generic;
2223
using System.Threading.Tasks;
2324

2425
namespace OpenQA.Selenium.BiDi.BrowsingContext;
@@ -79,6 +80,16 @@ await interception.OnAuthRequiredAsync(
7980
return interception;
8081
}
8182

83+
public Task<AddDataCollectorResult> AddDataCollectorAsync(IEnumerable<DataType> dataTypes, int maxEncodedDataSize, BrowsingContextAddDataCollectorOptions? options = null)
84+
{
85+
AddDataCollectorOptions addDataCollectorOptions = new(options)
86+
{
87+
Contexts = [context]
88+
};
89+
90+
return networkModule.AddDataCollectorAsync(dataTypes, maxEncodedDataSize, addDataCollectorOptions);
91+
}
92+
8293
public Task<SetCacheBehaviorResult> SetCacheBehaviorAsync(CacheBehavior behavior, BrowsingContextSetCacheBehaviorOptions? options = null)
8394
{
8495
SetCacheBehaviorOptions setCacheBehaviorOptions = new(options)

dotnet/src/webdriver/BiDi/Network/AddDataCollectorCommand.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,28 @@ internal sealed class AddDataCollectorCommand(AddDataCollectorParameters @params
2828

2929
internal sealed record AddDataCollectorParameters(IEnumerable<DataType> DataTypes, int MaxEncodedDataSize, CollectorType? CollectorType, IEnumerable<BrowsingContext.BrowsingContext>? Contexts, IEnumerable<Browser.UserContext>? UserContexts) : Parameters;
3030

31-
public class AddDataCollectorOptions : CommandOptions
31+
public class AddDataCollectorOptions() : CommandOptions
3232
{
33+
internal AddDataCollectorOptions(BrowsingContextAddDataCollectorOptions? options) : this()
34+
{
35+
CollectorType = options?.CollectorType;
36+
UserContexts = options?.UserContexts;
37+
}
38+
3339
public CollectorType? CollectorType { get; set; }
3440

3541
public IEnumerable<BrowsingContext.BrowsingContext>? Contexts { get; set; }
3642

3743
public IEnumerable<Browser.UserContext>? UserContexts { get; set; }
3844
}
3945

46+
public class BrowsingContextAddDataCollectorOptions
47+
{
48+
public CollectorType? CollectorType { get; set; }
49+
50+
public IEnumerable<Browser.UserContext>? UserContexts { get; set; }
51+
}
52+
4053
public sealed record AddDataCollectorResult(Collector Collector) : EmptyResult;
4154

4255
[JsonConverter(typeof(CamelCaseEnumConverter<DataType>))]

dotnet/src/webdriver/BiDi/Network/NetworkModule.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ public sealed partial class NetworkModule : Module
3030
{
3131
private NetworkJsonSerializerContext _jsonContext = null!;
3232

33-
public async Task<Collector> AddDataCollectorAsync(IEnumerable<DataType> DataTypes, int MaxEncodedDataSize, AddDataCollectorOptions? options = null)
33+
public async Task<AddDataCollectorResult> AddDataCollectorAsync(IEnumerable<DataType> dataTypes, int maxEncodedDataSize, AddDataCollectorOptions? options = null)
3434
{
35-
var @params = new AddDataCollectorParameters(DataTypes, MaxEncodedDataSize, options?.CollectorType, options?.Contexts, options?.UserContexts);
35+
var @params = new AddDataCollectorParameters(dataTypes, maxEncodedDataSize, options?.CollectorType, options?.Contexts, options?.UserContexts);
3636

37-
var result = await Broker.ExecuteCommandAsync(new AddDataCollectorCommand(@params), options, _jsonContext.AddDataCollectorCommand, _jsonContext.AddDataCollectorResult).ConfigureAwait(false);
38-
39-
return result.Collector;
37+
return await Broker.ExecuteCommandAsync(new AddDataCollectorCommand(@params), options, _jsonContext.AddDataCollectorCommand, _jsonContext.AddDataCollectorResult).ConfigureAwait(false);
4038
}
4139

4240
public async Task<AddInterceptResult> AddInterceptAsync(IEnumerable<InterceptPhase> phases, AddInterceptOptions? options = null)

dotnet/src/webdriver/BiDi/Network/SetCacheBehaviorCommand.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,9 @@ internal sealed class SetCacheBehaviorCommand(SetCacheBehaviorParameters @params
2828

2929
internal sealed record SetCacheBehaviorParameters(CacheBehavior CacheBehavior, IEnumerable<BrowsingContext.BrowsingContext>? Contexts) : Parameters;
3030

31-
public sealed class SetCacheBehaviorOptions : CommandOptions
31+
public sealed class SetCacheBehaviorOptions() : CommandOptions
3232
{
33-
public SetCacheBehaviorOptions()
34-
{
35-
36-
}
37-
38-
internal SetCacheBehaviorOptions(BrowsingContextSetCacheBehaviorOptions? options)
33+
internal SetCacheBehaviorOptions(BrowsingContextSetCacheBehaviorOptions? options) : this()
3934
{
4035

4136
}

dotnet/test/common/BiDi/Network/NetworkTest.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@ public async Task CanAddDataCollector()
3131
{
3232
// Firefox doesn't like int.MaxValue as max encoded data size
3333
// invalid argument: Expected "maxEncodedDataSize" to be less than the max total data size available (200000000), got 2147483647
34-
var collector = await bidi.Network.AddDataCollectorAsync([DataType.Response], 200000000);
34+
var addDataCollectorResult = await bidi.Network.AddDataCollectorAsync([DataType.Response], 200000000);
35+
36+
Assert.That(addDataCollectorResult, Is.Not.Null);
37+
Assert.That(addDataCollectorResult.Collector, Is.Not.Null);
38+
39+
// or context aware
40+
addDataCollectorResult = await context.Network.AddDataCollectorAsync([DataType.Response], 200000000);
3541

36-
Assert.That(collector, Is.Not.Null);
42+
Assert.That(addDataCollectorResult, Is.Not.Null);
43+
Assert.That(addDataCollectorResult.Collector, Is.Not.Null);
3744
}
3845

3946
[Test]

0 commit comments

Comments
 (0)