From 12ed6cc2d94295c792c84c1315edab2571a60d5c Mon Sep 17 00:00:00 2001 From: schrufygroovy <50398024+schrufygroovy@users.noreply.github.com> Date: Fri, 5 Apr 2024 23:22:59 +0200 Subject: [PATCH] [dotnet] use correct devtools session id after reinitialization (#13768) Fix regression issue, appeared in v4.17. Closing a tab breaks the devtools session Fixes #13755 --- .../src/webdriver/DevTools/DevToolsSession.cs | 16 +++++----- .../test/common/DevTools/DevToolsTabsTest.cs | 32 +++++++++++++++++++ 2 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 dotnet/test/common/DevTools/DevToolsTabsTest.cs diff --git a/dotnet/src/webdriver/DevTools/DevToolsSession.cs b/dotnet/src/webdriver/DevTools/DevToolsSession.cs index 45ecd2e6e86e3..cb84f28904fe4 100644 --- a/dotnet/src/webdriver/DevTools/DevToolsSession.cs +++ b/dotnet/src/webdriver/DevTools/DevToolsSession.cs @@ -239,9 +239,15 @@ public T GetVersionSpecificDomains() where T : DevToolsSessionDomains /// to throw an exception if a response is not received; otherwise, . /// The command response object implementing the interface. //[DebuggerStepThrough] - public Task SendCommand(string commandName, JToken commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true) + public async Task SendCommand(string commandName, JToken commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true) { - return SendCommand(commandName, ActiveSessionId, commandParameters, cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived); + if (this.attachedTargetId == null) + { + LogTrace("Session not currently attached to a target; reattaching"); + await this.InitializeSession().ConfigureAwait(false); + } + + return await SendCommand(commandName, this.ActiveSessionId, commandParameters, cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived); } /// @@ -262,12 +268,6 @@ public T GetVersionSpecificDomains() where T : DevToolsSessionDomains millisecondsTimeout = Convert.ToInt32(CommandTimeout.TotalMilliseconds); } - if (this.attachedTargetId == null) - { - LogTrace("Session not currently attached to a target; reattaching"); - await this.InitializeSession().ConfigureAwait(false); - } - var message = new DevToolsCommandData(Interlocked.Increment(ref this.currentCommandId), sessionId, commandName, commandParameters); if (this.connection != null && this.connection.IsActive) diff --git a/dotnet/test/common/DevTools/DevToolsTabsTest.cs b/dotnet/test/common/DevTools/DevToolsTabsTest.cs new file mode 100644 index 0000000000000..84f3b0673c355 --- /dev/null +++ b/dotnet/test/common/DevTools/DevToolsTabsTest.cs @@ -0,0 +1,32 @@ +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(); + 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 + ); + } + } +}