Skip to content

Commit 8526ad5

Browse files
authored
Merge pull request #8 from OrchardCoreContrib/delay-execution
Ability to delay the execution in headed mode
2 parents 0f1d717 + d770bd1 commit 8526ad5

16 files changed

Lines changed: 156 additions & 96 deletions

File tree

src/OrchardCoreContrib.Testing.UI/Browser.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ public class Browser(IPlaywrightBrowserAccessor playwrightBrowserAccessor) : IBr
2222
/// <inheritdoc/>
2323
public string Version { get; set; } = playwrightBrowserAccessor.PlaywrightBrowser.Version;
2424

25+
/// <inheritdoc/>
26+
public UITestOptions TestOptions { get; set; }
27+
2528
/// <inheritdoc/>
2629
public async Task<IPage> OpenPageAsync(string url)
2730
{
2831
var playwrightPage = await InnerBrowser.NewPageAsync();
2932

3033
await playwrightPage.GotoAsync(url);
3134

32-
var page = new Page(new PlaywrightPageAccessor(playwrightPage))
35+
var page = new Page(new PlaywrightPageAccessor(playwrightPage), this)
3336
{
3437
Title = await playwrightPage.TitleAsync(),
3538
Content = await playwrightPage.ContentAsync()

src/OrchardCoreContrib.Testing.UI/BrowserFactory.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public static async Task<IBrowser> CreateAsync(IPlaywright playwright, UITestOpt
3939
_ => throw new NotSupportedException()
4040
};
4141

42-
return new Browser(new PlaywrightBrowserAccessor(browser)) { Type = testOptions.BrowserType };
42+
return new Browser(new PlaywrightBrowserAccessor(browser))
43+
{
44+
Type = testOptions.BrowserType,
45+
TestOptions = testOptions
46+
};
4347
}
4448
}

