@@ -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 )
0 commit comments