Skip to content

Commit

Permalink
Add non generic version of UITest
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed Apr 30, 2024
1 parent 2841cd3 commit a094a03
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 31 deletions.
7 changes: 2 additions & 5 deletions src/OrchardCoreContrib.Testing.UI/UITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
namespace OrchardCoreContrib.Testing.UI;

/// <summary>
/// Represents a UI testing class that extends <see cref="UITestBase{TStartup}"/>.
/// 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>
/// <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
public class UITest(BrowserType browserType = BrowserType.Edge, bool headless = true) : IAsyncLifetime
{
private IPlaywright _playwright;

Expand Down
39 changes: 39 additions & 0 deletions src/OrchardCoreContrib.Testing.UI/UITestOfT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.Playwright;
using OrchardCoreContrib.Testing.UI.Infrastructure;
using Xunit;

namespace OrchardCoreContrib.Testing.UI;

/// <summary>
/// Represents a UI testing class that extends <see cref="UITestBase{TStartup}"/>.
/// </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>
/// <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()
{
_playwright = await Playwright.CreateAsync();

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

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

await Task.CompletedTask;
}
}
7 changes: 7 additions & 0 deletions test/OrchardCoreContrib.Testing.UI.Tests/PageHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OrchardCoreContrib.Testing.UI.Tests;

public static class PageHelper
{
public static string GetFullPath(string pageName)
=> new Uri(Path.Combine(Environment.CurrentDirectory, "Pages", pageName)).AbsoluteUri;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ public static async Task<IPage> GotoAsync(string pageName)

var page = await browser.NewPageAsync();

await page.GotoAsync(GetFullPath(pageName));
await page.GotoAsync(PageHelper.GetFullPath(pageName));

return new Page(new PlaywrightPageAccessor(page));
}

private static string GetFullPath(string pageName)
=> new Uri(Path.Combine(Environment.CurrentDirectory, "Pages", pageName)).AbsoluteUri;
}
43 changes: 43 additions & 0 deletions test/OrchardCoreContrib.Testing.UI.Tests/UITestOfTTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;

namespace OrchardCoreContrib.Testing.UI.Tests;

public class UITestOfTTests : UITest<UITestOfTTests.SimpleStartup>
{
[Fact]
public async Task RunTest()
{
// Arrange
var test = new UITest<SimpleStartup>();

// Act
await test.InitializeAsync();

// Assert
Assert.NotNull(test.Browser);
Assert.NotNull(test.BaseUrl);
}

[Fact]
public async Task NavigateToHomePage()
{
// Arrange & Act
var page = await Browser.OpenPageAsync(BaseUrl + "foo");

// Assert
Assert.Contains("Hello, world!", page.Content);
}

public class SimpleStartup
{
public void Configure(IApplicationBuilder app)
{
app.Map("/foo", app =>
{
app.Run(async context => await context.Response.WriteAsync("Hello, world!"));
});
}
}
}
30 changes: 8 additions & 22 deletions test/OrchardCoreContrib.Testing.UI.Tests/UITestTests.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,29 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
namespace OrchardCoreContrib.Testing.UI.Tests;

namespace OrchardCoreContrib.Testing.UI.Tests;

public class UITestTests : UITest<UITestTests.SimpleStartup>
public class UITestTests : UITest
{
[Fact]
public async Task RunTest()
{
// Arrange
var test = new UITest<SimpleStartup>();
var test = new UITest();

// Act
await test.InitializeAsync();

// Assert
Assert.NotNull(test.Browser);
Assert.NotNull(test.BaseUrl);
}

[Fact]
public async Task NavigateToHomePage()
{
// Arrange & Act
var page = await Browser.OpenPageAsync(BaseUrl + "foo");
// Arrange
var url = PageHelper.GetFullPath("index.html");
// Act
var page = await Browser.OpenPageAsync(url);

// Assert
Assert.Contains("Hello, world!", page.Content);
}

public class SimpleStartup
{
public void Configure(IApplicationBuilder app)
{
app.Map("/foo", app =>
{
app.Run(async context => await context.Response.WriteAsync("Hello, world!"));
});
}
Assert.Contains("Orchard Core Contrib", page.Title);
}
}

0 comments on commit a094a03

Please sign in to comment.