Skip to content

Commit

Permalink
Added base changes to include main App driver property to differentia…
Browse files Browse the repository at this point in the history
…te between the web driver and a base access driver (strictly naming convention change)
  • Loading branch information
jamesmcroft committed May 31, 2021
1 parent 994e5e8 commit 9c43dbc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
Binary file modified samples/W3SchoolsWebTests/Tools/Edge/MicrosoftWebDriver.exe
Binary file not shown.
Binary file modified samples/WebTests/Tools/Edge/MicrosoftWebDriver.exe
Binary file not shown.
43 changes: 26 additions & 17 deletions src/Legerity.Core/AppManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,30 @@ public static class AppManager
/// <summary>
/// Gets the instance of the started Windows application.
/// </summary>
public static WindowsDriver<WindowsElement> WindowsApp => WebApp as WindowsDriver<WindowsElement>;
public static WindowsDriver<WindowsElement> WindowsApp => App as WindowsDriver<WindowsElement>;

/// <summary>
/// Gets the instance of the started Android application.
/// </summary>
public static AndroidDriver<AndroidElement> AndroidApp => WebApp as AndroidDriver<AndroidElement>;
public static AndroidDriver<AndroidElement> AndroidApp => App as AndroidDriver<AndroidElement>;

/// <summary>
/// Gets the instance of the started iOS application.
/// </summary>
public static IOSDriver<IOSElement> IOSApp => WebApp as IOSDriver<IOSElement>;
public static IOSDriver<IOSElement> IOSApp => App as IOSDriver<IOSElement>;

/// <summary>
/// Get the instance of the started web application.
/// </summary>
public static RemoteWebDriver WebApp { get; set; }
public static RemoteWebDriver WebApp => App;

/// <summary>
/// Gets the instance of the started application.
/// <para>
/// This could be a <see cref="WindowsDriver{W}"/>, <see cref="AndroidDriver{W}"/>, <see cref="IOSDriver{W}"/>, or web driver.
/// </para>
/// </summary>
public static RemoteWebDriver App { get; set; }

/// <summary>
/// Starts the application ready for testing.
Expand All @@ -62,29 +70,29 @@ public static void StartApp(AppManagerOptions opts)
{
case WebAppManagerOptions webOpts:
{
WebApp = webOpts.DriverType switch
App = webOpts.DriverType switch
{
WebAppDriverType.Chrome => new ChromeDriver(webOpts.DriverUri),
WebAppDriverType.Firefox => new FirefoxDriver(webOpts.DriverUri),
WebAppDriverType.Opera => new OperaDriver(webOpts.DriverUri),
WebAppDriverType.Safari => new SafariDriver(webOpts.DriverUri),
WebAppDriverType.Edge => new EdgeDriver(webOpts.DriverUri),
WebAppDriverType.InternetExplorer => new InternetExplorerDriver(webOpts.DriverUri),
_ => WebApp
_ => App
};

VerifyAppDriver(WebApp, webOpts);
VerifyAppDriver(App, webOpts);

if (webOpts.Maximize)
{
WebApp.Manage().Window.Maximize();
App.Manage().Window.Maximize();
}
else
{
WebApp.Manage().Window.Size = webOpts.DesiredSize;
App.Manage().Window.Size = webOpts.DesiredSize;
}

WebApp.Url = webOpts.Url;
App.Url = webOpts.Url;
break;
}
case WindowsAppManagerOptions winOpts:
Expand All @@ -94,22 +102,23 @@ public static void StartApp(AppManagerOptions opts)
WinAppDriverHelper.Run();
}

WebApp = new WindowsDriver<WindowsElement>(
App = new WindowsDriver<WindowsElement>(
new Uri(winOpts.DriverUri),
winOpts.AppiumOptions);

VerifyAppDriver(WindowsApp, winOpts);

if (winOpts.Maximize)
{
WebApp.Manage().Window.Maximize();
App.Manage().Window.Maximize();
}

break;
}

case AndroidAppManagerOptions androidOpts:
{
WebApp = new AndroidDriver<AndroidElement>(
App = new AndroidDriver<AndroidElement>(
new Uri(androidOpts.DriverUri),
androidOpts.AppiumOptions);

Expand All @@ -119,7 +128,7 @@ public static void StartApp(AppManagerOptions opts)

case IOSAppManagerOptions iosOpts:
{
WebApp = new IOSDriver<IOSElement>(new Uri(iosOpts.DriverUri), iosOpts.AppiumOptions);
App = new IOSDriver<IOSElement>(new Uri(iosOpts.DriverUri), iosOpts.AppiumOptions);

VerifyAppDriver(IOSApp, iosOpts);
break;
Expand All @@ -132,10 +141,10 @@ public static void StartApp(AppManagerOptions opts)
/// </summary>
public static void StopApp()
{
if (WebApp != null)
if (App != null)
{
WebApp.Quit();
WebApp = null;
App.Quit();
App = null;
}

WinAppDriverHelper.Stop();
Expand Down
31 changes: 20 additions & 11 deletions src/Legerity.Core/Pages/BasePage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Legerity.Pages
{
using System;

using Legerity.Exceptions;

using OpenQA.Selenium;
Expand Down Expand Up @@ -42,11 +43,19 @@ protected BasePage()

/// <summary>
/// Gets the instance of the started web application.
/// </summary>
protected RemoteWebDriver WebApp => AppManager.WebApp;

/// <summary>
/// Gets the instance of the started application.
/// <para>
/// The <see cref="App"/> instance serves as base for the drivers and can be referenced for basic Selenium functions.
/// </para>
/// <para>
/// The <see cref="WebApp"/> instance also serves as base for the Appium drivers and can be referenced for basic Selenium functions.
/// This could be a <see cref="WindowsDriver{W}"/>, <see cref="AndroidDriver{W}"/>, <see cref="IOSDriver{W}"/>, or web driver.
/// </para>
/// </summary>
protected RemoteWebDriver WebApp => AppManager.WebApp;
protected RemoteWebDriver App => AppManager.App;

/// <summary>
/// Gets a given trait of the page to verify that the page is in view.
Expand All @@ -73,24 +82,24 @@ public void VerifyPageShown()
/// <exception cref="T:Legerity.Exceptions.PageNotShownException">Thrown if the page is not shown.</exception>
public void VerifyPageShown(TimeSpan? timeout)
{
if (this.WebApp == null)
if (this.App == null)
{
throw new DriverNotInitializedException(
$"An app driver has not been initialized. Call 'AppManager.StartApp()' with an instance of an {nameof(AppManagerOptions)} to setup for testing.");
}

if (timeout == null)
{
if (this.WebApp != null && this.WebApp.FindElement(this.Trait) == null)
if (this.App != null && this.App.FindElement(this.Trait) == null)
{
throw new PageNotShownException(this.GetType().Name);
}
}
else
{
if (this.WebApp != null)
if (this.App != null)
{
AttemptWaitForDriverElement(this.Trait, timeout.Value, this.WebApp);
AttemptWaitForDriverElement(this.Trait, timeout.Value, this.App);
}
}
}
Expand Down Expand Up @@ -121,24 +130,24 @@ public void VerifyElementShown(By by)
/// <exception cref="T:Legerity.Exceptions.ElementNotShownException">Thrown if the element is not shown.</exception>
public void VerifyElementShown(By by, TimeSpan? timeout)
{
if (this.WebApp == null)
if (this.App == null)
{
throw new DriverNotInitializedException(
$"An app driver has not been initialized. Call 'AppManager.StartApp()' with an instance of an {nameof(AppManagerOptions)} to setup for testing.");
}

if (timeout == null)
{
if (this.WebApp != null && this.WebApp.FindElement(by) == null)
if (this.App != null && this.App.FindElement(by) == null)
{
throw new ElementNotShownException(by.ToString());
}
}
else
{
if (this.WebApp != null)
if (this.App != null)
{
AttemptWaitForDriverElement(by, timeout.Value, this.WebApp);
AttemptWaitForDriverElement(by, timeout.Value, this.App);
}
}
}
Expand All @@ -152,7 +161,7 @@ public void VerifyElementShown(By by, TimeSpan? timeout)
/// <exception cref="T:Legerity.Exceptions.DriverNotInitializedException">Thrown if AppManager.StartApp() has not been called.</exception>
public void VerifyElementNotShown(By by)
{
if (this.WebApp == null)
if (this.App == null)
{
throw new DriverNotInitializedException(
$"An app driver has not been initialized. Call 'AppManager.StartApp()' with an instance of an {nameof(AppManagerOptions)} to setup for testing.");
Expand Down

0 comments on commit 9c43dbc

Please sign in to comment.