Skip to content

Commit

Permalink
[dotnet] use correct devtools session id after reinitialization
Browse files Browse the repository at this point in the history
Fix regression issue, appeared in v4.17. Closing a tab breaks the devtools session

Fixes #13755
  • Loading branch information
schrufygroovy authored and nvborisenko committed Apr 5, 2024
1 parent 38829c7 commit c23158c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
16 changes: 8 additions & 8 deletions dotnet/src/webdriver/DevTools/DevToolsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,15 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
/// <param name="throwExceptionIfResponseNotReceived"><see langword="true"/> to throw an exception if a response is not received; otherwise, <see langword="false"/>.</param>
/// <returns>The command response object implementing the <see cref="ICommandResponse{T}"/> interface.</returns>
//[DebuggerStepThrough]
public 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(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);
}

/// <summary>
Expand All @@ -262,12 +268,6 @@ public T GetVersionSpecificDomains<T>() 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)
Expand Down
32 changes: 32 additions & 0 deletions dotnet/test/common/DevTools/DevToolsTabsTest.cs
Original file line number Diff line number Diff line change
@@ -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<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
);
}
}
}

0 comments on commit c23158c

Please sign in to comment.