@@ -223,6 +223,17 @@ public void Dispose()
223223 GC . SuppressFinalize ( this ) ;
224224 }
225225
226+ /// <summary>
227+ /// Asynchronously disposes the WebDriver Instance
228+ /// </summary>
229+ /// <returns>A task representing the asynchronous dispose operation.</returns>
230+ public async ValueTask DisposeAsync ( )
231+ {
232+ await this . DisposeAsyncCore ( ) . ConfigureAwait ( false ) ;
233+ this . Dispose ( false ) ;
234+ GC . SuppressFinalize ( this ) ;
235+ }
236+
226237 /// <summary>
227238 /// Executes JavaScript "asynchronously" in the context of the currently selected frame or window,
228239 /// executing the callback function specified as the last argument in the list of arguments.
@@ -672,25 +683,60 @@ protected bool RegisterInternalDriverCommand(string commandName, [NotNullWhen(tr
672683 /// <param name="disposing">if its in the process of disposing</param>
673684 protected virtual void Dispose ( bool disposing )
674685 {
675- try
686+ if ( disposing )
676687 {
677688 if ( this . SessionId is not null )
678689 {
679- this . Execute ( DriverCommand . Quit , null ) ;
690+ try
691+ {
692+
693+ this . Execute ( DriverCommand . Quit , null ) ;
694+
695+ }
696+ catch ( NotImplementedException )
697+ {
698+ }
699+ catch ( InvalidOperationException )
700+ {
701+ }
702+ catch ( WebDriverException )
703+ {
704+ }
705+ finally
706+ {
707+ this . SessionId = null ! ;
708+ }
680709 }
710+
711+ this . CommandExecutor . Dispose ( ) ;
681712 }
682- catch ( NotImplementedException )
683- {
684- }
685- catch ( InvalidOperationException )
686- {
687- }
688- catch ( WebDriverException )
689- {
690- }
691- finally
713+ }
714+
715+ /// <summary>
716+ /// Asynchronously performs the core dispose logic.
717+ /// </summary>
718+ /// <returns>A task representing the asynchronous dispose operation.</returns>
719+ protected virtual async ValueTask DisposeAsyncCore ( )
720+ {
721+ if ( this . SessionId is not null )
692722 {
693- this . SessionId = null ! ;
723+ try
724+ {
725+ await this . ExecuteAsync ( DriverCommand . Quit , null ) . ConfigureAwait ( false ) ;
726+ }
727+ catch ( NotImplementedException )
728+ {
729+ }
730+ catch ( InvalidOperationException )
731+ {
732+ }
733+ catch ( WebDriverException )
734+ {
735+ }
736+ finally
737+ {
738+ this . SessionId = null ! ;
739+ }
694740 }
695741
696742 this . CommandExecutor . Dispose ( ) ;
0 commit comments