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

[🐛 Bug]: dotnet | DevTools | closing tab leaves broken DevToolsSession behind. Cannot be repaired/freshly created #13755

Closed
schrufygroovy opened this issue Mar 28, 2024 · 5 comments

Comments

@schrufygroovy
Copy link
Contributor

schrufygroovy commented Mar 28, 2024

What happened?

Problem:

General How to reproduce:

  • create a devToolsSession
  • create a new tab
  • close the old tab
  • try to do any command (it does not matter if you create new session (GetDevToolsSession) or close old session manually (CloseDevToolsSession))

Expected:

  • either it's possible to create new DevToolsSession that works OR keep old DevToolsSession working

Actual:

  • errors like:
CommandResponseException: Console.enable: 'Console.enable' wasn't found

I am able to reproduce in trunk of selenium. I have attached the test code.

Additional Info:
I can see in the logs that the autoattachment procedures seems to work fine:

Target ID BE926206F1A346DE69E40BF5D85B3B66 attached. Active session ID: 92CCDA539F878DCD130A8458A4D4381D

but for some reason the sessionId is not sent with the command.

Maybe it is related to this commit (LINE211): f7fd6d3#diff-0beb90c52c9357d5c8b3d1b68279e46971b026839cb7122bb29c6022776bff56L211

where some sessionId was replacing this.ActiveSessionId, but the line above:

await this.InitializeSession().ConfigureAwait(false);

is potentially updating the this.ActiveSessionId. So I guess this mechanism was partially broken.

I have also created a fork and branch that contains the test + a potential fix: trunk...schrufygroovy:selenium:fix-devtools-tab-closing

How can we reproduce the issue?

using NUnit.Framework;
using System.Threading.Tasks;

namespace OpenQA.Selenium.DevTools
{
    using CurrentCdpVersion = V123;

    [TestFixture]
    public class DevToolsTabsTest : DevToolsTestFixture
    {

        [Test]
        [IgnoreBrowser(Selenium.Browser.IE, "IE does not support Chrome DevTools Protocol")]
        [IgnoreBrowser(Selenium.Browser.Firefox, "Firefox does not support Chrome DevTools Protocol")]
        [IgnoreBrowser(Selenium.Browser.Safari, "Safari does not support Chrome DevTools Protocol")]
        public async Task ClosingTabDoesNotBreakDevToolsSession()
        {
            var domains = session.GetVersionSpecificDomains<CurrentCdpVersion.DevToolsSessionDomains>();
            await domains.Console.Enable();
            var oldWindowHandle = driver.CurrentWindowHandle;
            driver.SwitchTo().NewWindow(WindowType.Tab);
            driver.SwitchTo().Window(oldWindowHandle);
            driver.Close();
            Assert.That(
                async () => {
                    await domains.Console.Enable();
                },
                Throws.Nothing
            );
        }

        [Test]
        [IgnoreBrowser(Selenium.Browser.IE, "IE does not support Chrome DevTools Protocol")]
        [IgnoreBrowser(Selenium.Browser.Firefox, "Firefox does not support Chrome DevTools Protocol")]
        [IgnoreBrowser(Selenium.Browser.Safari, "Safari does not support Chrome DevTools Protocol")]
        public async Task ClosingTabDoesNotBreakDevToolsSession_WithManualClosing()
        {
            var newSession = devTools.GetDevToolsSession(new DevToolsOptions() { WaitForDebuggerOnStart = true });
            var domains = newSession.GetVersionSpecificDomains<CurrentCdpVersion.DevToolsSessionDomains>();
            await domains.Console.Enable();
            var oldWindowHandle = driver.CurrentWindowHandle;
            driver.SwitchTo().NewWindow(WindowType.Tab);
            driver.SwitchTo().Window(oldWindowHandle);
            devTools.CloseDevToolsSession();
            driver.Close();
            var newSession2 = devTools.GetDevToolsSession(new DevToolsOptions() { WaitForDebuggerOnStart = true });
            var newDomains2 = newSession2.GetVersionSpecificDomains<CurrentCdpVersion.DevToolsSessionDomains>();
            Assert.That(
                async () => {
                    await newDomains2.Console.Enable();
                },
                Throws.Nothing
            );
        }