src/OrchardCoreContrib.Testing.UI/Element.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ namespace OrchardCoreContrib.Testing.UI;
77
/// </summary>
88
/// <remarks>The <see cref="Element"/>.</remarks>
99
/// <param name="locator">The <see cref="ILocator"/>.</param>
10-
public class Element(ILocator locator) : IElement
10+
/// <param name="IPage">The <see cref="page"/>.</param>
11+
public class Element(ILocator locator, IPage page) : IElement
1112
{
13+
private readonly LocatorClickOptions _locatorClickOptions = page.Browser.TestOptions.Delay == 0
14+
? null
15+
: new() { Delay = page.Browser.TestOptions.Delay };
16+
private readonly LocatorPressSequentiallyOptions _locatorPressSequentiallyOptions = page.Browser.TestOptions.Delay == 0
17+
? null
18+
: new() { Delay = page.Browser.TestOptions.Delay };
19+
20+
IPage IElement.Page => page;
21+
1222
/// <inheritdoc/>
1323
public string InnerText { get; set; }
1424

@@ -22,12 +32,12 @@ public class Element(ILocator locator) : IElement
2232
public bool Visible { get; set; }
2333

2434
/// <inheritdoc/>
25-
public async Task ClickAsync() => await locator.ClickAsync();
35+
public async Task ClickAsync() => await locator.ClickAsync(_locatorClickOptions);
2636

2737
/// <inheritdoc/>
2838
public async Task TypeAsync(string text)
2939
{
30-
await locator.FillAsync(text);
40+
await locator.PressSequentiallyAsync(text, _locatorPressSequentiallyOptions);
3141

3242
InnerText = await locator.InnerTextAsync();
3343
InnerHtml = await locator.InnerHTMLAsync();

src/OrchardCoreContrib.Testing.UI/IBrowser.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ public interface IBrowser
1010
/// </summary>
1111
public Microsoft.Playwright.IBrowser InnerBrowser { get; }
1212

13+
/// <summary>
14+
/// Gets the test options that will be applied;
15+
/// </summary>
16+
public UITestOptions TestOptions { get; set; }
17+
1318
/// <summary>
1419
/// Gets or sets the browser type.
1520
/// </summary>

src/OrchardCoreContrib.Testing.UI/IElement.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
/// </summary>
66
public interface IElement
77
{
8+
internal IPage Page { get; }
9+
810
/// <summary>
911
/// Gets the inner text of the element.
1012
/// </summary>

src/OrchardCoreContrib.Testing.UI/IPage.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public interface IPage
1010
/// </summary>
1111
public Microsoft.Playwright.IPage InnerPage { get; }
1212

13+
internal IBrowser Browser { get; }
14+
1315
/// <summary>
1416
/// Gets or sets the page title.
1517
/// </summary>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Xunit;
2+
3+
namespace OrchardCoreContrib.Testing.UI;
4+
5+
/// <summary>
6+
/// Represents a contract for UI test.
7+
/// </summary>
8+
public interface IUITest : IAsyncLifetime
9+
{
10+
/// <summary>
11+
/// Gets or sets the browser instance to be used during the test.
12+
/// </summary>
13+
public IBrowser Browser { get; set; }
14+
15+
/// <summary>
16+
/// Gets the options used during the test.
17+
/// </summary>
18+
public UITestOptions Options { get; }
19+
}

src/OrchardCoreContrib.Testing.UI/Page.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ namespace OrchardCoreContrib.Testing.UI;
99
/// Creates an instance of <see cref="Page"/>.
1010
/// </remarks>
1111
/// <param name="playwrightPageAccessor">The <see cref="IPlaywrightPageAccessor"/>.</param>
12-
public class Page(IPlaywrightPageAccessor playwrightPageAccessor) : IPage
12+
/// <param name="browser">The <see cref="IBrowser"/>.</param>
13+
public class Page(IPlaywrightPageAccessor playwrightPageAccessor, IBrowser browser) : IPage
1314
{
1415
/// <inheritdoc/>
1516
public Microsoft.Playwright.IPage InnerPage => playwrightPageAccessor.PlaywrightPage;
1617

18+
IBrowser IPage.Browser => browser;
19+
1720
/// <inheritdoc/>
1821
public string Title { get; set; }
1922

@@ -33,7 +36,7 @@ public async Task GoToAsync(string url)
3336
public IElement FindElement(string selector)
3437
{
3538
var locator = InnerPage.Locator(selector);
36-
var element = new Element(locator)
39+
var element = new Element(locator, this)
3740
{
3841
InnerText = locator.InnerTextAsync().GetAwaiter().GetResult(),
3942
InnerHtml = locator.InnerHTMLAsync().GetAwaiter().GetResult(),
Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,17 @@
1-
using Microsoft.Playwright;
2-
using Xunit;
3-
4-
namespace OrchardCoreContrib.Testing.UI;
1+
namespace OrchardCoreContrib.Testing.UI;
52

63
/// <summary>
74
/// Represents a UI testing class.
85
/// </summary>
96
/// <param name="browserType">The browser type that will be used during the test. Defaults to <see cref="BrowserType.Edge"/>.</param>
107
/// <param name="headless">Whether the browser runs in headless mode or not. Defaults to <c>true</c>.</param>
11-
public class UITest(BrowserType browserType = BrowserType.Edge, bool headless = true) : IAsyncLifetime
12-
{
13-
private IPlaywright _playwright;
14-
15-
/// <summary>
16-
/// Gets the browser instance to be used during the test.
17-
/// </summary>
18-
public IBrowser Browser { get; private set; }
19-
20-
public UITestOptions Options { get; private set; }
21-
22-
/// <inheritdoc/>
23-
public async Task InitializeAsync()
8+
/// <param name="delay">The amount of time to wait between execute two actions. Defaults to <c>0</c>.</param>
9+
public class UITest(BrowserType browserType = BrowserType.Edge, bool headless = true, int delay = 0)
10+
: UITestBase(new UITestOptions
2411
{
25-
Options = new UITestOptions
26-
{
27-
BrowserType = browserType,
28-
Headless = headless
29-
};
30-
31-
_playwright = await Playwright.CreateAsync();
32-
33-
Browser = await BrowserFactory.CreateAsync(_playwright, Options);
34-
}
35-
36-
/// <inheritdoc/>
37-
public async Task DisposeAsync()
38-
{
39-
_playwright.Dispose();
40-
41-
await Task.CompletedTask;
42-
}
12+
BrowserType = browserType,
13+
Headless = headless,
14+
Delay = delay
15+
})
16+
{
4317
}
Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
1-
using OrchardCoreContrib.Testing.UI.Infrastructure;
1+
using Microsoft.Playwright;
22

33
namespace OrchardCoreContrib.Testing.UI;
44

5-
/// <summary>
6-
/// Represents a base class for UI testing.
7-
/// </summary>
8-
/// <typeparam name="TStartup">The startup class that will be used as entry point.</typeparam>
9-
/// <param name="fixture">The <see cref="WebApplicationFactoryFixture{TStartup}"/>.</param>
10-
public abstract class UITestBase<TStartup>(WebApplicationFactoryFixture<TStartup> fixture) where TStartup : class
5+
public abstract class UITestBase(UITestOptions testOptions) : IUITest
116
{
7+
private IPlaywright _playwright;
8+
9+
/// <summary>
10+
/// Gets or sets the browser instance to be used during the test.
11+
/// </summary>
12+
public IBrowser Browser { get; set; }
13+
1214
/// <summary>
13-
/// Gets the base URL used for the tested website.
15+
/// Gets the options used during the test.
1416
/// </summary>
15-
public string BaseUrl => fixture.ServerAddress;
17+
public UITestOptions Options => testOptions;
18+
19+
/// <inheritdoc/>
20+
public virtual async Task InitializeAsync()
21+
{
22+
_playwright = await Playwright.CreateAsync();
23+
24+
Browser = await BrowserFactory.CreateAsync(_playwright, Options);
25+
}
26+
27+
/// <inheritdoc/>
28+
public virtual async Task DisposeAsync()
29+
{
30+
_playwright.Dispose();
1631

17-
public UITestOptions Options { get; protected set; }
32+
await Task.CompletedTask;
33+
}
1834
}

0 commit comments

Comments
 (0)