Skip to content

Commit a46a9ac

Browse files
committed
Updating .NET DevTools code to persist DevTools session once created
1 parent 8be1107 commit a46a9ac

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

dotnet/src/webdriver/Chromium/ChromiumDriver.cs

+40-21
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public abstract class ChromiumDriver : RemoteWebDriver, ISupportsLogs, IDevTools
4545
private const string SendChromeCommandWithResult = "sendChromeCommandWithResult";
4646

4747
private readonly string optionsCapabilityName;
48+
private DevToolsSession devToolsSession;
4849

4950
/// <summary>
5051
/// Initializes a new instance of the <see cref="ChromiumDriver"/> class using the specified
@@ -169,7 +170,7 @@ public object ExecuteChromeCommandWithResult(string commandName, Dictionary<stri
169170
/// </summary>
170171
/// <param name="devToolsProtocolVersion">The version of the Chromium Developer Tools protocol to use. Defaults to autodetect the protocol version.</param>
171172
/// <returns>The active session to use to communicate with the Chromium Developer Tools debugging protocol.</returns>
172-
public DevToolsSession CreateDevToolsSession()
173+
public DevToolsSession GetDevToolsSession()
173174
{
174175
return CreateDevToolsSession(DevToolsSession.AutoDetectDevToolsProtocolVersion);
175176
}
@@ -181,33 +182,51 @@ public DevToolsSession CreateDevToolsSession()
181182
/// <returns>The active session to use to communicate with the Chromium Developer Tools debugging protocol.</returns>
182183
public DevToolsSession CreateDevToolsSession(int devToolsProtocolVersion)
183184
{
184-
if (!this.Capabilities.HasCapability(this.optionsCapabilityName))
185+
if (this.devToolsSession == null)
185186
{
186-
throw new WebDriverException("Cannot find " + this.optionsCapabilityName + " capability for driver");
187-
}
187+
if (!this.Capabilities.HasCapability(this.optionsCapabilityName))
188+
{
189+
throw new WebDriverException("Cannot find " + this.optionsCapabilityName + " capability for driver");
190+
}
188191

189-
Dictionary<string, object> options = this.Capabilities.GetCapability(this.optionsCapabilityName) as Dictionary<string, object>;
190-
if (options == null)
191-
{
192-
throw new WebDriverException("Found " + this.optionsCapabilityName + " capability, but is not an object");
193-
}
192+
Dictionary<string, object> options = this.Capabilities.GetCapability(this.optionsCapabilityName) as Dictionary<string, object>;
193+
if (options == null)
194+
{
195+
throw new WebDriverException("Found " + this.optionsCapabilityName + " capability, but is not an object");
196+
}
194197

195-
if (!options.ContainsKey("debuggerAddress"))
196-
{
197-
throw new WebDriverException("Did not find debuggerAddress capability in " + this.optionsCapabilityName);
198-
}
198+
if (!options.ContainsKey("debuggerAddress"))
199+
{
200+
throw new WebDriverException("Did not find debuggerAddress capability in " + this.optionsCapabilityName);
201+
}
199202

200-
string debuggerAddress = options["debuggerAddress"].ToString();
201-
try
202-
{
203-
DevToolsSession session = new DevToolsSession(debuggerAddress);
204-
session.Start(devToolsProtocolVersion).ConfigureAwait(false).GetAwaiter().GetResult();
205-
return session;
203+
string debuggerAddress = options["debuggerAddress"].ToString();
204+
try
205+
{
206+
DevToolsSession session = new DevToolsSession(debuggerAddress);
207+
session.Start(devToolsProtocolVersion).ConfigureAwait(false).GetAwaiter().GetResult();
208+
this.devToolsSession = session;
209+
}
210+
catch (Exception e)
211+
{
212+
throw new WebDriverException("Unexpected error creating WebSocket DevTools session.", e);
213+
}
206214
}
207-
catch (Exception e)
215+
216+
return this.devToolsSession;
217+
}
218+
219+
protected override void Dispose(bool disposing)
220+
{
221+
if (disposing)
208222
{
209-
throw new WebDriverException("Unexpected error creating WebSocket DevTools session.", e);
223+
if (this.devToolsSession != null)
224+
{
225+
this.devToolsSession.Dispose();
226+
}
210227
}
228+
229+
base.Dispose(disposing);
211230
}
212231

213232
private static ICapabilities ConvertOptionsToCapabilities(ChromiumOptions options)

dotnet/src/webdriver/DevTools/IDevTools.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ public interface IDevTools
2727
/// Creates a session to communicate with a browser using a Developer Tools debugging protocol.
2828
/// </summary>
2929
/// <returns>The active session to use to communicate with the Developer Tools debugging protocol.</returns>
30-
DevToolsSession CreateDevToolsSession();
30+
DevToolsSession GetDevToolsSession();
31+
32+
/// <summary>
33+
/// Creates a session to communicate with a browser using a specific version of the Developer Tools debugging protocol.
34+
/// </summary>
35+
/// <param name="protocolVersion">The specific version of the Developer Tools debugging protocol to use.</param>
36+
/// <returns>The active session to use to communicate with the Developer Tools debugging protocol.</returns>
37+
DevToolsSession GetDevToolsSession(int protocolVersion);
3138
}
3239
}

0 commit comments

Comments
 (0)