Skip to content

Conversation

@nvborisenko
Copy link
Member

@nvborisenko nvborisenko commented Jan 7, 2026

User description

This is following to pattern of extensions.

💥 What does this PR do?

Ability to add network data collector which is scoped to context:

context.Network.AddDataCollectorAsync(...); // implicitly scoped to context

🔧 Implementation Notes

  • I don't see a criminal
  • Fixed return type for AddDataCollector command

🔄 Types of changes

  • New feature (non-breaking change which adds functionality and tests!)
  • Breaking change (fix or feature that would cause existing functionality to change)

PR Type

Enhancement


Description

  • Add context-aware network data collector scoped to browsing context

  • Introduce BrowsingContextAddDataCollectorOptions for context-specific configuration

  • Fix AddDataCollectorAsync return type from Collector to AddDataCollectorResult

  • Update tests to verify both module-level and context-level data collector functionality


Diagram Walkthrough

flowchart LR
  A["BrowsingContextNetworkModule"] -->|"AddDataCollectorAsync"| B["AddDataCollectorOptions"]
  B -->|"with Contexts"| C["NetworkModule.AddDataCollectorAsync"]
  C -->|"returns"| D["AddDataCollectorResult"]
  E["BrowsingContextAddDataCollectorOptions"] -->|"converted to"| B
Loading

File Walkthrough

Relevant files
Enhancement
BrowsingContextNetworkModule.cs
Add context-aware AddDataCollectorAsync method                     

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

  • Add System.Collections.Generic import for IEnumerable support
  • Implement AddDataCollectorAsync method that wraps network module call
    with context-scoped options
  • Automatically set Contexts property to current browsing context
+11/-0   
AddDataCollectorCommand.cs
Refactor AddDataCollectorOptions with context-aware variant

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

  • Convert AddDataCollectorOptions to parameterless constructor with
    internal copy constructor
  • Add internal constructor accepting
    BrowsingContextAddDataCollectorOptions for conversion
  • Introduce new BrowsingContextAddDataCollectorOptions class for
    context-specific configuration
  • Separate context-aware options from module-level options
+14/-1   
Bug fix
NetworkModule.cs
Fix AddDataCollectorAsync return type and parameter naming

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

  • Fix return type from Collector to AddDataCollectorResult for
    consistency
  • Normalize parameter naming from PascalCase to camelCase
  • Simplify method by returning result directly instead of extracting
    Collector property
+3/-5     
Formatting
SetCacheBehaviorCommand.cs
Simplify SetCacheBehaviorOptions constructor pattern         

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

  • Refactor SetCacheBehaviorOptions to use parameterless constructor
    syntax
  • Consolidate internal constructor to use : this() pattern for cleaner
    code
+2/-7     
Tests
NetworkTest.cs
Update tests for AddDataCollectorResult and context-aware collector

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

  • Update test to verify AddDataCollectorResult object instead of
    Collector directly
  • Add assertions for both result object and nested Collector property
  • Add test case for context-aware AddDataCollectorAsync method
  • Verify both module-level and context-level data collector
    functionality
+9/-2     

@selenium-ci selenium-ci added the C-dotnet .NET Bindings label Jan 7, 2026
@qodo-code-review
Copy link
Contributor

qodo-code-review bot commented Jan 7, 2026

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

🔴
Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status:
Missing argument validation: The new public AddDataCollectorAsync wrapper does not validate dataTypes for
null/emptiness or maxEncodedDataSize for invalid values, which can lead to non-actionable
downstream failures.

Referred Code
public Task<AddDataCollectorResult> AddDataCollectorAsync(IEnumerable<DataType> dataTypes, int maxEncodedDataSize, BrowsingContextAddDataCollectorOptions? options = null)
{
    AddDataCollectorOptions addDataCollectorOptions = new(options)
    {
        Contexts = [context]
    };

    return networkModule.AddDataCollectorAsync(dataTypes, maxEncodedDataSize, addDataCollectorOptions);
}

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status:
External input validation: The updated AddDataCollectorAsync accepts caller-controlled inputs (dataTypes,
maxEncodedDataSize, and options) without explicit validation, so it is unclear if
upstream/downstream layers reliably prevent invalid or abusive values from reaching the
BiDi command broker.

