@@ -45,6 +45,7 @@ public abstract class ChromiumDriver : RemoteWebDriver, ISupportsLogs, IDevTools
45
45
private const string SendChromeCommandWithResult = "sendChromeCommandWithResult" ;
46
46
47
47
private readonly string optionsCapabilityName ;
48
+ private DevToolsSession devToolsSession ;
48
49
49
50
/// <summary>
50
51
/// Initializes a new instance of the <see cref="ChromiumDriver"/> class using the specified
@@ -169,7 +170,7 @@ public object ExecuteChromeCommandWithResult(string commandName, Dictionary<stri
169
170
/// </summary>
170
171
/// <param name="devToolsProtocolVersion">The version of the Chromium Developer Tools protocol to use. Defaults to autodetect the protocol version.</param>
171
172
/// <returns>The active session to use to communicate with the Chromium Developer Tools debugging protocol.</returns>
172
- public DevToolsSession CreateDevToolsSession ( )
173
+ public DevToolsSession GetDevToolsSession ( )
173
174
{
174
175
return CreateDevToolsSession ( DevToolsSession . AutoDetectDevToolsProtocolVersion ) ;
175
176
}
@@ -181,33 +182,51 @@ public DevToolsSession CreateDevToolsSession()
181
182
/// <returns>The active session to use to communicate with the Chromium Developer Tools debugging protocol.</returns>
182
183
public DevToolsSession CreateDevToolsSession ( int devToolsProtocolVersion )
183
184
{
184
- if ( ! this . Capabilities . HasCapability ( this . optionsCapabilityName ) )
185
+ if ( this . devToolsSession == null )
185
186
{
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
+ }
188
191
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
+ }
194
197
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
+ }
199
202
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
+ }
206
214
}
207
- catch ( Exception e )
215
+
216
+ return this . devToolsSession ;
217
+ }
218
+
219
+ protected override void Dispose ( bool disposing )
220
+ {
221
+ if ( disposing )
208
222
{
209
- throw new WebDriverException ( "Unexpected error creating WebSocket DevTools session." , e ) ;
223
+ if ( this . devToolsSession != null )
224
+ {
225
+ this . devToolsSession . Dispose ( ) ;
226
+ }
210
227
}
228
+
229
+ base . Dispose ( disposing ) ;
211
230
}
212
231
213
232
private static ICapabilities ConvertOptionsToCapabilities ( ChromiumOptions options )
0 commit comments