Playwright JS InterviewGuide
Playwright JS InterviewGuide
Playwright (JavaScript)
Complete Interview Guide
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
Kalpesh Jain
SDET @ Algebrik AI | Mentor | 42K+ Community ■
Bengaluru, Karnataka, India
[Link]/in/kalpeshnjain09
JSPM University Pune
Page 1
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Table of Contents
CORE CONCEPTS
1. Playwright Architecture
2. Installation & Project Setup
3. Test Runner (test, expect)
4. Browser Context & Page
LOCATORS
1. locator()
2. getByRole() ★
3. getByText()
4. CSS & XPath
5. Best Practices ★
FRAMEWORK DESIGN
1. Page Object Model (POM) ★
2. Folder Structure
3. Reusability & Utilities
4. Fixtures ★
5. Config Management
6. Data-Driven Testing
7. Reporting
8. CI/CD Integration Basics
Page 2
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Page 3
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
CORE CONCEPTS
1. Playwright Architecture
What is it
Playwright architecture defines how tests interact with browsers using a [Link] layer + browser engines
(Chromium, Firefox, WebKit).
Key Components
• Test Script (JS/TS)
• Playwright API
• Browser Server
• Browser Engines
• Browser Context
• Page
Practical Example
JavaScript
await [Link]('[Link]
});
Common Mistakes
✕ Saying 'Playwright only works on Chrome'
✕ Not mentioning browser context
✕ Over-explaining internal workings
Page 4
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
What is it
Setting up a Playwright project using [Link] and installing required dependencies.
Key Components
• [Link]
• npm
• @playwright/test
• Browser installation
• Project structure
Practical Example
Bash
npm init -y
Common Mistakes
✕ Saying 'Playwright needs Selenium'
✕ Not knowing setup commands
✕ Not knowing project structure
What is it
Playwright's built-in Test Runner is used to execute tests and perform assertions.
Page 5
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Key Components
• test()
• expect()
• Hooks (beforeEach, afterEach)
• Fixtures
Practical Example
JavaScript
await [Link]('[Link]
await expect(page).toHaveTitle(/Example/);
});
Common Mistakes
✕ Confusing test() with a plain function
✕ Not knowing expect() usage
✕ Ignoring hooks
What is it
Browser context is an isolated session, and page represents a tab.
Key Components
• Browser
• Context
• Page
Page 6
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Browser context is like a separate session similar to incognito mode, and page represents a single tab.
This helps in running tests independently without affecting each other.
Practical Example
JavaScript
Common Mistakes
✕ Saying context = browser
✕ Not understanding isolation
✕ Confusing page and context
Page 7
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
LOCATORS
1. locator()
What is it
locator() is a Playwright method used to find elements using selectors like CSS, XPath, id, etc.
Key Components
• CSS selectors (#id, .class)
• XPath
• Chaining (locator().locator())
• Auto-waiting support
Practical Example
JavaScript
await [Link]('#username').fill('Kalpesh');
await [Link]('.login-btn').click();
Common Mistakes
✕ Using long XPath unnecessarily
✕ Not using chaining
✕ Over-relying on CSS when better options exist
2. getByRole() ★
What is it
Used to locate elements based on ARIA roles (accessibility-based).
Page 8
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Key Components
• Role (button, textbox, link)
• Name (visible label/text)
• Accessibility tree
Practical Example
JavaScript
Common Mistakes
✕ Not using role-based locators
✕ Ignoring accessibility
✕ Using XPath instead
3. getByText()
What is it
Used to locate elements using visible text on the page.
Key Components
• Exact text match
• Partial text match
• Case sensitivity
• Works on visible UI text
Page 9
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Practical Example
JavaScript
await [Link]('Login').click();
await [Link]('Submit').click();
Common Mistakes
✕ Overusing text-based locators
✕ Not handling dynamic text
✕ Using when role-based locator is better
What is it
Traditional selector strategies used to locate elements in the DOM.
Key Components
• CSS (#id, .class, tag)
• XPath (//, contains(), text())
Practical Example
JavaScript
Common Mistakes
✕ Writing long and complex XPath
✕ Using XPath when better locators exist
✕ Not optimising selectors
Page 10
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
■ CSS vs XPath?
■ Which is faster and why?
■ When do you use XPath?
5. Best Practices ★
What is it
Guidelines to write stable, maintainable locators.
Key Components
• Prefer getByRole()
• Use data-testid
• Avoid XPath
• Keep selectors simple
• Use chaining
Practical Example
JavaScript
await [Link]('[data-testid="login-btn"]').click();
Common Mistakes
✕ Using absolute XPath
✕ Writing brittle selectors
✕ Ignoring readability
Page 11
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
1. Actions
What is it
Actions are methods used to interact with web elements like clicking, typing, selecting, hovering, etc.
Key Components
• click()
• fill()
• type()
• check() / uncheck()
• hover()
• selectOption()
• press()
Practical Example
JavaScript
await [Link]('#username').fill('Kalpesh');
await [Link]('#country').selectOption('India');
Common Mistakes
✕ Using unnecessary waits before actions
✕ Using force click without reason
✕ Confusing fill() and type()
Page 12
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
2. Assertions
What is it
Assertions are validations used to verify expected behaviour or UI state.
Key Components
• toHaveText()
• toBeVisible()
• toHaveURL()
• toHaveTitle()
• toBeChecked()
Practical Example
JavaScript
await expect(page).toHaveTitle(/Dashboard/);
await expect([Link]('.success-msg')).toBeVisible();
await expect([Link]('#name')).toHaveValue('Kalpesh');
Common Mistakes
✕ Using manual validations instead of expect()
✕ Not understanding auto-retry behaviour
✕ Writing weak assertions
3. Auto-Waiting ★
What is it
Playwright automatically waits for elements to become ready before performing actions or assertions.
Page 13
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Key Components
• Visibility check
• Element enabled state
• Stability check
• Auto-retrying assertions
Practical Example
JavaScript
Common Mistakes
✕ Adding unnecessary waitForTimeout()
✕ Using hard waits everywhere
✕ Not trusting Playwright auto-wait
4. Best Practices ★
What is it
Guidelines for writing reliable actions and assertions.
Key Components
• Prefer built-in assertions
• Avoid hard waits
• Use stable locators
• Validate meaningful UI behaviour
Page 14
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
I avoid hard waits and rely on Playwright's built-in auto-waiting and assertions. I also use stable locators
and meaningful validations.
Practical Example
JavaScript
await expect([Link]('heading')).toHaveText('Dashboard');
Common Mistakes
✕ Using waitForTimeout() frequently
✕ Weak validations
✕ Over-validating unnecessary UI elements
Page 15
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
FRAMEWORK DESIGN
What is it
POM is a design pattern where web pages are represented as separate classes/files containing locators
and reusable methods.
Key Components
• Page classes
• Locators
• Reusable methods
• Separation of test logic and page logic
Practical Example
JavaScript
class LoginPage {
constructor(page) {
[Link] = page;
[Link] = [Link]('#username');
[Link] = [Link]('#password');
await [Link](user);
await [Link](pass);
Common Mistakes
✕ Writing locators inside test files
✕ Mixing test logic and page logic
✕ Creating very large page classes
Page 16
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
2. Folder Structure
What is it
Folder structure organises framework components for scalability and maintainability.
Key Components
• pages/
• tests/
• utils/
• fixtures/
• test-data/
• config/
Practical Example
Structure
project-root/
■■■ pages/
■■■ tests/
■■■ utils/
■■■ fixtures/
■■■ test-data/
■■■ [Link]
Common Mistakes
✕ Keeping everything in one file
✕ No separation of concerns
✕ Improper naming conventions
Page 17
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
What is it
Utilities are reusable helper methods used across the framework to avoid duplicate code.
Key Components
• Common methods
• Helper functions
• Reusable actions
• Generic validations
Practical Example
JavaScript
Common Mistakes
✕ Duplicate code
✕ Large utility files with mixed responsibilities
✕ Hardcoded values
Page 18
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
4. Fixtures ★
What is it
Fixtures are reusable setup components used to share test setup and dependencies across tests.
Key Components
• Shared setup
• Test isolation
• Dependency injection
• beforeEach equivalent behaviour
Practical Example
JavaScript
await [Link]('[Link]
});
Common Mistakes
✕ Repeating setup in every test
✕ Misusing global setup
✕ Not understanding test isolation
5. Config Management
What is it
Configuration management controls framework settings like base URL, browser setup, retries, and
environments.
Page 19
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Key Components
• [Link]
• Base URL
• Retries
• Parallel execution
• Environment setup
Practical Example
JavaScript
use: {
baseURL: '[Link]
headless: true
Common Mistakes
✕ Hardcoding environment values
✕ No retry configuration
✕ Poor environment management
6. Data-Driven Testing
What is it
Running the same test with multiple datasets.
Key Components
• JSON data
• Parameterised tests
• External test data
Page 20
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Practical Example
JavaScript
const users = [
];
Common Mistakes
✕ Hardcoded test data
✕ Mixing test data and logic
✕ Poor data organisation
7. Reporting
What is it
Reporting helps track test execution results and failures.
Key Components
• HTML Reports
• Allure Reports
• Screenshots
• Logs
Practical Example
Page 21
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Bash
Common Mistakes
✕ No failure screenshots
✕ Ignoring logs
✕ Poor report readability
What is it
Integrating automation execution into CI/CD pipelines for continuous testing.
Key Components
• Jenkins
• GitHub Actions
• Automated execution
• Scheduled runs
Practical Example
Bash
Common Mistakes
✕ Manual-only execution
✕ No reporting integration
✕ Ignoring pipeline failures
Page 22
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
1. POM
2. Reusability
3. Fixtures
4. Config Management
5. Reporting
6. CI/CD
Page 23
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
1. Handling Frames
What is it
Frames (iframes) are embedded HTML documents inside a web page.
Key Components
• iframe
• frameLocator()
• Nested frames
• Frame isolation
Practical Example
JavaScript
await [Link]('#login-frame')
.locator('#username')
.fill('Kalpesh');
Common Mistakes
✕ Trying to access iframe elements directly
✕ Using normal locators without frameLocator()
✕ Ignoring nested frames
What is it
Page 24
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Handling scenarios where clicking an element opens a new browser tab or window.
Key Components
• [Link]('page')
• New page event
• Switching tabs
• Browser context
Practical Example
JavaScript
[Link]('page'),
[Link]('#open-tab')
]);
await [Link]();
Common Mistakes
✕ Not waiting for new page event
✕ Confusing browser context and page
✕ Hardcoding tab switching logic
What is it
Handling file upload and file download scenarios in automation testing.
Key Components
• setInputFiles()
• Download event
• File path handling
Page 25
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
• Upload validation
JavaScript
await [Link]('input[type="file"]')
.setInputFiles('test-data/[Link]');
JavaScript
await [Link]('#download-btn');
Common Mistakes
✕ Using OS-level automation unnecessarily
✕ Hardcoding file paths
✕ Not validating downloaded file
Page 26
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
1. Network Interception ★
What is it
Network interception allows capturing, monitoring, modifying, or blocking API/network requests during test
execution.
Key Components
• [Link]()
• Request interception
• Response modification
• Blocking requests
Practical Example
JavaScript
await [Link]();
});
Common Mistakes
✕ Blocking all requests unnecessarily
✕ Incorrect URL patterns
✕ Not understanding request flow
2. API Mocking ★
Page 27
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
What is it
API mocking means simulating backend responses without hitting the actual server.
Key Components
• Mock responses
• [Link]()
• Fake API data
• Isolation testing
Practical Example
JavaScript
await [Link]({
status: 200,
});
});
Common Mistakes
✕ Mocking unnecessary APIs
✕ Returning invalid response structure
✕ Overusing mocks in all tests
3. Debugging Tools
What is it
Playwright provides debugging tools to identify failures and analyse test behaviour.
Key Components
Page 28
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
• [Link]()
• PWDEBUG
• Console logs
• Debug mode
Practical Example
Bash
Common Mistakes
✕ Leaving pause() in production code
✕ Ignoring logs
✕ Not using debugging tools properly
4. Trace Viewer ★
What is it
Trace Viewer is a Playwright tool used to visually analyse test execution step-by-step.
Key Components
• Screenshots
• DOM snapshots
• Network logs
• Execution timeline
Page 29
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
JavaScript
use: {
trace: "on-first-retry"
Open Trace
Bash
Common Mistakes
✕ Enabling tracing for all executions unnecessarily
✕ Not analysing traces properly
✕ Ignoring network logs
5. Parallel Execution ★
What is it
Running multiple tests simultaneously to reduce execution time.
Key Components
• Workers
• Parallel test execution
• Resource optimisation
• Independent tests
Practical Example
JavaScript
Page 30
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
// [Link]
workers: 4
Common Mistakes
✕ Shared test data issues
✕ Dependent test cases
✕ Environment conflicts
What is it
Retries help rerun failed tests automatically, while error handling manages failures gracefully.
Key Components
• retries
• try-catch
• Failure handling
• Flaky test management
Practical Example
JavaScript
// [Link]
retries: 2
Common Mistakes
✕ Overusing retries to hide real issues
✕ No proper logging
✕ Ignoring flaky tests
Page 31
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Feature Command
Page 32
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
1. APIRequestContext ★
What is it
APIRequestContext is Playwright's built-in API testing utility used to send HTTP requests directly without
browser interaction.
Key Components
• request
• API context
• HTTP requests
• Base URL
Practical Example
JavaScript
Common Mistakes
✕ Confusing browser page and API request
✕ Hardcoding endpoints
✕ Not validating responses
What is it
HTTP methods are operations used to interact with APIs.
Page 33
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Key Components
• GET — Fetch data
• POST — Create data
• PUT — Update data
• DELETE — Remove data
Practical Example
JavaScript
await [Link]('/users');
await [Link]('/users', {
});
Common Mistakes
✕ Using wrong HTTP methods
✕ Not validating response codes
✕ Sending incorrect payloads
3. Request Headers
What is it
Headers provide additional information in API requests such as authentication and content type.
Key Components
• Authorization
• Content-Type
• Accept
• Custom headers
Page 34
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Practical Example
JavaScript
await [Link]('/users', {
});
Common Mistakes
✕ Missing authentication headers
✕ Incorrect content types
✕ Hardcoding sensitive tokens
What is it
Authentication verifies user identity before accessing protected APIs.
Key Components
• Bearer token
• JWT token
• Authorization header
• Session handling
Practical Example
JavaScript
Page 35
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
headers: {
Common Mistakes
✕ Exposing tokens in code
✕ Expired tokens
✕ Incorrect token format
5. Response Validation
What is it
Validating API response body and returned data.
Key Components
• JSON validation
• Response body
• Field validation
• Schema checks
Practical Example
JavaScript
expect([Link]).toBe('Kalpesh');
Common Mistakes
✕ Validating only status codes
✕ Ignoring response body
✕ Weak assertions
Page 36
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
What is it
Validating API response status codes.
Key Components
• 200 OK
• 201 Created
• 400 Bad Request
• 401 Unauthorized
• 500 Server Error
Practical Example
JavaScript
expect([Link]()).toBe(200);
Common Mistakes
✕ Ignoring failure codes
✕ Hardcoding wrong expectations
✕ Not validating negative scenarios
7. API Chaining ★
What is it
Page 37
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Key Components
• Dynamic IDs
• Sequential requests
• Dependency handling
Practical Example
JavaScript
await [Link](`/users/${[Link]}`);
Common Mistakes
✕ Hardcoding IDs
✕ Ignoring dependency failures
✕ Poor response handling
What is it
Combining API and UI testing together for faster and more efficient automation.
Key Components
• Backend setup
• UI validation
• Faster execution
• Reduced UI dependency
Page 38
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
Practical Example
JavaScript
await [Link]();
await expect([Link]('Kalpesh')).toBeVisible();
Common Mistakes
✕ Creating all data through UI
✕ Ignoring backend validation
✕ Slow test setup
9. Best Practices ★
What is it
Guidelines for writing stable and maintainable API tests.
Key Components
• Reusable request methods
• Proper validations
• Secure token handling
• Avoid hardcoded data
Practical Example
JavaScript
Page 39
Playwright (JavaScript) — Complete Interview Guide Kalpesh Jain | @kalpeshnjain09
expect([Link]()).toBeTruthy();
Common Mistakes
✕ Hardcoded test data
✕ No negative testing
✕ Weak assertions
1. API Chaining
3. Response Validation
Validation Purpose
Page 40