diff --git a/src/Legerity.Core/AppManager.cs b/src/Legerity.Core/AppManager.cs index be87b46d..5e4fabfd 100644 --- a/src/Legerity.Core/AppManager.cs +++ b/src/Legerity.Core/AppManager.cs @@ -93,7 +93,7 @@ public static class AppManager /// An optional count of retries after a timeout on the wait until condition before accepting the failure. /// /// - /// Thrown if the application is null or the session ID is null once initialized. + /// Thrown if the application is null, the session ID is null once initialized, or the driver fails to configure correctly before returning. /// /// Thrown if the Appium server could not be found when running with or true. /// Thrown if the WinAppDriver could not be found when running with true. @@ -113,104 +113,118 @@ public static RemoteWebDriver StartApp( appiumOpts.Configure(); } - switch (opts) + try { - case WebAppManagerOptions webOpts: + switch (opts) { - app = webOpts.DriverType switch - { - WebAppDriverType.Chrome => new ChromeDriver( - webOpts.DriverUri, - webOpts.DriverOptions as ChromeOptions ?? new ChromeOptions()), - WebAppDriverType.Firefox => new FirefoxDriver( - webOpts.DriverUri, - webOpts.DriverOptions as FirefoxOptions ?? new FirefoxOptions()), - WebAppDriverType.Opera => new OperaDriver( - webOpts.DriverUri, - webOpts.DriverOptions as OperaOptions ?? new OperaOptions()), - WebAppDriverType.Safari => new SafariDriver( - webOpts.DriverUri, - webOpts.DriverOptions as SafariOptions ?? new SafariOptions()), - WebAppDriverType.Edge => new EdgeDriver( - webOpts.DriverUri, - webOpts.DriverOptions as EdgeOptions ?? new EdgeOptions()), - WebAppDriverType.InternetExplorer => new InternetExplorerDriver( - webOpts.DriverUri, - webOpts.DriverOptions as InternetExplorerOptions ?? new InternetExplorerOptions()), - WebAppDriverType.EdgeChromium => new Microsoft.Edge.SeleniumTools.EdgeDriver( - webOpts.DriverUri, - webOpts.DriverOptions as Microsoft.Edge.SeleniumTools.EdgeOptions ?? - new Microsoft.Edge.SeleniumTools.EdgeOptions { UseChromium = true }), - _ => null - }; - - VerifyAppDriver(app, webOpts); - - if (webOpts.Maximize) - { - app.Manage().Window.Maximize(); - } - else - { - app.Manage().Window.Size = webOpts.DesiredSize; - } - - app.Url = webOpts.Url; - break; - } - - case WindowsAppManagerOptions winOpts: - { - if (winOpts.LaunchWinAppDriver) - { - WinAppDriverHelper.Run(); - } - - app = new WindowsDriver( - new Uri(winOpts.DriverUri), - winOpts.AppiumOptions); - - VerifyAppDriver(app, winOpts); - - if (winOpts.Maximize) - { - app.Manage().Window.Maximize(); - } - - break; - } - - case AndroidAppManagerOptions androidOpts: - { - if (androidOpts.LaunchAppiumServer) - { - AppiumServerHelper.Run(); - } - - app = new AndroidDriver( - new Uri(androidOpts.DriverUri), - androidOpts.AppiumOptions); - - VerifyAppDriver(app, androidOpts); - break; + case WebAppManagerOptions webOpts: + { + app = webOpts.DriverType switch + { + WebAppDriverType.Chrome => new ChromeDriver( + webOpts.DriverUri, + webOpts.DriverOptions as ChromeOptions ?? new ChromeOptions()), + WebAppDriverType.Firefox => new FirefoxDriver( + webOpts.DriverUri, + webOpts.DriverOptions as FirefoxOptions ?? new FirefoxOptions()), + WebAppDriverType.Opera => new OperaDriver( + webOpts.DriverUri, + webOpts.DriverOptions as OperaOptions ?? new OperaOptions()), + WebAppDriverType.Safari => new SafariDriver( + webOpts.DriverUri, + webOpts.DriverOptions as SafariOptions ?? new SafariOptions()), + WebAppDriverType.Edge => new EdgeDriver( + webOpts.DriverUri, + webOpts.DriverOptions as EdgeOptions ?? new EdgeOptions()), + WebAppDriverType.InternetExplorer => new InternetExplorerDriver( + webOpts.DriverUri, + webOpts.DriverOptions as InternetExplorerOptions ?? new InternetExplorerOptions()), + WebAppDriverType.EdgeChromium => new Microsoft.Edge.SeleniumTools.EdgeDriver( + webOpts.DriverUri, + webOpts.DriverOptions as Microsoft.Edge.SeleniumTools.EdgeOptions ?? + new Microsoft.Edge.SeleniumTools.EdgeOptions { UseChromium = true }), + _ => null + }; + + VerifyAppDriver(app, webOpts); + + if (webOpts.Maximize) + { + app.Manage().Window.Maximize(); + } + else + { + app.Manage().Window.Size = webOpts.DesiredSize; + } + + app.Url = webOpts.Url; + break; + } + + case WindowsAppManagerOptions winOpts: + { + if (winOpts.LaunchWinAppDriver) + { + WinAppDriverHelper.Run(); + } + + app = new WindowsDriver( + new Uri(winOpts.DriverUri), + winOpts.AppiumOptions); + + VerifyAppDriver(app, winOpts); + + if (winOpts.Maximize) + { + app.Manage().Window.Maximize(); + } + + break; + } + + case AndroidAppManagerOptions androidOpts: + { + if (androidOpts.LaunchAppiumServer) + { + AppiumServerHelper.Run(); + } + + app = new AndroidDriver( + new Uri(androidOpts.DriverUri), + androidOpts.AppiumOptions); + + VerifyAppDriver(app, androidOpts); + break; + } + + case IOSAppManagerOptions iosOpts: + { + if (iosOpts.LaunchAppiumServer) + { + AppiumServerHelper.Run(); + } + + app = new IOSDriver(new Uri(iosOpts.DriverUri), iosOpts.AppiumOptions); + + VerifyAppDriver(app, iosOpts); + break; + } + + default: + VerifyAppDriver(null, opts); + break; } + } + catch (Exception ex) + { + app?.Quit(); - case IOSAppManagerOptions iosOpts: + if (ex is LegerityException) { - if (iosOpts.LaunchAppiumServer) - { - AppiumServerHelper.Run(); - } - - app = new IOSDriver(new Uri(iosOpts.DriverUri), iosOpts.AppiumOptions); - - VerifyAppDriver(app, iosOpts); - break; + throw; } - default: - VerifyAppDriver(null, opts); - break; + throw new DriverLoadFailedException(opts, ex); } if (waitUntil != null) diff --git a/src/Legerity.Core/Exceptions/DriverLoadFailedException.cs b/src/Legerity.Core/Exceptions/DriverLoadFailedException.cs index 649765bf..59fabbac 100644 --- a/src/Legerity.Core/Exceptions/DriverLoadFailedException.cs +++ b/src/Legerity.Core/Exceptions/DriverLoadFailedException.cs @@ -1,5 +1,7 @@ namespace Legerity.Exceptions { + using System; + /// /// Defines an exception thrown if the Appium driver fails to load the requested application. /// @@ -17,6 +19,19 @@ internal DriverLoadFailedException(AppManagerOptions opts) this.AppManagerOptions = opts; } + /// + /// Initializes a new instance of the class. + /// + /// + /// The app manager options used to initialize the driver. + /// + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. + internal DriverLoadFailedException(AppManagerOptions opts, Exception innerException) + : base($"The application driver could not be initialized with the specified app manager options. {opts}", innerException) + { + this.AppManagerOptions = opts; + } + /// /// Gets the app manager options used to initialize the driver. ///