diff --git a/src/Plugin.Maui.UITestHelpers.Appium/AppiumApp.cs b/src/Plugin.Maui.UITestHelpers.Appium/AppiumApp.cs index dfb6b2e..f4d5e9c 100644 --- a/src/Plugin.Maui.UITestHelpers.Appium/AppiumApp.cs +++ b/src/Plugin.Maui.UITestHelpers.Appium/AppiumApp.cs @@ -150,7 +150,7 @@ protected static void SetGeneralAppiumOptions(IConfig config, AppiumOptions appi appiumOptions.AddAdditionalAppiumOption(MobileCapabilityType.NewCommandTimeout, 3000); } - public void Dispose() + public virtual void Dispose() { _driver?.Dispose(); } diff --git a/src/Plugin.Maui.UITestHelpers.Appium/AppiumCatalystApp.cs b/src/Plugin.Maui.UITestHelpers.Appium/AppiumCatalystApp.cs index 4b41939..8a3cc96 100644 --- a/src/Plugin.Maui.UITestHelpers.Appium/AppiumCatalystApp.cs +++ b/src/Plugin.Maui.UITestHelpers.Appium/AppiumCatalystApp.cs @@ -39,6 +39,44 @@ 3 or } } + public override void Dispose() + { + try + { + // Terminate the app before disposing the driver to ensure proper cleanup on macOS + var appId = Config.GetProperty("AppId"); + if (!string.IsNullOrWhiteSpace(appId) && _driver != null) + { + try + { + _driver.ExecuteScript("macos: terminateApp", new Dictionary + { + { "bundleId", appId }, + }); + } + catch + { + // Ignore errors during app termination as the app may already be closed + } + + // Explicitly quit the driver session to ensure proper cleanup + try + { + _driver.Quit(); + } + catch + { + // Ignore errors during quit as the session may already be terminated + } + } + } + finally + { + // Call the base dispose to handle standard cleanup + base.Dispose(); + } + } + private static AppiumOptions GetOptions(IConfig config) { config.SetProperty("PlatformName", "mac");