As I reviewed again the Playwright acceptance test setup as I provided some onboarding to discuss how Playwright works within the project I am working on. So I would like to give some efficiency tips on Playwright unit test code writing as shown in the code snippet.
OneTimeSetup() is a clean way to separate and initialize the CosmosDbHelper() class.
Setup() is the core code for setting up the test by creating an authenticated page
OneTimeTearDown() is a clean way to dispose of any completed current acceptance test for the page component, efficient for the Devops Pipeline tidyup.
NavigateToConversationURL() is a good example of acceptance testing to test a page navigation test where you do a authenticated page go to async call to navigate, and use await expect to do an assertion of a page title containing the title before checking the consentDialog in a page locator with further page component setup and locator methods called. It will include a Thread.Sleep(1000) to allow Playwright to check for the new page element/component to appear int its rendered context for the next part of acceptance testing.
public Axe Axe { get; private set; } = new Axe();
private AuthenticatedPlaywrightDriver _driver = new AuthenticatedPlaywrightDriver();
protected CosmosDBHelper _dbHelper;
public IPage AuthenticatedPage;
[OneTimeSetUp]
public void OneTimeSetUp()
{
//here we can initialize the cosmos client.
_dbHelper = new CosmosDBHelper();
}
/// <summary>
/// Set up the test by creating an authenticated page
/// </summary>
/// <returns></returns>
[SetUp]
public async Task SetUp()
{
AuthenticatedPage = await _driver.CreateAuthenticatedPage(Browser);
await Context.Tracing.StartAsync(new TracingStartOptions()
{
Screenshots = true,
Snapshots = true,
Sources = true,
});
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
_driver.Dispose();
}
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);
}
Acceptance Testing - Playwright Some Efficiency Tips