Skip to content

Instantly share code, notes, and snippets.

@khalidabuhakmeh
Created September 9, 2022 13:54
Show Gist options
  • Save khalidabuhakmeh/cfc0e3ba6b311b8a9ca3154fd5086a6a to your computer and use it in GitHub Desktop.
Save khalidabuhakmeh/cfc0e3ba6b311b8a9ca3154fd5086a6a to your computer and use it in GitHub Desktop.
Playwright tests with XUnit
[CollectionDefinition(nameof(PlaywrightFixture))]
public class SharedPlaywrightCollection : ICollectionFixture<PlaywrightFixture> {}
// ReSharper disable once ClassNeverInstantiated.Global
public class PlaywrightFixture : IAsyncLifetime
{
public async Task InitializeAsync()
{
PlaywrightInstance = await Playwright.CreateAsync();
Browser = await PlaywrightInstance.Chromium.LaunchAsync();
}
public IBrowser Browser { get; set; } = null!;
private IPlaywright PlaywrightInstance { get; set; } = null!;
public async Task DisposeAsync()
{
await Browser.DisposeAsync();
PlaywrightInstance.Dispose();
}
}
using Microsoft.Playwright;
using Xunit;
namespace WebTests;
public class UnitTest1
: IClassFixture<PlaywrightFixture>
{
private IBrowser Browser { get; }
public UnitTest1(PlaywrightFixture fixture)
{
Browser = fixture.Browser;
}
[Fact]
public async Task Google_Im_Feeling_lucky()
{
var page = await Browser.NewPageAsync();
await page.GotoAsync("https://google.com");
var locator = page.Locator("input#gbqfbb");
var actual = await locator.CountAsync();
Assert.Equal(1, actual);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment