Skip to content

Commit

Permalink
Add PlaywrightPageHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed Apr 29, 2024
1 parent 6732643 commit 1a98ede
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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<object[]> CombinatorsData =
[
new [] { By.Id("container") },
Expand Down Expand Up @@ -36,24 +35,12 @@ 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);

// Assert
Assert.True(element.Visible);
}

private static async Task<Microsoft.Playwright.IPage> CreatePlaywrightPageAsync()
{
var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync();

return await browser.NewPageAsync();
}
}
23 changes: 2 additions & 21 deletions test/OrchardCoreContrib.Testing.UI.Tests/PageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand All @@ -99,12 +88,4 @@ public async Task ShouldTakeScreenshot()
Assert.True(File.Exists(screenshotFilePath));
File.Delete(screenshotFilePath);
}

private static async Task<Microsoft.Playwright.IPage> CreatePlaywrightPageAsync()
{
var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync();

return await browser.NewPageAsync();
}
}
19 changes: 19 additions & 0 deletions test/OrchardCoreContrib.Testing.UI.Tests/PlaywrightPageHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace OrchardCoreContrib.Testing.UI.Tests;

internal class PlaywrightPageHelper
{
public static async Task<IPage> 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);
}

0 comments on commit 1a98ede

Please sign in to comment.