        [Test]
        [IgnoreBrowser(Selenium.Browser.IE, "IE does not support Chrome DevTools Protocol")]
        [IgnoreBrowser(Selenium.Browser.Firefox, "Firefox does not support Chrome DevTools Protocol")]
        [IgnoreBrowser(Selenium.Browser.Safari, "Safari does not support Chrome DevTools Protocol")]
        public async Task ClosingTabDoesNotBreakDevToolsSession_WithAutoAttaching()
        {
            var newSession = devTools.GetDevToolsSession(new DevToolsOptions() { WaitForDebuggerOnStart = true });
            var domains = newSession.GetVersionSpecificDomains<CurrentCdpVersion.DevToolsSessionDomains>();
            await domains.Console.Enable();
            var oldWindowHandle = driver.CurrentWindowHandle;
            driver.SwitchTo().NewWindow(WindowType.Tab);
            driver.SwitchTo().Window(oldWindowHandle);
            driver.Close();
            var newSession2 = devTools.GetDevToolsSession(new DevToolsOptions() { WaitForDebuggerOnStart = true });
            var newDomains2 = newSession2.GetVersionSpecificDomains<CurrentCdpVersion.DevToolsSessionDomains>();
            Assert.That(
                async () => {
                    await newDomains2.Console.Enable();
                },
                Throws.Nothing
            );
        }
    }
}


### Relevant log output

```shell
<OpenQA.Selenium.DevTools.CommandResponseException: Console.enable: 'Console.enable' wasn't found
   at OpenQA.Selenium.DevTools.DevToolsSession.SendCommand(String commandName, String sessionId, JToken commandParameters, CancellationToken cancellationToken, Nullable`1 millisecondsTimeout, Boolean throwExceptionIfResponseNotReceived) in C:\src\selenium\dotnet\src\webdriver\DevTools\DevToolsSession.cs:line 302
   at OpenQA.Selenium.DevTools.DevToolsSession.SendCommand[TCommand,TCommandResponse](TCommand command, CancellationToken cancellationToken, Nullable`1 millisecondsTimeout, Boolean throwExceptionIfResponseNotReceived) in C:\src\selenium\dotnet\src\webdriver\DevTools\DevToolsSession.cs:line 222

Operating System

Windows 11

Selenium version

4.19.0

What are the browser(s) and version(s) where you see this issue?

Chrome 123

What are the browser driver(s) and version(s) where you see this issue?

ChromeDriver 123

Are you using Selenium Grid?

No response

Copy link

@schrufygroovy, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

@diemol
Copy link
Member

diemol commented Mar 29, 2024

@EdwinVanVliet @nvborisenko do you folks think the change brought this issue?

@nvborisenko
Copy link
Member

It is regression issue, appeared in v4.17, and related to mentioned changes. @schrufygroovy thanks for detailed report.

@nvborisenko
Copy link
Member

@schrufygroovy your thoughts are correct. When tab is closed we are notified and we set ActiveSessionId to null, which is indicator for us that session should be re-initialized before sending any further commands. But we should re-initialize it earlier in SendCommand method which utilizes ActiveSessionId.

Would you mind to create a PR for us, including your unit tests?

schrufygroovy added a commit to schrufygroovy/selenium that referenced this issue Apr 3, 2024
Fix regression issue, appeared in v4.17. Closing a tab breaks the devtools session

Fixes SeleniumHQ#13755
schrufygroovy added a commit to schrufygroovy/selenium that referenced this issue Apr 3, 2024
Fix regression issue, appeared in v4.17. Closing a tab breaks the devtools session

Fixes SeleniumHQ#13755
nvborisenko pushed a commit to schrufygroovy/selenium that referenced this issue Apr 5, 2024
Fix regression issue, appeared in v4.17. Closing a tab breaks the devtools session

Fixes SeleniumHQ#13755
Copy link

github-actions bot commented May 5, 2024

This issue has been automatically locked since there has not been any recent activity since it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked and limited conversation to collaborators May 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants