Skip to content

Commit

Permalink
Added fix for StopApps causing exception due to app being removed in …
Browse files Browse the repository at this point in the history
…foreach
  • Loading branch information
jamesmcroft committed Jun 9, 2022
1 parent 73e0a81 commit ef6526f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 59 deletions.
55 changes: 4 additions & 51 deletions samples/WebTests/Pages/HomePage.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace WebTests.Pages
{
using System;
using System.Collections.ObjectModel;
using System.Linq;
using Legerity;

using Legerity.Pages;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
Expand All @@ -20,54 +19,8 @@ public class HomePage : BasePage

private readonly By readPostButtonLocator = By.ClassName("nectar-button");

/// <summary>
/// Initializes a new instance of the <see cref="BasePage"/> class using the <see cref="AppManager.App"/> instance that verifies the page has loaded within 2 seconds.
/// </summary>
/// <exception cref="T:Legerity.Exceptions.DriverNotInitializedException">Thrown if AppManager.StartApp() has not been called.</exception>
/// <exception cref="T:Legerity.Exceptions.PageNotShownException">Thrown if the page is not shown in 2 seconds.</exception>
public HomePage()
: this(AppManager.App, TimeSpan.FromSeconds(2))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="BasePage"/> class using a <see cref="RemoteWebDriver"/> instance that verifies the page has loaded within 2 seconds.
/// </summary>
/// <param name="app">
/// The instance of the started application driver that will be used to drive the page interaction.
/// </param>
/// <exception cref="T:Legerity.Exceptions.DriverNotInitializedException">Thrown if AppManager.StartApp() has not been called.</exception>
/// <exception cref="T:Legerity.Exceptions.PageNotShownException">Thrown if the page is not shown in 2 seconds.</exception>
public HomePage(RemoteWebDriver app)
: this(app, TimeSpan.FromSeconds(2))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="BasePage"/> class using the <see cref="AppManager.App"/> instance that verifies the page has loaded within the given timeout.
/// </summary>
/// <param name="traitTimeout">
/// The amount of time the driver should wait when searching for the <see cref="Trait"/> if it is not immediately present.
/// </param>
/// <exception cref="T:Legerity.Exceptions.DriverNotInitializedException">Thrown if AppManager.StartApp() has not been called.</exception>
/// <exception cref="T:Legerity.Exceptions.PageNotShownException">Thrown if the page is not shown in the given timeout.</exception>
public HomePage(TimeSpan? traitTimeout)
: this(AppManager.App, traitTimeout)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="BasePage"/> class using a <see cref="RemoteWebDriver"/> instance that verifies the page has loaded within the given timeout.
/// </summary>
/// <param name="app">
/// The instance of the started application driver that will be used to drive the page interaction.
/// </param>
/// <param name="traitTimeout">
/// The amount of time the driver should wait when searching for the <see cref="Trait"/> if it is not immediately present.
/// </param>
/// <exception cref="T:Legerity.Exceptions.DriverNotInitializedException">Thrown if AppManager.StartApp() has not been called.</exception>
/// <exception cref="T:Legerity.Exceptions.PageNotShownException">Thrown if the page is not shown in the given timeout.</exception>
public HomePage(RemoteWebDriver app, TimeSpan? traitTimeout) : base(app, traitTimeout)
: base(app)
{
}

Expand Down Expand Up @@ -105,10 +58,10 @@ public PostPage NavigateToHeroPostByIndex(int idx)
IWebElement heroPostsContainer = this.WebApp.FindElement(this.heroPostsLocator);

ReadOnlyCollection<IWebElement> heroPosts = heroPostsContainer.FindElements(this.heroPostLocator);

IWebElement heroPost = heroPosts[idx];
IWebElement readButton = heroPost.FindElement(this.readPostButtonLocator);

readButton.Click();

return new PostPage();
Expand Down
11 changes: 10 additions & 1 deletion samples/WebTests/Tests/Edge/HomePageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace WebTests.Tests.Edge
{
using Legerity;
using NUnit.Framework;

using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using WebTests.Pages;

Expand All @@ -21,7 +23,14 @@ public HomePageTests(AppManagerOptions options)
public void ReadHeroPost(int idx)
{
RemoteWebDriver app = this.StartApp();
new HomePage(app).NavigateToHeroPostByIndex(idx).ReadPost(0.25);
try
{
new HomePage(app).NavigateToHeroPostByIndex(idx).ReadPost(0.25);
}
catch (WebDriverException)
{
// Ignored.
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace WindowsCommunityToolkitSampleApp.Elements

public class VsCodeInAppNotification : InAppNotification
{
public VsCodeInAppNotification(WindowsElement element) : base(element)
public VsCodeInAppNotification(WindowsElement element)
: base(element)
{
}

Expand Down
22 changes: 17 additions & 5 deletions src/Legerity.Core/LegerityTestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,29 @@ public virtual void StopApp(bool stopServer)
/// </param>
public virtual void StopApp(RemoteWebDriver app, bool stopServer = false)
{
this.apps.Remove(app);
AppManager.StopApp(app, stopServer);
this.StopAppManagerApp(app, stopServer, true);
}

/// <summary>
/// Stops all running application drivers.
/// Stops all running application drivers, with an option to stop the running Appium or WinAppDriver server.
/// </summary>
public virtual void StopApps()
/// <param name="stopServer">
/// An optional value indicating whether to stop the running Appium or WinAppDriver server. Default, <b>true</b>.
/// </param>
public virtual void StopApps(bool stopServer = true)
{
this.apps.ForEach(app => this.StopApp(app));
this.apps.ForEach(app => this.StopAppManagerApp(app, stopServer, false));
this.apps.Clear();
}

private void StopAppManagerApp(RemoteWebDriver app, bool stopServer, bool removeApp)
{
if (removeApp)
{
this.apps.Remove(app);
}

AppManager.StopApp(app, stopServer);
}
}
}
8 changes: 7 additions & 1 deletion src/Legerity.Core/Pages/BasePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ protected BasePage(TimeSpan? traitTimeout)
protected BasePage(RemoteWebDriver app, TimeSpan? traitTimeout)
{
this.App = app;
this.VerifyPageShown(traitTimeout ?? TimeSpan.FromSeconds(2));
this.WaitTimeout = traitTimeout ?? TimeSpan.FromSeconds(2);
this.VerifyPageShown(this.WaitTimeout);
}

/// <summary>
Expand All @@ -81,6 +82,11 @@ protected BasePage(RemoteWebDriver app, TimeSpan? traitTimeout)
/// </summary>
public RemoteWebDriver App { get; }

/// <summary>
/// Gets or sets the amount of time the driver should wait when searching for elements if they are not immediately present.
/// </summary>
public TimeSpan WaitTimeout { get; set; }

/// <summary>
/// Gets the instance of the started Windows application.
/// </summary>
Expand Down

0 comments on commit ef6526f

Please sign in to comment.