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] use correct devtools session id after reinitialization #13768

Conversation

schrufygroovy
Copy link
Contributor

@schrufygroovy schrufygroovy commented Apr 3, 2024

User description

Description

After reinitialization of the dev tools session, that new active session id should be used.

Motivation and Context

Fixes regression issue: #13755

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)
    PLEASE double check: in case a sessionId is provided to SendCommand() I removed the check if attachedTargetId == null and the reinitialization of the session. My assumption was in case the sessionId is given, we don't need a session re-initialization.

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.

Type

bug_fix, tests


Description


Changes walkthrough

Relevant files
Enhancement
DevToolsSession.cs
Improve DevTools Session Management in Selenium WebDriver

dotnet/src/webdriver/DevTools/DevToolsSession.cs

  • Modified SendCommand to reattach session if attachedTargetId is null
    before sending a command.
  • Removed redundant session reattachment logic from overloaded
    SendCommand.
  • +8/-8     
    Tests
    DevToolsTabsTest.cs
    Add Test for Verifying DevTools Session After Closing Tab

    dotnet/test/common/DevTools/DevToolsTabsTest.cs

  • Added a new test ClosingTabDoesNotBreakDevToolsSession to verify
    DevTools session remains active after closing a tab.
  • +32/-0   

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

    @CLAassistant
    Copy link

    CLAassistant commented Apr 3, 2024

    CLA assistant check
    All committers have signed the CLA.

    Copy link
    Contributor

    qodo-merge-pro bot commented Apr 3, 2024

    PR Description updated to latest commit (bacefcc)

    Copy link
    Contributor

    qodo-merge-pro bot commented Apr 3, 2024

    PR Review

    ⏱️ Estimated effort to review [1-5]

    2, because the changes are focused and well-scoped, impacting DevTools session management and adding a specific test case. The logic is straightforward, and the added test directly addresses the regression issue.

    🧪 Relevant tests

    Yes

    🔍 Possible issues

    Possible Regression: Removing the check for attachedTargetId == null in the SendCommand overload that accepts a sessionId might introduce regressions if there are scenarios where reinitialization was necessary despite having a sessionId. It's important to ensure that all use cases have been considered and that this change doesn't inadvertently affect existing functionality.

    🔒 Security concerns

    No


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    Copy link
    Contributor

    qodo-merge-pro bot commented Apr 3, 2024

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Enhancement
    Add a check for an active connection before reinitializing the session.

    Consider checking if this.connection is not null and active before attempting to reattach
    the session in the SendCommand method. This can prevent unnecessary reinitialization if
    the connection is already active.

    dotnet/src/webdriver/DevTools/DevToolsSession.cs [244-247]

    -if (this.attachedTargetId == null)
    +if (this.attachedTargetId == null && (this.connection == null || !this.connection.IsActive))
     {
         LogTrace("Session not currently attached to a target; reattaching");
         await this.InitializeSession().ConfigureAwait(false);
     }
     
    Simplify the method signature by omitting explicit default value for CancellationToken.

    Avoid using default(CancellationToken) explicitly, as it is the default value for optional
    CancellationToken parameters. Simplify the method signature by omitting it.

    dotnet/src/webdriver/DevTools/DevToolsSession.cs [242]

    -public async Task<JToken> SendCommand(string commandName, JToken commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
    +public async Task<JToken> SendCommand(string commandName, JToken commandParameters, CancellationToken cancellationToken = default, int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
     
    Possible issue
    Rename the overloaded method to prevent potential stack overflow.

    To avoid potential stack overflow errors, consider renaming the overloaded SendCommand
    method to something more descriptive and to differentiate it from the recursive call.

    dotnet/src/webdriver/DevTools/DevToolsSession.cs [242]

    -public async Task<JToken> SendCommand(string commandName, JToken commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
    +public async Task<JToken> SendCommandWithSessionCheck(string commandName, JToken commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
     
    Best practice
    Use Assert.DoesNotThrowAsync for clearer assertion of non-throwing async operations.

    Consider using Assert.DoesNotThrowAsync instead of Assert.That with Throws.Nothing for
    better readability and intent clarity when asserting that an async operation does not
    throw an exception.

    dotnet/test/common/DevTools/DevToolsTabsTest.cs [24-28]

    -Assert.That(
    -    async () => {
    -        await domains.Console.Enable();
    -    },
    -    Throws.Nothing
    +Assert.DoesNotThrowAsync(
    +    async () => await domains.Console.Enable()
     );
     
    Maintainability
    Use nameof for parameter names in logs for better maintainability.

    Use nameof for parameter names in exceptions and logs to ensure refactoring tools update
    them automatically and to avoid potential typos.

    dotnet/src/webdriver/DevTools/DevToolsSession.cs [246]

    -LogTrace("Session not currently attached to a target; reattaching");
    +LogTrace($"{nameof(this.attachedTargetId)} not currently attached to a target; reattaching");
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    @schrufygroovy schrufygroovy force-pushed the fix-devtools-dealing-with-closing-tab branch from bacefcc to e306d50 Compare April 3, 2024 07:57
    Fix regression issue, appeared in v4.17. Closing a tab breaks the devtools session
    
    Fixes SeleniumHQ#13755
    @nvborisenko nvborisenko force-pushed the fix-devtools-dealing-with-closing-tab branch from e306d50 to c23158c Compare April 5, 2024 19:45
    @nvborisenko nvborisenko self-requested a review April 5, 2024 20:03
    @nvborisenko
    Copy link
    Member

    CI failure is not related to the changes in this PR, merging it.

    Thanks @schrufygroovy for your contribution!

    @nvborisenko nvborisenko merged commit 12ed6cc into SeleniumHQ:trunk Apr 5, 2024
    9 of 11 checks passed
    @p0deje
    Copy link
    Member

    p0deje commented Apr 6, 2024

    @schrufygroovy Could you please sign the contributor license agreement via https://cla-assistant.io/SeleniumHQ/selenium?pullRequest=13768?

    @schrufygroovy
    Copy link
    Contributor Author

    @schrufygroovy Could you please sign the contributor license agreement via https://cla-assistant.io/SeleniumHQ/selenium?pullRequest=13768?

    done

    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.

    4 participants