seleniumhq.github.io icon indicating copy to clipboard operation
seleniumhq.github.io copied to clipboard

[dotnet] Add examples for BiDi W3C Browsing Context

Open nvborisenko opened this issue 1 year ago • 4 comments

User description

Examples for https://www.selenium.dev/documentation/webdriver/bidi/w3c/browsing_context/

TODO:

  • [x] Finish examples
  • [ ] Add references in code blocks for C#
  • [ ] Reference stable version in csproj when v4.25 is released
  • [ ] Create new base fixture for all bidi tests

Types of changes

  • [ ] Change to the site (I have double-checked the Netlify deployment, and my changes look good)
  • [ ] Code example added (and I also added the example to all translated languages)
  • [ ] Improved translation
  • [ ] Added new translation (and I also added a notice to each document missing translation)

Checklist

  • [ ] I have read the contributing document.
  • [ ] I have used hugo to render the site/docs locally and I am sure it works.

PR Type

Tests, Enhancement


Description

  • Added multiple tests for various browsing context events and operations, including creation, destruction, navigation, and user prompts.
  • Enhanced the BaseTest class to include WebSocket URL option in ChromeOptions.
  • Updated NuGet configuration to include GitHub Packages as a source.
  • Upgraded Selenium.WebDriver package to a nightly version for testing.

Changes walkthrough 📝

Relevant files
Enhancement
1 files
BaseTest.cs
Add WebSocket URL option to ChromeOptions                               

examples/dotnet/SeleniumDocs/BaseTest.cs

  • Added UseWebSocketUrl option to ChromeOptions.
+2/-1     
Tests
11 files
BrowsingContextTest.BrowsingContextCreatedEvent.cs
Add test for BrowsingContextCreatedEvent                                 

examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.BrowsingContextCreatedEvent.cs

  • Added test for BrowsingContextCreatedEvent.
  • Utilized TaskCompletionSource for event handling.
  • +27/-0   
    BrowsingContextTest.BrowsingContextDestroyedEvent.cs
    Add test for BrowsingContextDestroyedEvent                             

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.BrowsingContextDestroyedEvent.cs

  • Added test for BrowsingContextDestroyedEvent.
  • Verified context destruction.
  • +30/-0   
    BrowsingContextTest.BrowsingContextLoadedEvent.cs
    Add test for BrowsingContextLoadedEvent                                   

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.BrowsingContextLoadedEvent.cs

  • Added test for BrowsingContextLoadedEvent.
  • Used TaskCompletionSource for event handling.
  • +27/-0   
    BrowsingContextTest.CreateBrowsingContext.cs
    Add tests for creating browsing contexts                                 

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.CreateBrowsingContext.cs

  • Added multiple tests for creating browsing contexts.
  • Included tests for tabs and windows with reference contexts.
  • +61/-0   
    BrowsingContextTest.DomContentLoadedEvent.cs
    Add test for DomContentLoadedEvent                                             

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.DomContentLoadedEvent.cs

  • Added test for DomContentLoadedEvent.
  • Used TaskCompletionSource for event handling.
  • +27/-0   
    BrowsingContextTest.FragmentNavigatedEvent.cs
    Add test for FragmentNavigatedEvent                                           

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.FragmentNavigatedEvent.cs

  • Added test for FragmentNavigatedEvent.
  • Verified navigation to fragment.
  • +29/-0   
    BrowsingContextTest.GetBrowsingContextTree.cs
    Add test for retrieving browsing context tree                       

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.GetBrowsingContextTree.cs

  • Added test for retrieving browsing context tree.
  • Verified tree structure and context relationships.
  • +28/-0   
    BrowsingContextTest.NavigateToUrl.cs
    Add tests for URL navigation                                                         

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.NavigateToUrl.cs

  • Added tests for navigating to URLs.
  • Verified navigation information and readiness state.
  • +33/-0   
    BrowsingContextTest.NavigationStartedEvent.cs
    Add test for NavigationStartedEvent                                           

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.NavigationStartedEvent.cs

  • Added test for NavigationStartedEvent.
  • Used TaskCompletionSource for event handling.
  • +26/-0   
    BrowsingContextTest.UserPromptEvent.cs
    Add tests for user prompt events                                                 

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.UserPromptEvent.cs

  • Added tests for UserPromptOpened and UserPromptClosed events.
  • Verified prompt handling.
  • +55/-0   
    BrowsingContextTest.cs
    Create BrowsingContextTest partial class                                 

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.cs

    • Created partial class for BrowsingContextTest.
    +8/-0     
    Configuration changes
    1 files
    NuGet.config
    Add GitHub Packages to NuGet sources                                         

    examples/dotnet/SeleniumDocs/NuGet.config

    • Added GitHub Packages as a package source.
    +1/-0     
    Dependencies
    1 files
    SeleniumDocs.csproj
    Update Selenium.WebDriver package version                               

    examples/dotnet/SeleniumDocs/SeleniumDocs.csproj

    • Updated Selenium.WebDriver package to a nightly version.
    +1/-1     

    💡 PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    nvborisenko avatar Sep 11 '24 19:09 nvborisenko

    Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    Latest commit f121c9efae0af31d833953c6561fcb23d39bab87

    netlify[bot] avatar Sep 11 '24 19:09 netlify[bot]

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Commented Code
    There is a commented out line of code that might be important for the test. It should be either removed or uncommented and explained.

    Nightly Version
    The PR is using a nightly version of Selenium.WebDriver (4.25.0-nightly202409100009). This might lead to instability and should be changed to a stable version before merging.

    qodo-code-review[bot] avatar Sep 11 '24 19:09 qodo-code-review[bot]

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Uncomment the line to handle the user prompt in the test

    Uncomment the line to handle the user prompt. This will allow the test to properly
    close the prompt and complete the event.

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.UserPromptEvent.cs [48]

    -//await browsingContext.HandleUserPromptAsync();
    +await browsingContext.HandleUserPromptAsync();
     
    
    • [ ] Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Uncommenting the line to handle the user prompt is crucial for the test to function correctly, as it ensures the prompt is properly closed and the event can complete. This addresses a potential issue in the test logic.

    9
    Enhancement
    Add a timeout to the HandleUserPromptAsync method call

    Consider adding a timeout parameter to the HandleUserPromptAsync method call to
    prevent indefinite waiting in case of issues.

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.UserPromptEvent.cs [48]

    -//await browsingContext.HandleUserPromptAsync();
    +await browsingContext.HandleUserPromptAsync(TimeSpan.FromSeconds(5));
     
    
    • [ ] Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding a timeout to the HandleUserPromptAsync method call is a valuable enhancement that prevents indefinite waiting, improving the robustness of the test in case of unexpected issues.

    8
    Add assertions to verify the content of the user prompt

    Consider adding assertions to verify the content of the user prompt, such as its
    type or message, to ensure the correct prompt is being handled.

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.UserPromptEvent.cs [29-30]

     Assert.IsNotNull(userPromptOpenedEventArgs);
    +Assert.AreEqual("prompt", userPromptOpenedEventArgs.Type);
    +Assert.AreEqual("Enter your name", userPromptOpenedEventArgs.Message);
     Console.WriteLine(userPromptOpenedEventArgs);
     
    
    • [ ] Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding assertions to verify the content of the user prompt enhances the test by ensuring that the correct prompt is being handled, which improves test accuracy and reliability.

    7
    Best practice
    Add error handling for the user prompt interaction

    Consider adding a try-catch block around the event handling to gracefully handle any
    exceptions that might occur during the prompt interaction.

    examples/dotnet/SeleniumDocs/BiDi/BrowsingContext/BrowsingContextTest.UserPromptEvent.cs [46-50]

    -driver.FindElement(By.Id("prompt")).Click();
    +try
    +{
    +    driver.FindElement(By.Id("prompt")).Click();
     
    -//await browsingContext.HandleUserPromptAsync();
    +    await browsingContext.HandleUserPromptAsync();
     
    -var userPromptClosedEventArgs = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));
    +    var userPromptClosedEventArgs = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));
    +}
    +catch (Exception ex)
    +{
    +    Console.WriteLine($"Error handling user prompt: {ex.Message}");
    +    Assert.Fail("User prompt handling failed");
    +}
     
    
    • [ ] Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Implementing a try-catch block for error handling during user prompt interaction is a best practice that enhances the test's robustness by gracefully managing exceptions and providing informative error messages.

    8

    qodo-code-review[bot] avatar Sep 11 '24 19:09 qodo-code-review[bot]

    Still waiting when we break nested types here https://github.com/SeleniumHQ/selenium/issues/15407

    nvborisenko avatar Mar 22 '25 10:03 nvborisenko

    @nvborisenko, what needs to be done in this PR to have it ready?

    diemol avatar Aug 17 '25 16:08 diemol

    Find a motivation, and then: image

    nvborisenko avatar Aug 17 '25 18:08 nvborisenko