Skip to content

Commit d770bd1

Browse files
committed
Refactor UITest abstraction & implementation APIs
1 parent e63f005 commit d770bd1

5 files changed

Lines changed: 80 additions & 79 deletions

File tree

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+
}
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
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using OrchardCoreContrib.Testing.UI.Infrastructure;
2+
3+
namespace OrchardCoreContrib.Testing.UI;
4+
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, UITestOptions testOptions)
11+
: UITestBase(testOptions) where TStartup : class
12+
{
13+
/// <summary>
14+
/// Gets the base URL used for the tested website.
15+
/// </summary>
16+
public string BaseUrl => fixture.ServerAddress;
17+
}
Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Microsoft.Playwright;
2-
using OrchardCoreContrib.Testing.UI.Infrastructure;
3-
using Xunit;
1+
using OrchardCoreContrib.Testing.UI.Infrastructure;
42

53
namespace OrchardCoreContrib.Testing.UI;
64

@@ -9,37 +7,14 @@ namespace OrchardCoreContrib.Testing.UI;
97
/// </summary>
108
/// <param name="browserType">The browser type that will be used during the test. Defaults to <see cref="BrowserType.Edge"/>.</param>
119
/// <param name="headless">Whether the browser runs in headless mode or not. Defaults to <c>true</c>.</param>
10+
/// <param name="delay">The amount of time to wait between execute two actions. Defaults to <c>0</c>.</param>
1211
/// <typeparam name="TStartup">The startup class type that will be used as entry point.</typeparam>
13-
public class UITest<TStartup>(BrowserType browserType = BrowserType.Edge, bool headless = true) :
14-
UITestBase<TStartup>(new WebApplicationFactoryFixture<TStartup>()),
15-
IAsyncLifetime where TStartup : class
16-
{
17-
private IPlaywright _playwright;
18-
19-
/// <summary>
20-
/// Gets the browser instance to be used during the test.
21-
/// </summary>
22-
public IBrowser Browser { get; private set; }
23-
24-
/// <inheritdoc/>
25-
public async Task InitializeAsync()
12+
public class UITest<TStartup>(BrowserType browserType = BrowserType.Edge, bool headless = true, int delay = 0) :
13+
UITestBase<TStartup>(new WebApplicationFactoryFixture<TStartup>(), new UITestOptions
2614
{
27-
Options = new UITestOptions
28-
{
29-
BrowserType = browserType,
30-
Headless = headless
31-
};
32-
33-
_playwright = await Playwright.CreateAsync();
34-
35-
Browser = await BrowserFactory.CreateAsync(_playwright, Options);
36-
}
37-
38-
/// <inheritdoc/>
39-
public async Task DisposeAsync()
40-
{
41-
_playwright.Dispose();
42-
43-
await Task.CompletedTask;
44-
}
15+
BrowserType = browserType,
16+
Headless = headless,
17+
Delay = delay
18+
}), IUITest where TStartup : class
19+
{
4520
}

0 commit comments

Comments
 (0)