Playwright Study Material
🔹 1. What is Playwright?
● Open-source automation library by Microsoft.
● SupportsWeb, Mobile (via browsers), APIs, and componenttesting.
● Works withJavaScript/TypeScript, Python, Java, .NET.
● Cross-browser:Chromium, Firefox, WebKit.
● Supportsparallel execution, auto-waiting, networkinterception, test recording.
🔹 2. Key Features
● Auto-waiting(no need for explicit waits like Selenium).
● Headless & Headed execution.
● Trace Viewerfor debugging (records steps/screenshots/networklogs).
● Network mocking & request interception.
● Cross-browser and cross-platform (Windows, Mac, Linux, CI/CD friendly).
● Mobile emulation(simulate iPhone, Pixel, etc.).
🔹 3. Installation & Setup (Java Example)
<!-- pom.xml (Maven dependency) -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.48.0</version>
</dependency>
Initialize Playwright:
import com.microsoft.playwright.*;
public class FirstTest {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(new
BrowserType.LaunchOptions().setHeadless(false));
Page page = browser.newPage();
page.navigate("https://example.com");
System.out.println(page.title());
browser.close();
}
}
}
🔹 4. Core Playwright Methods
page.navigate("url")
● Navigation:
page.locator("css/xpath/text")
● Locators:
Click/Type:
page.locator("#username").fill("admin");
page.locator("#loginBtn").click();
●
Assertions:
assert page.locator("h1").textContent().contains("Welcome");
●
Waits (automatic):
page.waitForURL("**/dashboard");
●
● F
rames & Popups:
page.frame("frameName")
● S
creenshots:
page.screenshot(new
Page.ScreenshotOptions().setPath(Paths.get("test.png")));
🔹 5. Playwright Test Runner (TS/JS focus)
● Comes with built-intest runner(unlike Selenium).
● Parallel execution by default.
● Supports test fixtures, retries, grouping.
Example (TypeScript):
import { test, expect } from '@playwright/test';
test('Login test', async ({ page }) => {
await page.goto('https://example.com/login');
await page.fill('#username', 'admin');
await page.fill('#password', 'password123');
await page.click('#loginBtn');
await expect(page).toHaveURL(/.*dashboard/);
});
🔹 6. Advanced Features
● Network Interception:Mock API calls.
await page.route('**/api/data', route =>
route.fulfill({ status: 200, body: JSON.stringify({ key: "mocked"
}) })
);
● Tracing & Debugging:
npx playwright test --trace on
● Parallel Execution & Projects (cross-browser tests):
projects: [
{ name: 'Chromium', use: { browserName: 'chromium' } },
{ name: 'Firefox', use: { browserName: 'firefox' } }
]
🔹 7. Playwright vs Selenium
Feature Selenium Playwright
Language Support Java, C#, Python, JS ava, C#, Python, JS,
J
TS
Auto-wait ❌ No (explicit waits needed) ✅ Yes
Cross-browser ✅ Yes ✅ Yes
Mobile Emulation Limited ✅ Built-in
Network Intercept Limited ✅ Strong
Test Runner ❌ Needs JUnit/TestNG ✅ Built-in
Speed & Reliability Moderate Fast
🔹 8. Interview Q&A (Playwright Focus)
Q1. Why Playwright over Selenium?
● F
aster execution, auto-wait, trace debugging, built-in parallel test runner, reliable for
modern apps (SPA/React/Angular).
Q2. How do you handle dynamic elements?
page.locator("text=Login")or
● Use page.getByRole('button', {
name: 'Login' })instead of brittle XPaths.
3. Can Playwright test APIs?
Q
👉 requestcontext. Example:
Yes, using
const response = await request.get('https://api.example.com/data');
expect(response.status()).toBe(200);
Q4. How do you run Playwright tests in CI/CD?
● Integrate with Jenkins, GitHub Actions, Azure DevOps.
● Headless execution (
--headless
).
● Generate HTML reports (
npx playwright show-report
).
5. Can Playwright be used with BDD (Cucumber)?
Q
👉 Yes, integrate via JavaScript or Java with Cucumber plugins.
✅
If you want, I can also prepare aPlaywright CheatSheet + Banking Test Scenarios +
Interview Q&A PDF(similar to Java/SQL kit).
o you want me tocombine Playwright with your Java+ SQL + Banking prep kit, or
D
should I make aseparate Playwright-focused guide?