Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Add examples for BiDi W3C Browsing Context #1940

Draft
wants to merge 14 commits into
base: trunk
Choose a base branch
from

Conversation

nvborisenko
Copy link
Member

@nvborisenko nvborisenko commented Sep 11, 2024

User description

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

TODO:

  • 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

    Copy link

    netlify bot commented Sep 11, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit c3d85b9

    Copy link
    Contributor

    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.

    Copy link
    Contributor

    codiumai-pr-agent-pro bot commented Sep 11, 2024

    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


    var element = (await context.LocateNodesAsync(Locator.Css("#checky")))[0];

    //TODO: ShareId is a type, not string
    Copy link
    Member Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Then we can implicitly convert RemoteNodeValue (if I remember correctly) to SharedId.

    await context.CaptureScreenshotAsync(new() { Clip = new ElementClipRectangle(element) });

    And moving forfard:

    await context.CaptureScreenshotAsync(new() { Clip = ClipRectangle.Element(element) });

    {
    var context1 = await driver.AsBidirectionalContextAsync();

    var context2 = await context1.BiDi.CreateContextAsync(ContextType.Tab, new() { ReferenceContext = context1 });
    Copy link
    Member Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Would be great if context1 can expose CreateContextAsync method, which implicitly sets ReferenceContext to the context1

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant