Skip to content

Commit

Permalink
Refactor UITest abstraction & implementation APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed May 1, 2024
1 parent e63f005 commit d770bd1
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 79 deletions.
19 changes: 19 additions & 0 deletions src/OrchardCoreContrib.Testing.UI/IUITest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Xunit;

namespace OrchardCoreContrib.Testing.UI;

/// <summary>
/// Represents a contract for UI test.
/// </summary>
public interface IUITest : IAsyncLifetime
{
/// <summary>
/// Gets or sets the browser instance to be used during the test.
/// </summary>
public IBrowser Browser { get; set; }

/// <summary>
/// Gets the options used during the test.
/// </summary>
public UITestOptions Options { get; }
}
44 changes: 9 additions & 35 deletions src/OrchardCoreContrib.Testing.UI/UITest.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,17 @@
using Microsoft.Playwright;
using Xunit;

namespace OrchardCoreContrib.Testing.UI;
namespace OrchardCoreContrib.Testing.UI;

/// <summary>
/// Represents a UI testing class.
/// </summary>
/// <param name="browserType">The browser type that will be used during the test. Defaults to <see cref="BrowserType.Edge"/>.</param>
/// <param name="headless">Whether the browser runs in headless mode or not. Defaults to <c>true</c>.</param>
public class UITest(BrowserType browserType = BrowserType.Edge, bool headless = true) : IAsyncLifetime
{
private IPlaywright _playwright;

/// <summary>
/// Gets the browser instance to be used during the test.
/// </summary>
public IBrowser Browser { get; private set; }

public UITestOptions Options { get; private set; }

/// <inheritdoc/>
public async Task InitializeAsync()
/// <param name="delay">The amount of time to wait between execute two actions. Defaults to <c>0</c>.</param>
public class UITest(BrowserType browserType = BrowserType.Edge, bool headless = true, int delay = 0)
: UITestBase(new UITestOptions
{
Options = new UITestOptions
{
BrowserType = browserType,
Headless = headless
};

_playwright = await Playwright.CreateAsync();

Browser = await BrowserFactory.CreateAsync(_playwright, Options);
}

/// <inheritdoc/>
public async Task DisposeAsync()
{
_playwright.Dispose();

await Task.CompletedTask;
}
BrowserType = browserType,
Headless = headless,
Delay = delay
})
{
}
36 changes: 26 additions & 10 deletions src/OrchardCoreContrib.Testing.UI/UITestBase.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
using OrchardCoreContrib.Testing.UI.Infrastructure;
using Microsoft.Playwright;

namespace OrchardCoreContrib.Testing.UI;

/// <summary>
/// Represents a base class for UI testing.
/// </summary>
/// <typeparam name="TStartup">The startup class that will be used as entry point.</typeparam>
/// <param name="fixture">The <see cref="WebApplicationFactoryFixture{TStartup}"/>.</param>
public abstract class UITestBase<TStartup>(WebApplicationFactoryFixture<TStartup> fixture) where TStartup : class
public abstract class UITestBase(UITestOptions testOptions) : IUITest
{
private IPlaywright _playwright;

/// <summary>
/// Gets or sets the browser instance to be used during the test.
/// </summary>
public IBrowser Browser { get; set; }

/// <summary>
/// Gets the base URL used for the tested website.
/// Gets the options used during the test.
/// </summary>
public string BaseUrl => fixture.ServerAddress;
public UITestOptions Options => testOptions;

/// <inheritdoc/>
public virtual async Task InitializeAsync()
{
_playwright = await Playwright.CreateAsync();

Browser = await BrowserFactory.CreateAsync(_playwright, Options);
}

/// <inheritdoc/>
public virtual async Task DisposeAsync()
{
_playwright.Dispose();

public UITestOptions Options { get; protected set; }
await Task.CompletedTask;
}
}
17 changes: 17 additions & 0 deletions src/OrchardCoreContrib.Testing.UI/UITestBaseOfT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using OrchardCoreContrib.Testing.UI.Infrastructure;

namespace OrchardCoreContrib.Testing.UI;

/// <summary>
/// Represents a base class for UI testing.
/// </summary>
/// <typeparam name="TStartup">The startup class that will be used as entry point.</typeparam>
/// <param name="fixture">The <see cref="WebApplicationFactoryFixture{TStartup}"/>.</param>
public abstract class UITestBase<TStartup>(WebApplicationFactoryFixture<TStartup> fixture, UITestOptions testOptions)
: UITestBase(testOptions) where TStartup : class
{
/// <summary>
/// Gets the base URL used for the tested website.
/// </summary>
public string BaseUrl => fixture.ServerAddress;
}
43 changes: 9 additions & 34 deletions src/OrchardCoreContrib.Testing.UI/UITestOfT.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.Playwright;
using OrchardCoreContrib.Testing.UI.Infrastructure;
using Xunit;
using OrchardCoreContrib.Testing.UI.Infrastructure;

namespace OrchardCoreContrib.Testing.UI;

Expand All @@ -9,37 +7,14 @@ namespace OrchardCoreContrib.Testing.UI;
/// </summary>
/// <param name="browserType">The browser type that will be used during the test. Defaults to <see cref="BrowserType.Edge"/>.</param>
/// <param name="headless">Whether the browser runs in headless mode or not. Defaults to <c>true</c>.</param>
/// <param name="delay">The amount of time to wait between execute two actions. Defaults to <c>0</c>.</param>
/// <typeparam name="TStartup">The startup class type that will be used as entry point.</typeparam>
public class UITest<TStartup>(BrowserType browserType = BrowserType.Edge, bool headless = true) :
UITestBase<TStartup>(new WebApplicationFactoryFixture<TStartup>()),
IAsyncLifetime where TStartup : class
{
private IPlaywright _playwright;

/// <summary>
/// Gets the browser instance to be used during the test.
/// </summary>
public IBrowser Browser { get; private set; }

/// <inheritdoc/>
public async Task InitializeAsync()
public class UITest<TStartup>(BrowserType browserType = BrowserType.Edge, bool headless = true, int delay = 0) :
UITestBase<TStartup>(new WebApplicationFactoryFixture<TStartup>(), new UITestOptions
{
Options = new UITestOptions
{
BrowserType = browserType,
Headless = headless
};

_playwright = await Playwright.CreateAsync();

Browser = await BrowserFactory.CreateAsync(_playwright, Options);
}

/// <inheritdoc/>
public async Task DisposeAsync()
{
_playwright.Dispose();

await Task.CompletedTask;
}
BrowserType = browserType,
Headless = headless,
Delay = delay
}), IUITest where TStartup : class
{
}

0 comments on commit d770bd1

Please sign in to comment.