Skip to content

Commit

Permalink
Merge pull request #192 from MADE-Apps/191-failed-app-launch-fix
Browse files Browse the repository at this point in the history
馃悰 Fix for issue where app driver is not quit when exception thrown in startup
  • Loading branch information
tom-made committed Dec 30, 2022
2 parents 462cdd2 + 44af192 commit aab3633
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 92 deletions.
198 changes: 106 additions & 92 deletions src/Legerity.Core/AppManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </param>
/// <exception cref="DriverLoadFailedException">
/// 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.
/// </exception>
/// <exception cref="T:Legerity.Exceptions.AppiumServerLoadFailedException">Thrown if the Appium server could not be found when running with <see cref="AndroidAppManagerOptions.LaunchAppiumServer"/> or <see cref="IOSAppManagerOptions.LaunchAppiumServer"/> true.</exception>
/// <exception cref="T:Legerity.Windows.Exceptions.WinAppDriverNotFoundException">Thrown if the WinAppDriver could not be found when running with <see cref="WindowsAppManagerOptions.LaunchWinAppDriver"/> true.</exception>
Expand All @@ -113,104 +113,118 @@ public static class AppManager
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<WindowsElement>(
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<AndroidElement>(
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<WindowsElement>(
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<AndroidElement>(
new Uri(androidOpts.DriverUri),
androidOpts.AppiumOptions);

VerifyAppDriver(app, androidOpts);
break;
}

case IOSAppManagerOptions iosOpts:
{
if (iosOpts.LaunchAppiumServer)
{
AppiumServerHelper.Run();
}

app = new IOSDriver<IOSElement>(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<IOSElement>(new Uri(iosOpts.DriverUri), iosOpts.AppiumOptions);

VerifyAppDriver(app, iosOpts);
break;
throw;
}

default:
VerifyAppDriver(null, opts);
break;
throw new DriverLoadFailedException(opts, ex);
}

if (waitUntil != null)
Expand Down
15 changes: 15 additions & 0 deletions src/Legerity.Core/Exceptions/DriverLoadFailedException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Legerity.Exceptions
{
using System;

/// <summary>
/// Defines an exception thrown if the Appium driver fails to load the requested application.
/// </summary>
Expand All @@ -17,6 +19,19 @@ internal DriverLoadFailedException(AppManagerOptions opts)
this.AppManagerOptions = opts;
}

/// <summary>
/// Initializes a new instance of the <see cref="DriverLoadFailedException"/> class.
/// </summary>
/// <param name="opts">
/// The app manager options used to initialize the driver.
/// </param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
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;
}

/// <summary>
/// Gets the app manager options used to initialize the driver.
/// </summary>
Expand Down

0 comments on commit aab3633

Please sign in to comment.