From 4c0ac3d932fc877aa5989a08f8da6791402b7514 Mon Sep 17 00:00:00 2001 From: MASACR99 Date: Fri, 15 Mar 2024 18:48:29 +0100 Subject: [PATCH] [dotnet] Add dispose on constructor failure to ensure driver closes (#13673) * Forced Dispose of driver on chromedriver exception * Changed try-catch to only envelop StartSession and moved driver Quit command to own function * Remove extra spaces * Undo DriverDispose changes and instead call quit --------- Co-authored-by: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> --- dotnet/src/webdriver/WebDriver.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/WebDriver.cs b/dotnet/src/webdriver/WebDriver.cs index fdf1b6127c34c..4e9097427f007 100644 --- a/dotnet/src/webdriver/WebDriver.cs +++ b/dotnet/src/webdriver/WebDriver.cs @@ -54,7 +54,18 @@ public class WebDriver : IWebDriver, ISearchContext, IJavaScriptExecutor, IFinds protected WebDriver(ICommandExecutor executor, ICapabilities capabilities) { this.executor = executor; - this.StartSession(capabilities); + + try + { + this.StartSession(capabilities); + } + catch (Exception) + { + // Failed to start driver session, disposing of driver + this.Quit(); + throw; + } + this.elementFactory = new WebElementFactory(this); this.network = new NetworkManager(this); this.registeredCommands.AddRange(DriverCommand.KnownCommands); @@ -692,7 +703,6 @@ protected virtual void Dispose(bool disposing) { this.sessionId = null; } - this.executor.Dispose(); }