Referred Code
public async Task<AddDataCollectorResult> AddDataCollectorAsync(IEnumerable<DataType> dataTypes, int maxEncodedDataSize, AddDataCollectorOptions? options = null)
{
    var @params = new AddDataCollectorParameters(dataTypes, maxEncodedDataSize, options?.CollectorType, options?.Contexts, options?.UserContexts);

    return await Broker.ExecuteCommandAsync(new AddDataCollectorCommand(@params), options, _jsonContext.AddDataCollectorCommand, _jsonContext.AddDataCollectorResult).ConfigureAwait(false);
}

Learn more about managing compliance generic rules or creating your own custom rules

  • Update
Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-code-review
Copy link
Contributor

qodo-code-review bot commented Jan 7, 2026

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
Fix array initializer syntax

Correct the invalid array initializer syntax for the Contexts property.

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs [87]

-Contexts = [context]
+Contexts = new[] { context }
  • Apply / Chat
Suggestion importance[1-10]: 10

__

Why: The suggestion correctly identifies that the [context] syntax is not valid C# for array initialization and will cause a compilation error. Applying this fix is critical for the code to compile.

High
Possible issue
Correct test array syntax

Correct the invalid collection initializer syntax for the dataTypes parameter in
test method calls.

dotnet/test/common/BiDi/Network/NetworkTest.cs [34-40]

-var addDataCollectorResult = await bidi.Network.AddDataCollectorAsync([DataType.Response], 200000000);
-addDataCollectorResult = await context.Network.AddDataCollectorAsync([DataType.Response], 200000000);
+var addDataCollectorResult = await bidi.Network.AddDataCollectorAsync(new[] { DataType.Response }, 200000000);
+addDataCollectorResult = await context.Network.AddDataCollectorAsync(new[] { DataType.Response }, 200000000);
  • Apply / Chat
Suggestion importance[1-10]: 10

__

Why: The suggestion correctly identifies that the [DataType.Response] syntax is not valid C# for collection initialization and will cause a compilation error. Applying this fix is critical for the tests to compile.

High
Learned
best practice
Defensive copy of option collections

Copy options.UserContexts into a new concrete collection (or immutable
collection) before storing it, so later mutations of the caller-provided
enumerable don’t affect command options.

dotnet/src/webdriver/BiDi/Network/AddDataCollectorCommand.cs [33-37]

 internal AddDataCollectorOptions(BrowsingContextAddDataCollectorOptions? options) : this()
 {
     CollectorType = options?.CollectorType;
-    UserContexts = options?.UserContexts;
+    UserContexts = options?.UserContexts is null ? null : new List<Browser.UserContext>(options.UserContexts);
 }
  • Apply / Chat
Suggestion importance[1-10]: 6

__

Why:
Relevant best practice - When storing collection-like state from callers, make a defensive/immutable copy to avoid external mutation and hidden side effects.

Low
Validate inputs before delegating

Validate dataTypes is non-null and maxEncodedDataSize is within expected bounds
(e.g., positive) before forwarding the call to avoid opaque downstream failures.

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs [83-91]

 public Task<AddDataCollectorResult> AddDataCollectorAsync(IEnumerable<DataType> dataTypes, int maxEncodedDataSize, BrowsingContextAddDataCollectorOptions? options = null)
 {
+    ArgumentNullException.ThrowIfNull(dataTypes);
+    ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maxEncodedDataSize);
+
     AddDataCollectorOptions addDataCollectorOptions = new(options)
     {
         Contexts = [context]
     };
 
     return networkModule.AddDataCollectorAsync(dataTypes, maxEncodedDataSize, addDataCollectorOptions);
 }
  • Apply / Chat
Suggestion importance[1-10]: 5

__

Why:
Relevant best practice - Add explicit validation and null/availability guards at integration boundaries by checking presence and bounds before use.

Low
  • Update

@nvborisenko nvborisenko merged commit 480164d into SeleniumHQ:trunk Jan 10, 2026
11 checks passed
@nvborisenko nvborisenko deleted the bidi-context-collector branch January 10, 2026 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants