Whom has used Playwright for unit testing in .NET? I have, and its great for testing and affirming results in a page component as is great at testing the expected behavior of a web page written in .NET!
However there are a few issues, if you are ever encounter issues when Javascript/AJAX calls having to fully complete so that the page is in a truly "ready" state. Here is a handy tip for this: -
Dotnet:
await popup.WaitForLoadStateAsync(LoadState.Networkidle)
https://playwright.dev/dotnet/docs/api/class-page#page-wait-for-load-state
JS:
await page.waitForLoadState("networkidle")
https://playwright.dev/docs/api/class-page#page-wait-for-load-state
For page components, using Thread.Sleep is particular useful when switching between pages or different URls, consider the method NavigateToPageURL() , seen in the code snippet below, we do a sleep, using Thread.Sleep(1000) of 1 second to ensure that the next new component loads up, refactor this code in
Don't use Delays or an actual timeout in the Playwright C# code when testing as I would say its partially or totally unreliable!
public async Task NavigateToPageURL(string url, bool dialog = true)
{
await AuthenticatedPage.GotoAsync(url, new() { WaitUntil = WaitUntilState.NetworkIdle });
// Wait until the page title starts with "Chat: " followed by any text
await Expect(AuthenticatedPage).ToHaveTitleAsync(new Regex(@"^Chat: .*"));
if (dialog)
{
dialogPopup = new DialogPopup(AuthenticatedPage);
await dialogPopup.ClickAgreeButton();
await Expect(AuthenticatedPage).ToHaveTitleAsync(new Regex(@"^Chat: .*"));
}
Thread.Sleep(1000);
}
Playwright Code