Playwright Best Practices
Bakkappa N
YouTuber @ Testers Talk | Content Creator | Playwright Cypress Selenium SDET Rest Assured API Postman | Automation Testing | QA Test Lead
Playwright best practices for writing stable & maintainable automation tests.
1. Make tests as isolated as possible
Each test should be completely isolated from another test and should run independently with its own local storage, session storage, data, cookies etc. Test isolation improves reproducibility, makes debugging easier and prevents cascading test failures.
Example: Hooks
import { test } from '@playwright/test';
test.beforeEach(async ({ page }) => {
// Runs before each test and signs in each page.
await page.goto('URL');
await page.getByLabel('Username or email address').fill('username');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Sign in' }).click();
});
test('first', async ({ page }) => {
// page is signed in.
});
test('second', async ({ page }) => {
// page is signed in.
});
2. Use locators correct locator strategy.
Example:
page.getByRole('button', { name: 'submit' });
3. Use chaining and filtering
await page
.getByRole('listitem')
.filter({ hasText: 'Product 2' })
.getByRole('button', { name: 'Add to cart' })
.click();
4. Prefer user-facing attributes to XPath or CSS selectors
Incorrect:
page.locator('button.buttonIcon.episode-actions-later');
Correct:
page.getByRole('button', { name: 'submit' });
5. Generate locators using Codegen tool
6. Use web first assertions & Don't use manual assertions
Example:
await expect(page.getByText('welcome')).toBeVisible();
7. Use VS code extension to debug or development of plawright tests
Reference: https://playwright.dev/docs
===== Playwright Automation Full Courses =====
-> Playwright Full Course - https://youtube.com/playlist?list=PLUeDIlio4THEgPRVJRqZRS8uw8hhVNQCM&si=65JRAAG-SkkfphDm
-> Playwright API Testing Crash Course - https://youtube.com/playlist?list=PLUeDIlio4THF3rnYZ63qkbHwMTXiG67vz&si=hNd_ji_kXVmIAr_R
-> Playwright with Azure DeveOps Pipeline - https://youtube.com/playlist?list=PLUeDIlio4THG8irTXJn-Z02nYzmi6dUOF&si=3b4G5tvemlPy0--j
-> Playwright with CRM application testing - https://youtu.be/WwovRRp0f4o?si=w0rzJlUI9BhfP4KS
===== Playwright GitHub Repositories =====
-> Playwright Full Course - https://github.com/BakkappaN/PlaywrightTutorialFullCourse
-> Playwright API Testing Crash Course - https://github.com/BakkappaN/PlaywrightAPITestingTutorial
-> Playwright with Azure DeveOps Pipeline - https://github.com/BakkappaN/PlaywrightAzureDevopsPipeline
-> Playwright with CRM application testing -https://github.com/BakkappaN/MicrosoftD365CRMPlaywrightFramework
-> Playwright with JavaScript Framework [UI + API] - https://github.com/BakkappaN/PlaywrightBaseAutomationFramework
-> Playwright with TypeScript Framework [UI + API]- https://github.com/BakkappaN/Playwright-TypeScript-Framework
Learn more about Playwright:
-> Playwright API Testing Crash Course
-> Playwright with Azure Devops Complete guide
-> Testing Microsoft CRM application using Playwright
#playwright #apitesting #qa #softwaretesting #crm #javascript #typescript #ado #azuredeveops #framework #fullcourse #codegen #test #e2e #testing #automation #testerstalk #bakkappan #git #github