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] Propagate async from navigation to tests #15308

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

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Feb 19, 2025

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Propagates async from navigation, with the exception of NavigationTests.cs (the tests which explicitly validate the sync behavior), and the IWebDriver.Url setter, because there are almost a thousand hits for this...
image

Motivation and Context

Minimizing sync-over-async is pure goodness. Additionally, It is an eventual goal to make Selenium an Async library: #14067

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Enhancement, Tests


Description

  • Transitioned test methods to asynchronous implementations.

  • Replaced synchronous navigation calls with asynchronous equivalents.

  • Updated test signatures to support async/await patterns.

  • Enhanced test reliability and alignment with modern .NET async practices.


Changes walkthrough 📝

Relevant files
Tests
9 files
CookieImplementationTest.cs
Converted cookie-related tests to async methods.                 
+5/-4     
DevToolsProfilerTest.cs
Updated DevTools profiler tests to use async navigation. 
+1/-1     
ExecutingJavascriptTest.cs
Refactored JavaScript execution tests for async navigation.
+10/-10 
NavigationTest.cs
Transitioned navigation tests to async methods.                   
+3/-3     
NetworkInterceptionTests.cs
Updated network interception tests to async navigation.   
+1/-1     
PageLoadingTest.cs
Refactored page loading tests for async navigation.           
+25/-22 
PrintTest.cs
Updated print tests to initialize with async navigation. 
+3/-2     
SlowLoadingPageTest.cs
Converted slow-loading page tests to async methods.           
+3/-2     
FirefoxDriverTest.cs
Refactored Firefox driver tests for async addon handling.
+11/-10 

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis 🔶

    14067 - Partially compliant

    Compliant requirements:

    • Add Async methods to all classes (for navigation methods)
    • Rewrite Sync methods to use the Async methods (for navigation methods)

    Non-compliant requirements:

    • Write Blog Post about proposed changes
    • Mark all Sync methods as deprecated
    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Async Validation

    Ensure that async operations are properly awaited and that test timeouts are adjusted appropriately for async execution

    private void TestPageLoadTimeoutIsEnforced(long webDriverPageLoadTimeoutInSeconds)
    {
        // Test page will load this many seconds longer than WD pageLoadTimeout.
        long pageLoadTimeBufferInSeconds = 10;
        string slowLoadingPageUrl = EnvironmentManager.Instance.UrlBuilder.WhereIs("sleep?time=" + (webDriverPageLoadTimeoutInSeconds + pageLoadTimeBufferInSeconds));
        driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(webDriverPageLoadTimeoutInSeconds);
        AssertPageLoadTimeoutIsEnforced(async () => await driver.Navigate().GoToUrlAsync(slowLoadingPageUrl), webDriverPageLoadTimeoutInSeconds, pageLoadTimeBufferInSeconds);
    }
    
    private void AssertPageLoadTimeoutIsEnforced(AsyncTestDelegate delegateToTest, long webDriverPageLoadTimeoutInSeconds, long pageLoadTimeBufferInSeconds)
    {
        DateTime start = DateTime.Now;
    
        // TODO in NUnit 4, change this to await Assert.ThatAsync
        Assert.That(delegateToTest, Throws.InstanceOf<WebDriverTimeoutException>(), "I should have timed out after " + webDriverPageLoadTimeoutInSeconds + " seconds");
        DateTime end = DateTime.Now;
        TimeSpan duration = end - start;
        Assert.That(duration.TotalSeconds, Is.GreaterThan(webDriverPageLoadTimeoutInSeconds));

    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 19, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Learned
    best practice
    Add null validation checks for required method parameters to prevent NullReferenceExceptions

    The handler parameter passed to networkInterceptor.AddResponseHandler() should
    be validated for null before use since it's a required parameter that could be
    null. Add a null check using ArgumentNullException.ThrowIfNull().

    dotnet/test/common/NetworkInterceptionTests.cs [101]

    +ArgumentNullException.ThrowIfNull(handler, nameof(handler));
     networkInterceptor.AddResponseHandler(handler);
    • Apply this suggestion
    Suggestion importance[1-10]: 6
    Low
    • Update

    @@ -359,7 +360,7 @@ public void ShouldTimeoutIfAPageTakesTooLongToLoadAfterClick()

    try
    {
    AssertPageLoadTimeoutIsEnforced(() => link.Click(), 2, 3);
    AssertPageLoadTimeoutIsEnforced(async () => link.Click(), 2, 3);
    Copy link
    Contributor Author

    Choose a reason for hiding this comment

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

    This will give a warning, but there is no ClickAsync yet. I can add more code to clean it up, but is it worth it?

    Copy link
    Member

    @nvborisenko nvborisenko left a comment

    Choose a reason for hiding this comment

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

    Sync api is still under focus.

    @RenderMichael RenderMichael marked this pull request as draft February 19, 2025 21:10
    @RenderMichael
    Copy link
    Contributor Author

    This PR can be a draft until v5 work begins; hopefully this PR is not stake by then.

    @nvborisenko
    Copy link
    Member

    Instead of staling, I am thinking to create some "next-generation" branch, where we can put our work for v5. Questions:

    • how to maintain (resolving conflicts)
    • decision what should be included into v5

    @RenderMichael
    Copy link
    Contributor Author

    I think there is no difference between keeping this PR from being stale, and keeping a “next-generation” branch from being stale.

    If we want to do v5 work already, we can have feature flags. We can keep trunk backwards-compatible while doing all kinds of work. Many new features (like async APIs) don’t need a feature flag, they can just be internal.

    I like the idea of doing v5 work, but without a rough estimate on when v5 is coming (6 months? 3 years?) it may not be worth it to start now.

    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.

    2 participants