From a094a03b1a70aed51092dc8babbcdcf1a9c34472 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Wed, 1 May 2024 01:13:33 +0300 Subject: [PATCH] Add non generic version of UITest --- src/OrchardCoreContrib.Testing.UI/UITest.cs | 7 +-- .../UITestOfT.cs | 39 +++++++++++++++++ .../PageHelper.cs | 7 +++ .../PlaywrightPageHelper.cs | 5 +-- .../UITestOfTTests.cs | 43 +++++++++++++++++++ .../UITestTests.cs | 30 ++++--------- 6 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 src/OrchardCoreContrib.Testing.UI/UITestOfT.cs create mode 100644 test/OrchardCoreContrib.Testing.UI.Tests/PageHelper.cs create mode 100644 test/OrchardCoreContrib.Testing.UI.Tests/UITestOfTTests.cs diff --git a/src/OrchardCoreContrib.Testing.UI/UITest.cs b/src/OrchardCoreContrib.Testing.UI/UITest.cs index 09aa109..1fb1cc5 100644 --- a/src/OrchardCoreContrib.Testing.UI/UITest.cs +++ b/src/OrchardCoreContrib.Testing.UI/UITest.cs @@ -5,14 +5,11 @@ namespace OrchardCoreContrib.Testing.UI; /// -/// Represents a UI testing class that extends . +/// Represents a UI testing class. /// /// The browser type that will be used during the test. Defaults to . /// Whether the browser runs in headless mode or not. Defaults to true. -/// The startup class type that will be used as entry point. -public class UITest(BrowserType browserType = BrowserType.Edge, bool headless = true) : - UITestBase(new WebApplicationFactoryFixture()), - IAsyncLifetime where TStartup : class +public class UITest(BrowserType browserType = BrowserType.Edge, bool headless = true) : IAsyncLifetime { private IPlaywright _playwright; diff --git a/src/OrchardCoreContrib.Testing.UI/UITestOfT.cs b/src/OrchardCoreContrib.Testing.UI/UITestOfT.cs new file mode 100644 index 0000000..09aa109 --- /dev/null +++ b/src/OrchardCoreContrib.Testing.UI/UITestOfT.cs @@ -0,0 +1,39 @@ +using Microsoft.Playwright; +using OrchardCoreContrib.Testing.UI.Infrastructure; +using Xunit; + +namespace OrchardCoreContrib.Testing.UI; + +/// +/// Represents a UI testing class that extends . +/// +/// The browser type that will be used during the test. Defaults to . +/// Whether the browser runs in headless mode or not. Defaults to true. +/// The startup class type that will be used as entry point. +public class UITest(BrowserType browserType = BrowserType.Edge, bool headless = true) : + UITestBase(new WebApplicationFactoryFixture()), + IAsyncLifetime where TStartup : class +{ + private IPlaywright _playwright; + + /// + /// Gets the browser instance to be used during the test. + /// + public IBrowser Browser { get; private set; } + + /// + public async Task InitializeAsync() + { + _playwright = await Playwright.CreateAsync(); + + Browser = await BrowserFactory.CreateAsync(_playwright, browserType, headless); + } + + /// + public async Task DisposeAsync() + { + _playwright.Dispose(); + + await Task.CompletedTask; + } +} diff --git a/test/OrchardCoreContrib.Testing.UI.Tests/PageHelper.cs b/test/OrchardCoreContrib.Testing.UI.Tests/PageHelper.cs new file mode 100644 index 0000000..8406225 --- /dev/null +++ b/test/OrchardCoreContrib.Testing.UI.Tests/PageHelper.cs @@ -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; +} diff --git a/test/OrchardCoreContrib.Testing.UI.Tests/PlaywrightPageHelper.cs b/test/OrchardCoreContrib.Testing.UI.Tests/PlaywrightPageHelper.cs index 28a0e6f..45b18f7 100644 --- a/test/OrchardCoreContrib.Testing.UI.Tests/PlaywrightPageHelper.cs +++ b/test/OrchardCoreContrib.Testing.UI.Tests/PlaywrightPageHelper.cs @@ -9,11 +9,8 @@ public static async Task 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; } diff --git a/test/OrchardCoreContrib.Testing.UI.Tests/UITestOfTTests.cs b/test/OrchardCoreContrib.Testing.UI.Tests/UITestOfTTests.cs new file mode 100644 index 0000000..fc11e0b --- /dev/null +++ b/test/OrchardCoreContrib.Testing.UI.Tests/UITestOfTTests.cs @@ -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 +{ + [Fact] + public async Task RunTest() + { + // Arrange + 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"); + + // 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!")); + }); + } + } +} diff --git a/test/OrchardCoreContrib.Testing.UI.Tests/UITestTests.cs b/test/OrchardCoreContrib.Testing.UI.Tests/UITestTests.cs index 767f03d..cab8641 100644 --- a/test/OrchardCoreContrib.Testing.UI.Tests/UITestTests.cs +++ b/test/OrchardCoreContrib.Testing.UI.Tests/UITestTests.cs @@ -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 +public class UITestTests : UITest { [Fact] public async Task RunTest() { // Arrange - var test = new UITest(); + 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); } }