What is Playwright?
Simple Answer: Playwright is a modern test automation tool made by Microsoft. It helps you test websites automatically in different browsers like Chrome, Firefox, and Safari.
Think of it like a robot that opens a browser, clicks buttons, fills forms, and checks if everything works correctly – just like a human tester, but faster and more accurate.
🔑 Key Points
- Made by Microsoft – Developed and maintained by Microsoft
- Works on all major browsers – Chrome, Firefox, Safari
- One code for all browsers – Write once, test everywhere
- Auto-waiting – Automatically waits for elements to be ready
- Reliable tests – Tests don't break due to timing issues
- Multiple languages – Works with JavaScript, Python, .NET, and Java
💻 Example: Simple Google Search Test
const { test, expect } = require('@playwright/test');
test('Search Google for Playwright', async ({ page }) => {
await page.goto('https://www.google.com');
await page.fill('input[name="q"]', 'Playwright');
await page.press('input[name="q"]', 'Enter');
await page.waitForSelector('h3');
const firstResult = await page.textContent('h3');
expect(firstResult).toContain('Playwright');
await page.screenshot({ path: 'google-search.png' });
});
- Opens the Google homepage
- Types "Playwright" into the search box
- Presses Enter to search
- Waits for the search results to load
- Checks if the first result contains "Playwright"
- Takes a screenshot of the results page
Change the search text from "Playwright" to "Automation Testing". Then change the assertion to check if the first result contains "Automation".
What are the Key Features of Playwright?
Simple Answer: Playwright has many useful features that make testing easier. Here are the most important ones explained simply:
🎯 Features
Playwright waits automatically for elements to appear. You don't need to add manual waits.
Same test code works on Chrome, Firefox, and Safari. No browser-specific code needed.
Run many tests at the same time to save time. Great for large test suites.
Automatically captures screenshots and videos when tests fail. Helps debug quickly.
Find elements using text, role, test-id, and more. More reliable than old methods.
Mock API responses and intercept network requests. Test edge cases easily.
Test mobile views by emulating devices like iPhone, iPad, and Pixel.
Generates detailed HTML reports with screenshots and test execution details.
💻 Examples
1. Auto-Waiting
test('Auto-waiting example', async ({ page }) => {
await page.goto('https://example.com');
await page.click('button#submit');
await page.fill('input#name', 'John');
await expect(page.locator('.success-message')).toBeVisible();
});
- Opens the page
- Waits automatically until the submit button is clickable, then clicks it
- Waits automatically until the input is visible, then fills it
- Waits until the success message appears before checking it — no
sleep()needed
2. Running on All Browsers
module.exports = {
projects: [
{ name: 'chromium' },
{ name: 'firefox' },
{ name: 'webkit' },
]
};
test('Works on all browsers', async ({ page }) => {
await page.goto('https://example.com');
await expect(page).toHaveTitle('Example Domain');
});
- Register three browser projects once — Chromium (Chrome), Firefox, and WebKit (Safari)
- Write the test only once
- Playwright automatically runs the same test on all three browsers
3. Smart Selectors
await page.locator('button.submit-btn').click();
await page.locator('text=Login').click();
await page.locator('role=button', { name: 'Submit' }).click();
await page.locator('[data-testid="login-btn"]').click();
await page.locator('placeholder=Enter name').fill('John');
- Find and click a button using its CSS class
- Find and click an element using its visible text
- Find and click a button using its accessibility role — best for accessibility
- Find and click an element using its test-id — most stable option
- Find an input using its placeholder text and fill it
4. Screenshots
test('Take screenshots', async ({ page }) => {
await page.goto('https://example.com');
await page.screenshot({ path: 'fullpage.png', fullPage: true });
await page.locator('header').screenshot({ path: 'header.png' });
});
- Opens the page
- Takes a screenshot of the full page and saves it
- Takes a screenshot of just the header element and saves it
5. Mock API Responses
test('Mock API response', async ({ page }) => {
await page.route('**/api/users', route => {
route.fulfill({
status: 200,
body: JSON.stringify([
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' }
])
});
});
await page.goto('https://example.com');
});
- Intercepts any call made to the
/api/usersendpoint - Fulfills that call with a fake (mocked) JSON response instead of hitting the real API
- Opens the page — it will show the mocked data
📊 Playwright vs Selenium
| Feature | Playwright | Selenium |
|---|---|---|
| Auto-Waiting | ✅ Built-in | ❌ Manual waits needed |
| WebDriver | ✅ Not needed | ❌ Requires WebDriver |
| Cross-Browser | ✅ Chrome, Firefox, Safari | ✅ Chrome, Firefox, Safari, Edge |
| Built-in Tracing | ✅ Yes | ❌ No |
| Network Control | ✅ Built-in | ❌ Limited |
Create a test that: 1) Opens a website, 2) Takes a screenshot, 3) Mocks an API response, 4) Verifies the mocked data appears on the page.
Playwright is a modern, powerful testing tool with many built-in features that make test automation faster, more reliable, and easier to debug.
What is Selenium?
Simple Answer: Selenium is a free, open-source tool used to automate web browsers. It lets you write code that opens a browser, clicks links, fills forms, and checks results — just like a real user, but automatically.
It's one of the oldest and most widely used automation tools in the QA industry, supported by almost every company that does web testing.
🔑 Key Points
- Open-source – Free to use, backed by a large community
- Cross-browser – Works with Chrome, Firefox, Edge, and Safari
- Multiple languages – Supports Java, Python, C#, JavaScript, and Ruby
- WebDriver based – Talks directly to the browser using the WebDriver protocol
- Framework friendly – Works well with TestNG, JUnit, and Maven
- Huge industry adoption – Most in-demand skill in QA automation job postings
💻 Example: Simple Login Test (Java)
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("password123");
driver.findElement(By.id("loginBtn")).click();
String welcomeText = driver.findElement(By.id("welcomeMsg")).getText();
Assert.assertEquals(welcomeText, "Welcome, testuser!");
driver.quit();
- Opens Chrome using WebDriver and navigates to the login page
- Enters the username and password into the input fields
- Clicks the login button
- Reads the welcome message shown after login
- Verifies the welcome message matches what is expected
- Closes the browser
Change the assertion to check that the URL after login contains "/dashboard" instead of checking the welcome text.
What is Appium?
Simple Answer: Appium is a free, open-source tool used to automate mobile apps — both Android and iOS. It works for native apps, hybrid apps, and mobile web, without needing to change the app's code.
Think of it as Selenium for mobile — the same idea of automating user actions, but applied to phones and tablets instead of a desktop browser.
🔑 Key Points
- Cross-platform – Works for both Android and iOS with a similar API
- No app code changes needed – Automates the app exactly as it is
- Multiple languages – Supports Java, Python, JavaScript, and more
- Native, hybrid & web apps – Handles all major mobile app types
- Real devices & emulators – Can run tests on real phones, simulators, or cloud devices
- Built on WebDriver protocol – Similar architecture and commands to Selenium
💻 Example: Simple Login Test (Java)
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "Android");
caps.setCapability("deviceName", "Pixel_5");
caps.setCapability("appPackage", "com.example.app");
caps.setCapability("appActivity", ".LoginActivity");
AndroidDriver driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), caps);
driver.findElement(By.id("com.example.app:id/username")).sendKeys("testuser");
driver.findElement(By.id("com.example.app:id/password")).sendKeys("password123");
driver.findElement(By.id("com.example.app:id/loginBtn")).click();
String welcomeText = driver.findElement(By.id("com.example.app:id/welcomeMsg")).getText();
Assert.assertEquals(welcomeText, "Welcome, testuser!");
driver.quit();
- Sets up device and app details (platform, device name, app package, launch activity)
- Connects to the Appium server and launches the app on the device
- Enters username and password into the app's login fields
- Taps the login button
- Reads the welcome message shown after login
- Verifies the welcome message matches what is expected, then closes the session
Change the capabilities to target iOS instead of Android — swap platformName to "iOS" and update the device/app details accordingly.
What is API Testing?
Simple Answer: API Testing means checking whether the backend of an application — the part that sends and receives data — works correctly, without touching the actual user interface (UI).
Instead of clicking buttons on a screen, you directly send requests (like GET, POST, PUT, DELETE) to the server and check if the response is correct — the data, the status code, and the response time.
🔑 Key Points
- Faster than UI testing – No browser or app rendering involved
- Tests the logic layer – Validates business logic directly at the source
- Common tools – Postman, REST Assured, and RestSharp
- Key checks – Status code, response body, headers, and response time
- Works early in development – Can be tested before the UI is even built
- Great for automation – Very stable, rarely breaks due to UI changes
💻 Example: Simple GET Request Test (REST Assured)
given()
.baseUri("https://api.example.com")
.when()
.get("/users/1")
.then()
.statusCode(200)
.body("name", equalTo("John Doe"))
.body("email", equalTo("john@example.com"))
.time(lessThan(2000L));
- Sets the base URL of the API
- Sends a GET request to the
/users/1endpoint - Checks that the response status code is 200 (success)
- Verifies the "name" and "email" fields in the response body
- Checks that the response arrived within 2 seconds
Write a POST request test that creates a new user and verifies the response status code is 201 (Created).