1. What is Playwright ?
A free and open-source tool for testing automation of web applications and API.
2. What are the benefits of using Playwright ?
- it is a free and open-source tool.
- It supports multiple languages: Java, Python, JavaScript, TypeScript, and .NET (including C#).
- supports multiple browsers: Chromium (Google Chrome and Microsoft Edge), WebKit (Safari), and Firefox.
- supports multiple testing platforms/operating systems: Windows, MacOS, Linux
- It has easy setup and configuration
- It includes built-in reporting options and supports custom reporters.
- it has its own locators/selectors, such as getByRole(), getByText(), and getByTestId().
- It supports screenshots, visual snapshot comparisons, and video recording when configured.
- it has auto wait
- it supports recording and replaying. Codegen records user interactions and generates test code; traces can help you inspect and debug executions.
- it provides easy debug and re-running of failed tests. It offers debugging tools, Trace Viewer, and test retries when configured.
- it allows testing multiple tabs(contexts) and multiple browser instances.
- it supports mobile testing. It supports mobile browser/device emulation, but it is not a native mobile app automation tool like Appium.
- it supports API testing
- it is faster than other test automation tools in general.
- allows running the test in headed and headless modes
- supports parallel execution. Tests can run in parallel using workers and across multiple CI jobs.
- supports CICD and Docker. It integrates with CI pipelines and provides official Docker images.
3. How you run a playwright test in a particular browser from command line ?
npx playwright test --project=chromium
npx playwright test --project=firefox
npx playwright test --project=webkit
4. What are the steps to install and
configure Playwright?
1. Downlaod and Intall Node.js
2. Download and install VS Code
3. Create a project folder in your computer
4. Open the folder in VS Code
5. Initialize Playwright:
npm init playwright@latest --> this will install latest version of
playwright.
6. While configuring the playwright, select JavaScript or TypeScript, the test
folder, and whether to add a CI workflow.
7. Install browsers, run:
npx playwright install
npx playwright install — Installs the supported browser binaries.
npx playwright install chromium — Installs only Chromium.
5. What is a fixture in Playwright ?
A fixture is a reusable setup mechanism that provides the objects or resources a test needs and manages their lifecycle, including setup before the test and cleanup afterward.
Let’s compare with a resturant:
· Before the customer arrives: The restaurant prepares the table, cutlery, napkins, and water.
· When the customer arrives: Everything is ready to use.
· After the customer leaves: The restaurant cleans the table and prepares it for the next customer.
Similarly, in Playwright, fixtures prepare resources before a test and clean them up when the test finishes.
"Fixtures do setup before running the test and cleanup afterward."
For example,
the page fixture provides a browser
page and manages its lifecycle, including closing it after the test.
Can we override fixtures and create custom fixtures?
Yes! We can extend built-in fixtures or override them to customize their behavior.
6. How does the “page” fixture work?
Consider this example:
javascript
test('this is a test', async ({ page }) => { await page.goto('https://example.com');});
Here is what happens behind the scenes:
- Playwright starts or reuses a browser instance as needed.
- It creates an isolated browser context for the test.
3. It creates a page within that context and injects it into your test through { page }.
4. Your test uses page to navigate and interact with the website.
- After the test finishes, Playwright cleans up the page and its context automatically.
You don't need to manually create or close these resources when using the standard Playwright Test fixtures.
7. What is the difference between NPM and NPX?
- NPM (Node Package Manager): It is used to install, update, and manage JavaScript packages and their dependencies. For example, we use
npm init playwright@latest to set up a Playwright project.
- NPX (Node Package Executor): It is used to execute packages or commands without necessarily installing them globally. For example, we use
npx playwright test to execute Playwright tests.
8. What are "async" and "await" in Playwright ?
"async" represents an asynchronous function. It makes the function return a promise.
"await" makes the async function wait for the promise to settle. The promise could settle with the result as fulfilled or rejected. After the promise is settled, the function moves with the next step. It throws an error if the promise is rejected, as per the error handling methods applied.
In Playwright, we use async and await to ensure the browser actions such as navigation, clicking, filling etc.
For example:
test('Login to the account page.', async ({ page }) => {
await page.goto('https://example.com');
await page.fill('#username', 'user123');
await page.locator().fill('password', '*********');
await page.click('#login');
});
Here's what happens:
async allows the test function to use await.
await page.goto(...) waits for the navigation operation to complete according to Playwright's navigation rules.
await page.fill(...) waits for Playwright to complete the fill operation.
await page.click(...) waits for Playwright to complete the click operation.
The next statement executes after the click Promise fulfills. If an awaited operation throws an error, execution jumps to error handling or the test fails.
What happens while await is waiting?
Suppose you execute:
await page.goto('https://example.com');
JavaScript does not freeze the entire program while waiting. Instead, the async function pauses at that line, allowing the JavaScript event loop to process other available work. Once the Promise settles, the function resumes.
9. How you manage the failed test cases in Playwright?
In Playwright, we use the retries option to rerun failed test cases a specified number of times.
There are two ways to configure retries:
Configuration file: Set retries in playwright.config.js.
Command line: Use npx playwright test --retries=1 to retry failed tests once.
If retries are configured in both places, the command-line option overrides the configuration file setting.
One important note: Retries apply to failed tests, not every test. By default, retries are set to 0.