Skip to content

Commit

Permalink
[dotnet] Rename TerminateDevToolsSession to ResetDevToolsSession
Browse files Browse the repository at this point in the history
For the case where a CDP session is made invalid, for example, because a
window is closed, this method will now reset the CDP session so that the
user can continue to use it without closing and establishing a new web
socket connection.

Also added disposal of DevTools sessions to Firefox and Remote when
required.
  • Loading branch information
jimevans committed Sep 22, 2021
1 parent 5a6504a commit 702b1c7
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 42 deletions.
9 changes: 6 additions & 3 deletions dotnet/src/webdriver/Chromium/ChromiumDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,15 @@ public DevToolsSession GetDevToolsSession(int devToolsProtocolVersion)
return this.devToolsSession;
}

public void TerminateDevToolsSession()
/// <summary>
/// Resets a DevTools session
/// </summary>
public void ResetDevToolsSession()
{
if (this.devToolsSession != null)
{
this.devToolsSession.Dispose();
this.devToolsSession = null;
this.devToolsSession.ActiveSessionId = null;
this.devToolsSession.InitializeSession().ConfigureAwait(false).GetAwaiter().GetResult();
}
}

Expand Down
75 changes: 43 additions & 32 deletions dotnet/src/webdriver/DevTools/DevToolsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,49 @@ public T GetVersionSpecificDomains<T>() where T: DevToolsSessionDomains
/// <summary>
/// Asynchronously starts the session.
/// </summary>
/// <param name="protocolVersion">The version of the protocol to use in communicating with the browswer.</param>
/// <param name="requestedProtocolVersion">The requested version of the protocol to use in communicating with the browswer.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
internal async Task Start(int protocolVersion)
internal async Task Start(int requestedProtocolVersion)
{
int protocolVersion = await InitializeProtocol(requestedProtocolVersion);
this.domains = DevToolsDomains.InitializeDomains(protocolVersion, this);
await this.InitializeSession();
}

internal async Task InitializeSession()
{
string targetId = null;
var targets = await this.domains.Target.GetTargets();
foreach (var target in targets)
{
if (target.Type == "page")
{
targetId = target.TargetId;
LogTrace("Found Target ID {0}.", targetId);
string sessionId = await this.domains.Target.AttachToTarget(targetId);
LogTrace("Target ID {0} attached. Active session ID: {1}", targetId, sessionId);
this.ActiveSessionId = sessionId;
break;
}
}

await this.domains.Target.SetAutoAttach();
LogTrace("AutoAttach is set.", targetId);
try
{
// Wrap this in a try-catch, because it's not the end of the
// world if clearing the log doesn't work.
await this.domains.Log.Clear();
LogTrace("Log cleared.", targetId);
}
catch (WebDriverException)
{
}
}

private async Task<int> InitializeProtocol(int requestedProtocolVersion)
{
int protocolVersion = requestedProtocolVersion;
if (this.websocketAddress == null)
{
string debuggerUrl = string.Format(CultureInfo.InvariantCulture, "http://{0}", this.debuggerEndpoint);
Expand All @@ -266,7 +305,7 @@ internal async Task Start(int protocolVersion)
var versionInfo = JsonConvert.DeserializeObject<DevToolsVersionInfo>(rawVersionInfo);
this.websocketAddress = versionInfo.WebSocketDebuggerUrl;

if (protocolVersion == AutoDetectDevToolsProtocolVersion)
if (requestedProtocolVersion == AutoDetectDevToolsProtocolVersion)
{
bool versionParsed = int.TryParse(versionInfo.BrowserMajorVersion, out protocolVersion);
if (!versionParsed)
Expand All @@ -283,35 +322,7 @@ internal async Task Start(int protocolVersion)
}
}

this.domains = DevToolsDomains.InitializeDomains(protocolVersion, this);

string targetId = null;
var targets = await this.domains.Target.GetTargets();
foreach(var target in targets)
{
if (target.Type == "page")
{
targetId = target.TargetId;
LogTrace("Found Target ID {0}.", targetId);
string sessionId = await this.domains.Target.AttachToTarget(targetId);
LogTrace("Target ID {0} attached. Active session ID: {1}", targetId, sessionId);
this.ActiveSessionId = sessionId;
break;
}
}

await this.domains.Target.SetAutoAttach();
LogTrace("AutoAttach is set.", targetId);
try
{
// Wrap this in a try-catch, because it's not the end of the
// world if clearing the log doesn't work.
await this.domains.Log.Clear();
LogTrace("Log cleared.", targetId);
}
catch (WebDriverException)
{
}
return protocolVersion;
}

private async Task OpenSessionConnection(CancellationToken cancellationToken)
Expand Down
5 changes: 4 additions & 1 deletion dotnet/src/webdriver/DevTools/IDevTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public interface IDevTools
/// <returns>The active session to use to communicate with the Developer Tools debugging protocol.</returns>
DevToolsSession GetDevToolsSession(int protocolVersion);

void TerminateDevToolsSession();
/// <summary>
/// Resets a DevTools session
/// </summary>
void ResetDevToolsSession();
}
}
22 changes: 19 additions & 3 deletions dotnet/src/webdriver/Firefox/FirefoxDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,15 @@ public DevToolsSession GetDevToolsSession(int devToolsProtocolVersion)
return this.devToolsSession;
}

public void TerminateDevToolsSession()
/// <summary>
/// Resets a DevTools session
/// </summary>
public void ResetDevToolsSession()
{
if (this.devToolsSession != null)
{
this.devToolsSession.Dispose();
this.devToolsSession = null;
this.devToolsSession.ActiveSessionId = null;
this.devToolsSession.InitializeSession().ConfigureAwait(false).GetAwaiter().GetResult();
}
}

Expand All @@ -317,6 +320,19 @@ protected virtual void PrepareEnvironment()
// Does nothing, but provides a hook for subclasses to do "stuff"
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (this.devToolsSession != null)
{
this.devToolsSession.Dispose();
}
}

base.Dispose(disposing);
}

private static ICapabilities ConvertOptionsToCapabilities(FirefoxOptions options)
{
if (options == null)
Expand Down
22 changes: 19 additions & 3 deletions dotnet/src/webdriver/Remote/RemoteWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,13 +444,29 @@ public DevToolsSession GetDevToolsSession(int protocolVersion)
return this.devToolsSession;
}

public void TerminateDevToolsSession()
/// <summary>
/// Resets a DevTools session
/// </summary>
public void ResetDevToolsSession()
{
if (this.devToolsSession != null)
{
this.devToolsSession.Dispose();
this.devToolsSession = null;
this.devToolsSession.ActiveSessionId = null;
this.devToolsSession.InitializeSession().ConfigureAwait(false).GetAwaiter().GetResult();
}
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (this.devToolsSession != null)
{
this.devToolsSession.Dispose();
}
}

base.Dispose(disposing);
}

private static ICapabilities ConvertOptionsToCapabilities(DriverOptions options)
Expand Down

0 comments on commit 702b1c7

Please sign in to comment.