Skip to content

Commit ef6526f

Browse files
committed
Added fix for StopApps causing exception due to app being removed in foreach
1 parent 73e0a81 commit ef6526f

File tree

5 files changed

+40
-59
lines changed

5 files changed

+40
-59
lines changed

samples/WebTests/Pages/HomePage.cs

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
namespace WebTests.Pages
22
{
3-
using System;
43
using System.Collections.ObjectModel;
54
using System.Linq;
6-
using Legerity;
5+
76
using Legerity.Pages;
87
using OpenQA.Selenium;
98
using OpenQA.Selenium.Remote;
@@ -20,54 +19,8 @@ public class HomePage : BasePage
2019

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

23-
/// <summary>
24-
/// 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.
25-
/// </summary>
26-
/// <exception cref="T:Legerity.Exceptions.DriverNotInitializedException">Thrown if AppManager.StartApp() has not been called.</exception>
27-
/// <exception cref="T:Legerity.Exceptions.PageNotShownException">Thrown if the page is not shown in 2 seconds.</exception>
28-
public HomePage()
29-
: this(AppManager.App, TimeSpan.FromSeconds(2))
30-
{
31-
}
32-
33-
/// <summary>
34-
/// 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.
35-
/// </summary>
36-
/// <param name="app">
37-
/// The instance of the started application driver that will be used to drive the page interaction.
38-
/// </param>
39-
/// <exception cref="T:Legerity.Exceptions.DriverNotInitializedException">Thrown if AppManager.StartApp() has not been called.</exception>
40-
/// <exception cref="T:Legerity.Exceptions.PageNotShownException">Thrown if the page is not shown in 2 seconds.</exception>
4122
public HomePage(RemoteWebDriver app)
42-
: this(app, TimeSpan.FromSeconds(2))
43-
{
44-
}
45-
46-
/// <summary>
47-
/// 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.
48-
/// </summary>
49-
/// <param name="traitTimeout">
50-
/// The amount of time the driver should wait when searching for the <see cref="Trait"/> if it is not immediately present.
51-
/// </param>
52-
/// <exception cref="T:Legerity.Exceptions.DriverNotInitializedException">Thrown if AppManager.StartApp() has not been called.</exception>
53-
/// <exception cref="T:Legerity.Exceptions.PageNotShownException">Thrown if the page is not shown in the given timeout.</exception>
54-
public HomePage(TimeSpan? traitTimeout)
55-
: this(AppManager.App, traitTimeout)
56-
{
57-
}
58-
59-
/// <summary>
60-
/// 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.
61-
/// </summary>
62-
/// <param name="app">
63-
/// The instance of the started application driver that will be used to drive the page interaction.
64-
/// </param>
65-
/// <param name="traitTimeout">
66-
/// The amount of time the driver should wait when searching for the <see cref="Trait"/> if it is not immediately present.
67-
/// </param>
68-
/// <exception cref="T:Legerity.Exceptions.DriverNotInitializedException">Thrown if AppManager.StartApp() has not been called.</exception>
69-
/// <exception cref="T:Legerity.Exceptions.PageNotShownException">Thrown if the page is not shown in the given timeout.</exception>
70-
public HomePage(RemoteWebDriver app, TimeSpan? traitTimeout) : base(app, traitTimeout)
23+
: base(app)
7124
{
7225
}
7326

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

10760
ReadOnlyCollection<IWebElement> heroPosts = heroPostsContainer.FindElements(this.heroPostLocator);
108-
61+
10962
IWebElement heroPost = heroPosts[idx];
11063
IWebElement readButton = heroPost.FindElement(this.readPostButtonLocator);
111-
64+
11265
readButton.Click();
11366

11467
return new PostPage();

samples/WebTests/Tests/Edge/HomePageTests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ namespace WebTests.Tests.Edge
22
{
33
using Legerity;
44
using NUnit.Framework;
5+
6+
using OpenQA.Selenium;
57
using OpenQA.Selenium.Remote;
68
using WebTests.Pages;
79

@@ -21,7 +23,14 @@ public HomePageTests(AppManagerOptions options)
2123
public void ReadHeroPost(int idx)
2224
{
2325
RemoteWebDriver app = this.StartApp();
24-
new HomePage(app).NavigateToHeroPostByIndex(idx).ReadPost(0.25);
26+
try
27+
{
28+
new HomePage(app).NavigateToHeroPostByIndex(idx).ReadPost(0.25);
29+
}
30+
catch (WebDriverException)
31+
{
32+
// Ignored.
33+
}
2534
}
2635
}
2736
}

samples/WindowsCommunityToolkitSampleApp/Elements/VsCodeInAppNotification.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace WindowsCommunityToolkitSampleApp.Elements
1010

1111
public class VsCodeInAppNotification : InAppNotification
1212
{
13-
public VsCodeInAppNotification(WindowsElement element) : base(element)
13+
public VsCodeInAppNotification(WindowsElement element)
14+
: base(element)
1415
{
1516
}
1617

src/Legerity.Core/LegerityTestClass.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,29 @@ public virtual void StopApp(bool stopServer)
146146
/// </param>
147147
public virtual void StopApp(RemoteWebDriver app, bool stopServer = false)
148148
{
149-
this.apps.Remove(app);
150-
AppManager.StopApp(app, stopServer);
149+
this.StopAppManagerApp(app, stopServer, true);
151150
}
152151

153152
/// <summary>
154-
/// Stops all running application drivers.
153+
/// Stops all running application drivers, with an option to stop the running Appium or WinAppDriver server.
155154
/// </summary>
156-
public virtual void StopApps()
155+
/// <param name="stopServer">
156+
/// An optional value indicating whether to stop the running Appium or WinAppDriver server. Default, <b>true</b>.
157+
/// </param>
158+
public virtual void StopApps(bool stopServer = true)
157159
{
158-
this.apps.ForEach(app => this.StopApp(app));
160+
this.apps.ForEach(app => this.StopAppManagerApp(app, stopServer, false));
159161
this.apps.Clear();
160162
}
163+
164+
private void StopAppManagerApp(RemoteWebDriver app, bool stopServer, bool removeApp)
165+
{
166+
if (removeApp)
167+
{
168+
this.apps.Remove(app);
169+
}
170+
171+
AppManager.StopApp(app, stopServer);
172+
}
161173
}
162174
}

src/Legerity.Core/Pages/BasePage.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ protected BasePage(TimeSpan? traitTimeout)
6767
protected BasePage(RemoteWebDriver app, TimeSpan? traitTimeout)
6868
{
6969
this.App = app;
70-
this.VerifyPageShown(traitTimeout ?? TimeSpan.FromSeconds(2));
70+
this.WaitTimeout = traitTimeout ?? TimeSpan.FromSeconds(2);
71+
this.VerifyPageShown(this.WaitTimeout);
7172
}
7273

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

85+
/// <summary>
86+
/// Gets or sets the amount of time the driver should wait when searching for elements if they are not immediately present.
87+
/// </summary>
88+
public TimeSpan WaitTimeout { get; set; }
89+
8490
/// <summary>
8591
/// Gets the instance of the started Windows application.
8692
/// </summary>

0 commit comments

Comments
 (0)