Created
September 9, 2022 13:54
-
-
Save khalidabuhakmeh/cfc0e3ba6b311b8a9ca3154fd5086a6a to your computer and use it in GitHub Desktop.
Playwright tests with XUnit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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