Skip to content

Commit 44af192

Browse files
committed
🐛 Added change to ensure app driver is quit if an exception is thrown in start up
1 parent 462cdd2 commit 44af192

File tree

2 files changed

+121
-92
lines changed

2 files changed

+121
-92
lines changed

src/Legerity.Core/AppManager.cs

Lines changed: 106 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static class AppManager
9393
/// An optional count of retries after a timeout on the wait until condition before accepting the failure.
9494
/// </param>
9595
/// <exception cref="DriverLoadFailedException">
96-
/// Thrown if the application is null or the session ID is null once initialized.
96+
/// Thrown if the application is null, the session ID is null once initialized, or the driver fails to configure correctly before returning.
9797
/// </exception>
9898
/// <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>
9999
/// <exception cref="T:Legerity.Windows.Exceptions.WinAppDriverNotFoundException">Thrown if the WinAppDriver could not be found when running with <see cref="WindowsAppManagerOptions.LaunchWinAppDriver"/> true.</exception>
@@ -113,104 +113,118 @@ public static RemoteWebDriver StartApp(
113113
appiumOpts.Configure();
114114
}
115115

116-
switch (opts)
116+
try
117117
{
118-
case WebAppManagerOptions webOpts:
118+
switch (opts)
119119
{
120-
app = webOpts.DriverType switch
121-
{
122-
WebAppDriverType.Chrome => new ChromeDriver(
123-
webOpts.DriverUri,
124-
webOpts.DriverOptions as ChromeOptions ?? new ChromeOptions()),
125-
WebAppDriverType.Firefox => new FirefoxDriver(
126-
webOpts.DriverUri,
127-
webOpts.DriverOptions as FirefoxOptions ?? new FirefoxOptions()),
128-
WebAppDriverType.Opera => new OperaDriver(
129-
webOpts.DriverUri,
130-
webOpts.DriverOptions as OperaOptions ?? new OperaOptions()),
131-
WebAppDriverType.Safari => new SafariDriver(
132-
webOpts.DriverUri,
133-
webOpts.DriverOptions as SafariOptions ?? new SafariOptions()),
134-
WebAppDriverType.Edge => new EdgeDriver(
135-
webOpts.DriverUri,
136-
webOpts.DriverOptions as EdgeOptions ?? new EdgeOptions()),
137-
WebAppDriverType.InternetExplorer => new InternetExplorerDriver(
138-
webOpts.DriverUri,
139-
webOpts.DriverOptions as InternetExplorerOptions ?? new InternetExplorerOptions()),
140-
WebAppDriverType.EdgeChromium => new Microsoft.Edge.SeleniumTools.EdgeDriver(
141-
webOpts.DriverUri,
142-
webOpts.DriverOptions as Microsoft.Edge.SeleniumTools.EdgeOptions ??
143-
new Microsoft.Edge.SeleniumTools.EdgeOptions { UseChromium = true }),
144-
_ => null
145-
};
146-
147-
VerifyAppDriver(app, webOpts);
148-
149-
if (webOpts.Maximize)
150-
{
151-
app.Manage().Window.Maximize();
152-
}
153-
else
154-
{
155-
app.Manage().Window.Size = webOpts.DesiredSize;
156-
}
157-
158-
app.Url = webOpts.Url;
159-
break;
160-
}
161-
162-
case WindowsAppManagerOptions winOpts:
163-
{
164-
if (winOpts.LaunchWinAppDriver)
165-
{
166-
WinAppDriverHelper.Run();
167-
}
168-
169-
app = new WindowsDriver<WindowsElement>(
170-
new Uri(winOpts.DriverUri),
171-
winOpts.AppiumOptions);
172-
173-
VerifyAppDriver(app, winOpts);
174-
175-
if (winOpts.Maximize)
176-
{
177-
app.Manage().Window.Maximize();
178-
}
179-
180-
break;
181-
}
182-
183-
case AndroidAppManagerOptions androidOpts:
184-
{
185-
if (androidOpts.LaunchAppiumServer)
186-
{
187-
AppiumServerHelper.Run();
188-
}
189-
190-
app = new AndroidDriver<AndroidElement>(
191-
new Uri(androidOpts.DriverUri),
192-
androidOpts.AppiumOptions);
193-
194-
VerifyAppDriver(app, androidOpts);
195-
break;
120+
case WebAppManagerOptions webOpts:
121+
{
122+
app = webOpts.DriverType switch
123+
{
124+
WebAppDriverType.Chrome => new ChromeDriver(
125+
webOpts.DriverUri,
126+
webOpts.DriverOptions as ChromeOptions ?? new ChromeOptions()),
127+
WebAppDriverType.Firefox => new FirefoxDriver(
128+
webOpts.DriverUri,
129+
webOpts.DriverOptions as FirefoxOptions ?? new FirefoxOptions()),
130+
WebAppDriverType.Opera => new OperaDriver(
131+
webOpts.DriverUri,
132+
webOpts.DriverOptions as OperaOptions ?? new OperaOptions()),
133+
WebAppDriverType.Safari => new SafariDriver(
134+
webOpts.DriverUri,
135+
webOpts.DriverOptions as SafariOptions ?? new SafariOptions()),
136+
WebAppDriverType.Edge => new EdgeDriver(
137+
webOpts.DriverUri,
138+
webOpts.DriverOptions as EdgeOptions ?? new EdgeOptions()),
139+
WebAppDriverType.InternetExplorer => new InternetExplorerDriver(
140+
webOpts.DriverUri,
141+
webOpts.DriverOptions as InternetExplorerOptions ?? new InternetExplorerOptions()),
142+
WebAppDriverType.EdgeChromium => new Microsoft.Edge.SeleniumTools.EdgeDriver(
143+
webOpts.DriverUri,
144+
webOpts.DriverOptions as Microsoft.Edge.SeleniumTools.EdgeOptions ??
145+
new Microsoft.Edge.SeleniumTools.EdgeOptions { UseChromium = true }),
146+
_ => null
147+
};
148+
149+
VerifyAppDriver(app, webOpts);
150+
151+
if (webOpts.Maximize)
152+
{
153+
app.Manage().Window.Maximize();
154+
}
155+
else
156+
{
157+
app.Manage().Window.Size = webOpts.DesiredSize;
158+
}
159+
160+
app.Url = webOpts.Url;
161+
break;
162+
}
163+
164+
case WindowsAppManagerOptions winOpts:
165+
{
166+
if (winOpts.LaunchWinAppDriver)
167+
{
168+
WinAppDriverHelper.Run();
169+
}
170+
171+
app = new WindowsDriver<WindowsElement>(
172+
new Uri(winOpts.DriverUri),
173+
winOpts.AppiumOptions);
174+
175+
VerifyAppDriver(app, winOpts);
176+
177+
if (winOpts.Maximize)
178+
{
179+
app.Manage().Window.Maximize();
180+
}
181+
182+
break;
183+
}
184+
185+
case AndroidAppManagerOptions androidOpts:
186+
{
187+
if (androidOpts.LaunchAppiumServer)
188+
{
189+
AppiumServerHelper.Run();
190+
}
191+
192+
app = new AndroidDriver<AndroidElement>(
193+
new Uri(androidOpts.DriverUri),
194+
androidOpts.AppiumOptions);
195+
196+
VerifyAppDriver(app, androidOpts);
197+
break;
198+
}
199+
200+
case IOSAppManagerOptions iosOpts:
201+
{
202+
if (iosOpts.LaunchAppiumServer)
203+
{
204+
AppiumServerHelper.Run();
205+
}
206+
207+
app = new IOSDriver<IOSElement>(new Uri(iosOpts.DriverUri), iosOpts.AppiumOptions);
208+
209+
VerifyAppDriver(app, iosOpts);
210+
break;
211+
}
212+
213+
default:
214+
VerifyAppDriver(null, opts);
215+
break;
196216
}
217+
}
218+
catch (Exception ex)
219+
{
220+
app?.Quit();
197221

198-
case IOSAppManagerOptions iosOpts:
222+
if (ex is LegerityException)
199223
{
200-
if (iosOpts.LaunchAppiumServer)
201-
{
202-
AppiumServerHelper.Run();
203-
}
204-
205-
app = new IOSDriver<IOSElement>(new Uri(iosOpts.DriverUri), iosOpts.AppiumOptions);
206-
207-
VerifyAppDriver(app, iosOpts);
208-
break;
224+
throw;
209225
}
210226

211-
default:
212-
VerifyAppDriver(null, opts);
213-
break;
227+
throw new DriverLoadFailedException(opts, ex);
214228
}
215229

216230
if (waitUntil != null)

src/Legerity.Core/Exceptions/DriverLoadFailedException.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace Legerity.Exceptions
22
{
3+
using System;
4+
35
/// <summary>
46
/// Defines an exception thrown if the Appium driver fails to load the requested application.
57
/// </summary>
@@ -17,6 +19,19 @@ internal DriverLoadFailedException(AppManagerOptions opts)
1719
this.AppManagerOptions = opts;
1820
}
1921

22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="DriverLoadFailedException"/> class.
24+
/// </summary>
25+
/// <param name="opts">
26+
/// The app manager options used to initialize the driver.
27+
/// </param>
28+
/// <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>
29+
internal DriverLoadFailedException(AppManagerOptions opts, Exception innerException)
30+
: base($"The application driver could not be initialized with the specified app manager options. {opts}", innerException)
31+
{
32+
this.AppManagerOptions = opts;
33+
}
34+
2035
/// <summary>
2136
/// Gets the app manager options used to initialize the driver.
2237
/// </summary>

0 commit comments

Comments
 (0)