From 1a98edea983752e5e06a999ca03592be933b53bb Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Mon, 29 Apr 2024 20:49:24 +0300 Subject: [PATCH] Add PlaywrightPageHelper --- .../Helpers/CssSelectorsTests.cs | 21 ++++------------- .../PageTests.cs | 23 ++----------------- .../PlaywrightPageHelper.cs | 19 +++++++++++++++ 3 files changed, 25 insertions(+), 38 deletions(-) create mode 100644 test/OrchardCoreContrib.Testing.UI.Tests/PlaywrightPageHelper.cs diff --git a/test/OrchardCoreContrib.Testing.UI.Tests/Helpers/CssSelectorsTests.cs b/test/OrchardCoreContrib.Testing.UI.Tests/Helpers/CssSelectorsTests.cs index 1052d4d..1bdd132 100644 --- a/test/OrchardCoreContrib.Testing.UI.Tests/Helpers/CssSelectorsTests.cs +++ b/test/OrchardCoreContrib.Testing.UI.Tests/Helpers/CssSelectorsTests.cs @@ -1,10 +1,9 @@ -namespace OrchardCoreContrib.Testing.UI.Helpers.Tests; +using OrchardCoreContrib.Testing.UI.Tests; + +namespace OrchardCoreContrib.Testing.UI.Helpers.Tests; public class CssSelectorsTests { - private static readonly string _binFolderPath = Path.GetDirectoryName(typeof(CssSelectorsTests).Assembly.Location); - private static readonly string _pagesFolderPath = Path.Combine(_binFolderPath, "Pages"); - public static readonly IEnumerable CombinatorsData = [ new [] { By.Id("container") }, @@ -36,11 +35,7 @@ public class CssSelectorsTests public async Task ShouldFindByCssSelector(string selector) { // Arrange - var playwrightPage = await CreatePlaywrightPageAsync(); - - await playwrightPage.GotoAsync(Path.Combine(_pagesFolderPath, "selectors.html")); - - var page = new Page(new PlaywrightPageAccessor(playwrightPage)); + var page = await PlaywrightPageHelper.GotoAsync("selectors.html"); // Act var element = page.FindElement(selector); @@ -48,12 +43,4 @@ public async Task ShouldFindByCssSelector(string selector) // Assert Assert.True(element.Visible); } - - private static async Task CreatePlaywrightPageAsync() - { - var playwright = await Playwright.CreateAsync(); - var browser = await playwright.Chromium.LaunchAsync(); - - return await browser.NewPageAsync(); - } } diff --git a/test/OrchardCoreContrib.Testing.UI.Tests/PageTests.cs b/test/OrchardCoreContrib.Testing.UI.Tests/PageTests.cs index dbb235f..0c19299 100644 --- a/test/OrchardCoreContrib.Testing.UI.Tests/PageTests.cs +++ b/test/OrchardCoreContrib.Testing.UI.Tests/PageTests.cs @@ -2,9 +2,6 @@ namespace OrchardCoreContrib.Testing.UI.Tests; public class PageTests { - private static readonly string _binFolderPath = Path.GetDirectoryName(typeof(PageTests).Assembly.Location); - private static readonly string _pagesFolderPath = Path.Combine(_binFolderPath, "Pages"); - [Fact] public void ShouldCreatePage() { @@ -66,11 +63,7 @@ public void ShouldFindElement() public async Task ShouldClickAnElement() { // Arrange - var playwrightPage = await CreatePlaywrightPageAsync(); - - await playwrightPage.GotoAsync(Path.Combine(_pagesFolderPath, "index.html")); - - var page = new Page(new PlaywrightPageAccessor(playwrightPage)); + var page = await PlaywrightPageHelper.GotoAsync("index.html"); // Act await page.ClickAsync("button"); @@ -86,11 +79,7 @@ public async Task ShouldTakeScreenshot() // Arrange var screenshotFilePath = "screenshot.jpg"; - var playwrightPage = await CreatePlaywrightPageAsync(); - - await playwrightPage.GotoAsync(Path.Combine(_pagesFolderPath, "index.html")); - - var page = new Page(new PlaywrightPageAccessor(playwrightPage)); + var page = await PlaywrightPageHelper.GotoAsync("index.html"); // Act await page.ScreenShotAsync(screenshotFilePath); @@ -99,12 +88,4 @@ public async Task ShouldTakeScreenshot() Assert.True(File.Exists(screenshotFilePath)); File.Delete(screenshotFilePath); } - - private static async Task CreatePlaywrightPageAsync() - { - var playwright = await Playwright.CreateAsync(); - var browser = await playwright.Chromium.LaunchAsync(); - - return await browser.NewPageAsync(); - } } \ No newline at end of file diff --git a/test/OrchardCoreContrib.Testing.UI.Tests/PlaywrightPageHelper.cs b/test/OrchardCoreContrib.Testing.UI.Tests/PlaywrightPageHelper.cs new file mode 100644 index 0000000..ae45669 --- /dev/null +++ b/test/OrchardCoreContrib.Testing.UI.Tests/PlaywrightPageHelper.cs @@ -0,0 +1,19 @@ +namespace OrchardCoreContrib.Testing.UI.Tests; + +internal class PlaywrightPageHelper +{ + public static async Task GotoAsync(string pageName) + { + var playwright = await Playwright.CreateAsync(); + var browser = await playwright.Chromium.LaunchAsync(); + + var page = await browser.NewPageAsync(); + + await page.GotoAsync(GetFullPath(pageName)); + + return new Page(new PlaywrightPageAccessor(page)); + } + + private static string GetFullPath(string pageName) + => Path.Combine(Environment.CurrentDirectory, "Pages", pageName); +}