From ef6526f4a1b852616071b996b692cfb1051c483c Mon Sep 17 00:00:00 2001 From: James Croft Date: Thu, 9 Jun 2022 17:02:29 +0100 Subject: [PATCH] Added fix for StopApps causing exception due to app being removed in foreach --- samples/WebTests/Pages/HomePage.cs | 55 ++----------------- samples/WebTests/Tests/Edge/HomePageTests.cs | 11 +++- .../Elements/VsCodeInAppNotification.cs | 3 +- src/Legerity.Core/LegerityTestClass.cs | 22 ++++++-- src/Legerity.Core/Pages/BasePage.cs | 8 ++- 5 files changed, 40 insertions(+), 59 deletions(-) diff --git a/samples/WebTests/Pages/HomePage.cs b/samples/WebTests/Pages/HomePage.cs index d9a8d760..141acb84 100644 --- a/samples/WebTests/Pages/HomePage.cs +++ b/samples/WebTests/Pages/HomePage.cs @@ -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; @@ -20,54 +19,8 @@ public class HomePage : BasePage private readonly By readPostButtonLocator = By.ClassName("nectar-button"); - /// - /// Initializes a new instance of the class using the instance that verifies the page has loaded within 2 seconds. - /// - /// Thrown if AppManager.StartApp() has not been called. - /// Thrown if the page is not shown in 2 seconds. - public HomePage() - : this(AppManager.App, TimeSpan.FromSeconds(2)) - { - } - - /// - /// Initializes a new instance of the class using a instance that verifies the page has loaded within 2 seconds. - /// - /// - /// The instance of the started application driver that will be used to drive the page interaction. - /// - /// Thrown if AppManager.StartApp() has not been called. - /// Thrown if the page is not shown in 2 seconds. public HomePage(RemoteWebDriver app) - : this(app, TimeSpan.FromSeconds(2)) - { - } - - /// - /// Initializes a new instance of the class using the instance that verifies the page has loaded within the given timeout. - /// - /// - /// The amount of time the driver should wait when searching for the if it is not immediately present. - /// - /// Thrown if AppManager.StartApp() has not been called. - /// Thrown if the page is not shown in the given timeout. - public HomePage(TimeSpan? traitTimeout) - : this(AppManager.App, traitTimeout) - { - } - - /// - /// Initializes a new instance of the class using a instance that verifies the page has loaded within the given timeout. - /// - /// - /// The instance of the started application driver that will be used to drive the page interaction. - /// - /// - /// The amount of time the driver should wait when searching for the if it is not immediately present. - /// - /// Thrown if AppManager.StartApp() has not been called. - /// Thrown if the page is not shown in the given timeout. - public HomePage(RemoteWebDriver app, TimeSpan? traitTimeout) : base(app, traitTimeout) + : base(app) { } @@ -105,10 +58,10 @@ public PostPage NavigateToHeroPostByIndex(int idx) IWebElement heroPostsContainer = this.WebApp.FindElement(this.heroPostsLocator); ReadOnlyCollection heroPosts = heroPostsContainer.FindElements(this.heroPostLocator); - + IWebElement heroPost = heroPosts[idx]; IWebElement readButton = heroPost.FindElement(this.readPostButtonLocator); - + readButton.Click(); return new PostPage(); diff --git a/samples/WebTests/Tests/Edge/HomePageTests.cs b/samples/WebTests/Tests/Edge/HomePageTests.cs index 9f9f355b..c8de60eb 100644 --- a/samples/WebTests/Tests/Edge/HomePageTests.cs +++ b/samples/WebTests/Tests/Edge/HomePageTests.cs @@ -2,6 +2,8 @@ namespace WebTests.Tests.Edge { using Legerity; using NUnit.Framework; + + using OpenQA.Selenium; using OpenQA.Selenium.Remote; using WebTests.Pages; @@ -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. + } } } } \ No newline at end of file diff --git a/samples/WindowsCommunityToolkitSampleApp/Elements/VsCodeInAppNotification.cs b/samples/WindowsCommunityToolkitSampleApp/Elements/VsCodeInAppNotification.cs index aaad922c..576a14e4 100644 --- a/samples/WindowsCommunityToolkitSampleApp/Elements/VsCodeInAppNotification.cs +++ b/samples/WindowsCommunityToolkitSampleApp/Elements/VsCodeInAppNotification.cs @@ -10,7 +10,8 @@ namespace WindowsCommunityToolkitSampleApp.Elements public class VsCodeInAppNotification : InAppNotification { - public VsCodeInAppNotification(WindowsElement element) : base(element) + public VsCodeInAppNotification(WindowsElement element) + : base(element) { } diff --git a/src/Legerity.Core/LegerityTestClass.cs b/src/Legerity.Core/LegerityTestClass.cs index cff641e1..d925494b 100644 --- a/src/Legerity.Core/LegerityTestClass.cs +++ b/src/Legerity.Core/LegerityTestClass.cs @@ -146,17 +146,29 @@ public virtual void StopApp(bool stopServer) /// public virtual void StopApp(RemoteWebDriver app, bool stopServer = false) { - this.apps.Remove(app); - AppManager.StopApp(app, stopServer); + this.StopAppManagerApp(app, stopServer, true); } /// - /// Stops all running application drivers. + /// Stops all running application drivers, with an option to stop the running Appium or WinAppDriver server. /// - public virtual void StopApps() + /// + /// An optional value indicating whether to stop the running Appium or WinAppDriver server. Default, true. + /// + 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); + } } } \ No newline at end of file diff --git a/src/Legerity.Core/Pages/BasePage.cs b/src/Legerity.Core/Pages/BasePage.cs index 6d681fcb..a9861342 100644 --- a/src/Legerity.Core/Pages/BasePage.cs +++ b/src/Legerity.Core/Pages/BasePage.cs @@ -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); } /// @@ -81,6 +82,11 @@ protected BasePage(RemoteWebDriver app, TimeSpan? traitTimeout) /// public RemoteWebDriver App { get; } + /// + /// Gets or sets the amount of time the driver should wait when searching for elements if they are not immediately present. + /// + public TimeSpan WaitTimeout { get; set; } + /// /// Gets the instance of the started Windows application. ///