Skip to content

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

@schrufygroovy

Description

@schrufygroovy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-dotnet.NET BindingsI-defectSomething is not working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions