Playwright Automation with Claude
Asking Claude for a Playwright test is easy. Knowing whether the test is right is the hard part. This page teaches Playwright Test itself: the setup, the test structure, browser contexts, locators, assertions, fixtures, and the debug tools. Then it shows how to use Claude to create, explain, debug, and refactor tests, and how to review each generated test before you trust it. If you know the tool, you can see when Claude is wrong.
17 min read17 মিনিটে পড়া যায় · 11 sections11টি সেকশন · 22 sources22টি সোর্স · updated Nov 2026আপডেট: Nov 2026
Each test gets its own browser contextপ্রতিটা test পায় নিজের browser context
Run two tests and see their storage stay apart, then break the isolation.দুটো test চালিয়ে দেখুন তাদের storage আলাদা থাকে, তারপর isolation ভেঙে দেখুন।
You’ll learnআপনি শিখবেন
- Browser, context, page: three layersbrowser, context, page: তিনটা স্তর
- Each test gets its own contextপ্রতিটা test পায় নিজের context
- Isolation stops cascading failuresisolation ব্যর্থতার ধাপে ধাপে ছড়ানো আটকায়
- Fixtures and several contexts in one testfixture, আর এক test-এ একাধিক context
Install and configure Playwright Test
Install. One command starts a new project or adds Playwright to an existing one: Source 1সোর্স 1
npm init playwright@latestThe command asks four questions: Source 1সোর্স 1
- TypeScript or JavaScript. The default is TypeScript.
- The tests folder. The default is
tests, ore2eiftestsalready exists. - Whether to add a GitHub Actions workflow.
- Whether to install the Playwright browsers. The default is yes.
You can run it again later. It does not overwrite existing tests. Source 1সোর্স 1
What it creates. Playwright downloads the browser binaries and writes playwright.config.ts, package.json, a lock file, and tests/example.spec.ts. Source 1সোর্স 1
Browsers and system. Playwright supports Chromium, WebKit, and Firefox on Windows, Linux, and macOS. Source 1সোর্স 1 It needs Node.js 22.x, 24.x, or 26.x (the latest of each). Source 1সোর্স 1 By default, tests run headless and in parallel across Chromium, Firefox, and WebKit. Source 1সোর্স 1
Version. The newest entry in the release notes, read on 2026-11-15, is version 1.65. Source 2সোর্স 2 Check your version with npx playwright --version. Update with npm install -D @playwright/test@latest, then npx playwright install --with-deps. Source 1সোর্স 1
Configure. playwright.config.ts holds the target browsers, timeouts, retries, projects, and reporters. Source 1সোর্স 1 These keys are common: Source 3সোর্স 3
| Key | Example value from the docs |
|---|---|
testDir | 'tests' |
fullyParallel | true |
forbidOnly | !!process.env.CI |
retries | process.env.CI ? 2 : 0 |
use.baseURL | 'http://localhost:3000' |
সহজ English
One command sets up Playwright: npm init playwright@latest. It works for a new project and for an existing one. Source 1সোর্স 1
It asks you four things: TypeScript or JavaScript, the name of the tests folder, whether to add a GitHub Actions workflow, and whether to install browsers. Source 1সোর্স 1 You can run it again later. It does not overwrite your tests. Source 1সোর্স 1
It downloads the browsers. It also writes a config file, playwright.config.ts, and one example test. Source 1সোর্স 1
Playwright runs Chromium, WebKit, and Firefox. It needs Node.js 22, 24, or 26. Source 1সোর্স 1 Tests run headless by default. Source 1সোর্স 1
The newest version in the release notes on 2026-11-15 is 1.65. Source 2সোর্স 2 Run npx playwright --version to see your version. Source 1সোর্স 1
The config file holds the browsers, timeouts, retries, projects, and reporters. Source 1সোর্স 1
বাংলা ব্যাখ্যা
একটি command দিয়েই Playwright set up হয়: npm init playwright@latest। এটি নতুন project শুরু করে, অথবা আগের project-এ Playwright যোগ করে। Source 1সোর্স 1
Command-টি চারটি প্রশ্ন করে: TypeScript না JavaScript (default TypeScript), tests folder-এর নাম (default tests, আর tests থাকলে e2e), GitHub Actions workflow যোগ করবেন কি না, আর browser install করবেন কি না (default হ্যাঁ)। Source 1সোর্স 1 পরে আবার চালালেও এটি আপনার আগের test overwrite করে না। Source 1সোর্স 1
এটি browser binary download করে এবং playwright.config.ts, package.json, একটি lock file আর tests/example.spec.ts তৈরি করে। Source 1সোর্স 1
Playwright Windows, Linux ও macOS-এ Chromium, WebKit আর Firefox চালায়। এর জন্য Node.js 22.x, 24.x বা 26.x লাগে। Source 1সোর্স 1 Default-ভাবে test headless আর parallel চলে। Source 1সোর্স 1
2026-11-15 তারিখে release notes-এর সবচেয়ে নতুন version হলো 1.65। Source 2সোর্স 2 নিজের version দেখতে npx playwright --version চালান। Source 1সোর্স 1
playwright.config.ts-এ browser, timeout, retry, project আর reporter এক জায়গায় থাকে। Source 1সোর্স 1 সাধারণ key হলো testDir, fullyParallel, forbidOnly, retries আর use.baseURL। Source 3সোর্স 3
Write and run a test
Structure. A test file imports test and expect from @playwright/test. Source 4সোর্স 4 A test performs actions and asserts the state against expectations. Source 4সোর্স 4
test.describegroups tests. Source 4সোর্স 4test.beforeEachandtest.afterEachrun before and after each test. Source 4সোর্স 4test.beforeAllandtest.afterAllrun once per worker. Source 4সোর্স 4- The
pagefixture gives each test a fresh page. Source 4সোর্স 4 page.goto()waits for the page to reach the load state. Source 4সোর্স 4
The example below is the tutorial's own. It follows the pattern of the docs:
import { test, expect } from '@playwright/test';
test.describe('login page', () => {
test.beforeEach(async ({ page }) => {
await page.goto('https://staging.example.com/login');
});
test('shows an error for a wrong password', async ({ page }) => {
await page.getByLabel('Email').fill('qa-user@example.com');
await page.getByLabel('Password').fill('wrong-password');
await page.getByRole('button', { name: 'Sign in' }).click();
await expect(page.getByText('Invalid email or password')).toBeVisible();
});
});Run. These commands come from the docs: Source 5সোর্স 5
| Command | What it does |
|---|---|
npx playwright test | Runs all tests, headless |
npx playwright test --headed | Shows the browser window |
npx playwright test --ui | Opens UI mode |
npx playwright test --project webkit | Runs one project only |
npx playwright test login.spec.ts | Runs one file |
npx playwright test -g "wrong password" | Runs the tests with that title |
npx playwright test --last-failed | Runs only the tests that failed last time |
npx playwright show-report | Opens the HTML report |
The HTML report opens by itself if some tests failed. Source 5সোর্স 5
Projects. A project is a group of tests with the same configuration. Source 6সোর্স 6 Use one project for each browser, or run the same tests logged in and logged out. Source 6সোর্স 6
সহজ English
A test file imports test and expect from @playwright/test. Source 4সোর্স 4 A test does some actions. Then it checks the result.
test.describeputs tests in a group. Source 4সোর্স 4test.beforeEachruns before each test. Source 4সোর্স 4test.beforeAllruns one time per worker. Source 4সোর্স 4- The
pagefixture gives each test a new, clean page. Source 4সোর্স 4
You run tests from the terminal. Source 5সোর্স 5
npx playwright testruns all tests.--headedshows the browser.--uiopens UI mode.--project webkitruns one project only.-gruns the tests whose title matches.--last-failedruns again only the tests that failed.npx playwright show-reportopens the HTML report.
The report opens by itself when a test fails. Source 5সোর্স 5
A project is a group of tests with the same settings. You can have one project for each browser. Source 6সোর্স 6
বাংলা ব্যাখ্যা
একটি test file @playwright/test থেকে test আর expect import করে। Source 4সোর্স 4 Test কিছু action করে, তারপর ফলাফল assert করে।
test.describeকয়েকটি test-কে একটি group-এ রাখে। Source 4সোর্স 4test.beforeEachআরtest.afterEachপ্রতিটি test-এর আগে ও পরে চলে। Source 4সোর্স 4test.beforeAllআরtest.afterAllপ্রতি worker-এ একবার চলে। Source 4সোর্স 4pagefixture প্রতিটি test-কে একটি নতুন page দেয়। Source 4সোর্স 4page.goto()page-টি load state-এ পৌঁছানো পর্যন্ত অপেক্ষা করে। Source 4সোর্স 4
Terminal থেকে test চালান: npx playwright test সব test চালায়, --headed browser window দেখায়, --ui UI mode খোলে, --project webkit শুধু একটি project চালায়, -g title দিয়ে test বাছে, আর --last-failed শুধু আগের বার fail করা test আবার চালায়। Source 5সোর্স 5 কোনো test fail করলে HTML report নিজে থেকে খোলে। অন্য সময় npx playwright show-report চালান। Source 5সোর্স 5
Project হলো একই configuration-এর একদল test। প্রতিটি browser-এর জন্য একটি project রাখা যায়, অথবা একই test logged in আর logged out অবস্থায় চালানো যায়। Source 6সোর্স 6
Browser, context, page, and fixtures
Three layers. Playwright can create multiple browser contexts in one scenario, and a page belongs to a context. Source 7সোর্স 7
- A browser context is like an incognito profile. Contexts are fast to create and fully isolated, even in one browser. Source 7সোর্স 7
- Playwright creates a context for each test and gives a default page in it. Source 7সোর্স 7
- So each test has its own local storage, session storage, and cookies. Source 7সোর্স 7 This isolation prevents cascading test failures. Source 7সোর্স 7
- One test can create several contexts, for example an admin and a user in a chat flow. Source 7সোর্স 7
Fixtures. A fixture sets up the environment for a test: everything it needs and nothing else. Fixtures are isolated between tests. Source 8সোর্স 8
| Built-in fixture | What it gives |
|---|---|
page | An isolated page for this test Source 8সোর্স 8 |
context | The isolated context that the page belongs to Source 8সোর্স 8 |
browser | The browser, shared across tests to save resources Source 8সোর্স 8 |
browserName | chromium, firefox, or webkit Source 8সোর্স 8 |
request | An isolated APIRequestContext for API calls Source 8সোর্স 8 |
Custom fixtures. test.extend() creates a new test object with your own fixture. Source 8সোর্স 8 The code before await use() is the setup. The code after it is the teardown. Source 8সোর্স 8 Playwright sets up only the fixtures that a test uses. Source 8সোর্স 8
The example below is the tutorial's own. It gives each test a loginPage page object:
import { test as base } from '@playwright/test';
import { LoginPage } from './pages/LoginPage';
export const test = base.extend<{ loginPage: LoginPage }>({
loginPage: async ({ page }, use) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await use(loginPage);
},
});
export { expect } from '@playwright/test';A test then asks for it by name: test('...', async ({ loginPage }) => { ... }).
সহজ English
Think of three layers. A browser can run several browser contexts. A context is like an incognito profile. A page belongs to a context. Source 7সোর্স 7
Playwright makes a new context for each test. Source 7সোর্স 7 So each test has its own cookies and storage. A test that fails does not leave a mess for the next test. One test can also open two contexts, for example an admin and a user who chat. Source 7সোর্স 7
A fixture is something that a test needs, ready before the test starts. Source 8সোর্স 8 page is a fixture. Others are context, browser, browserName, and request. Source 8সোর্স 8
You can make your own fixture with test.extend(). Source 8সোর্স 8 The code before await use() prepares it. The code after await use() cleans up. Source 8সোর্স 8 Playwright prepares only the fixtures that a test asks for. Source 8সোর্স 8
বাংলা ব্যাখ্যা
তিনটি স্তর মনে রাখুন: browser, browser context আর page। Browser context একটি incognito profile-এর মতো। একই browser-এর ভেতরেও প্রতিটি context পুরোপুরি আলাদা। Source 7সোর্স 7
Playwright প্রতিটি test-এর জন্য একটি নতুন context আর তার ভেতরে একটি default page তৈরি করে। Source 7সোর্স 7 তাই প্রতিটি test-এর নিজের local storage, session storage আর cookie থাকে। একটি test-এ একাধিক context-ও বানানো যায়, যেমন chat flow-তে একজন admin আর একজন user। Source 7সোর্স 7
Fixture হলো test-এর environment: test-এর যা দরকার ঠিক ততটুকু। Fixture এক test থেকে আরেক test-এ আলাদা থাকে। Source 8সোর্স 8 Built-in fixture হলো page, context, browser (সব test-এ shared), browserName আর request। Source 8সোর্স 8
নিজের fixture বানাতে test.extend() ব্যবহার করুন। Source 8সোর্স 8 await use()-এর আগের code হলো setup, পরের code হলো teardown। Source 8সোর্স 8 যে fixture কোনো test চায়, Playwright শুধু সেটিই set up করে। Source 8সোর্স 8 Technical panel-এর loginPage fixture-টি tutorial-এর নিজের উদাহরণ।
Locators that survive change
A locator is a way to find an element on the page at any moment. Each time an action uses it, Playwright finds an up-to-date element. Source 9সোর্স 9
The recommended built-in locators, in the order that the docs list them: Source 9সোর্স 9
| Locator | Finds an element by |
|---|---|
getByRole() | Its accessibility role and name, for example a button named "Sign in" |
getByText() | Its text content |
getByLabel() | The text of the label of a form control |
getByPlaceholder() | The placeholder of an input |
getByAltText() | The text alternative of an image |
getByTitle() | Its title attribute |
getByTestId() | Its data-testid attribute (you can configure another one) |
Priority. The docs recommend role locators first, because they are closest to how users and assistive technology see the page. Source 9সোর্স 9 Use text locators for non-interactive elements such as div and span. Source 9সোর্স 9 Test ids are the most resilient, but they are not user facing. Source 9সোর্স 9 To use another attribute, set testIdAttribute in use of the config. Source 9সোর্স 9
Chaining and filtering. Chain locators to narrow the search to one part of the page. Source 10সোর্স 10
await page
.getByRole('listitem')
.filter({ hasText: 'Product 2' })
.getByRole('button', { name: 'Add to cart' })
.click();Strictness. Locators are strict. An action throws an error if more than one element matches. Source 9সোর্স 9 first(), last(), and nth() turn this off, but the docs do not recommend them: when the page changes, Playwright can click an element that you did not intend. Source 9সোর্স 9
Why CSS and XPath break. Selectors tied to the DOM structure break when the DOM changes. Source 9সোর্স 9 A selector on CSS classes breaks when a designer changes a class. Source 10সোর্স 10 The docs do not recommend CSS or XPath. Source 9সোর্স 9
// Brittle: tied to the DOM
await page.locator('#tsf > div:nth-child(2) > div.A8SBwf > input').click();
// Resilient: what the user sees
await page.getByRole('button', { name: 'submit' }).click();Codegen also picks locators in this order: role, text, then test id. Source 11সোর্স 11
সহজ English
A locator tells Playwright how to find an element, such as a button or a text box. Playwright looks for the element again each time it acts. Source 9সোর্স 9
The docs list these built-in locators, and they recommend getByRole() first: Source 9সোর্স 9
getByRole()finds an element by what it is and its name, like "the button named Sign in". The docs recommend it first, because users see the page this way.getByText()finds an element by its text. Use it for text that you do not click.getByLabel()finds a form field by its label.getByTestId()finds an element by its test id. It is very stable, but users do not see it.
You can chain locators. For example, first find a list item, then the button inside it. Source 10সোর্স 10
Locators are strict. If two elements match, the action fails with an error. Source 9সোর্স 9 The docs do not recommend nth() for this. The page can change, and then Playwright can click the wrong element. Source 9সোর্স 9
Long CSS or XPath paths follow the structure of the HTML. They break when the HTML changes. Source 9সোর্স 9
বাংলা ব্যাখ্যা
Locator হলো page-এ কোনো element খোঁজার উপায়। প্রতিটি action-এর সময় Playwright element-টি নতুন করে খোঁজে। Source 9সোর্স 9
Docs যে built-in locator-গুলো recommend করে: getByRole(), getByText(), getByLabel(), getByPlaceholder(), getByAltText(), getByTitle() আর getByTestId()। Source 9সোর্স 9 সবার আগে role locator ব্যবহার করুন, কারণ user আর assistive technology page-কে এভাবেই দেখে। Source 9সোর্স 9 div বা span-এর মতো non-interactive element-এর জন্য text locator ব্যবহার করুন। Source 9সোর্স 9 Test id সবচেয়ে টেকসই, কিন্তু user এটি দেখে না। Source 9সোর্স 9
Locator chain করে page-এর একটি অংশে খোঁজা সীমিত করা যায়, আর filter({ hasText }) দিয়ে বাছাই করা যায়। Source 10সোর্স 10
Locator strict: একাধিক element মিললে action error দেয়। Source 9সোর্স 9 first(), last() বা nth() দিয়ে এটি এড়ানো যায়, কিন্তু docs এগুলো recommend করে না, কারণ page বদলালে Playwright ভুল element-এ click করতে পারে। Source 9সোর্স 9
DOM structure-এর সাথে বাঁধা CSS বা XPath, DOM বদলালেই ভেঙে যায়। Source 9সোর্স 9 Designer একটি CSS class বদলালেও test ভাঙে। Source 10সোর্স 10 Codegen-ও role, text আর test id-কে আগে রাখে। Source 11সোর্স 11
Assertions and auto-waiting
Auto-waiting before actions. Before an action, Playwright runs actionability checks and waits until they pass. If they do not pass within the timeout, the action fails with a TimeoutError. Source 12সোর্স 12 For click(), the element must: Source 12সোর্স 12
- resolve to exactly one element,
- be visible and stable (not animating),
- receive events (no overlay on top of it),
- be enabled and not inert.
The force option turns off the non-essential checks. Source 12সোর্স 12 So you do not need manual waits. Source 4সোর্স 4
Web-first assertions. An auto-retrying assertion checks the page again and again until the condition is true or the assertion timeout ends. Source 13সোর্স 13 These assertions are async, so you must await them. Source 13সোর্স 13
| Kind | Examples | Waits? |
|---|---|---|
| Auto-retrying | toBeVisible(), toHaveText(), toHaveURL(), toHaveCount(), toHaveValue() | Yes Source 13সোর্স 13 |
| Non-retrying | toBe(), toEqual(), toBeTruthy(), toContain() | No. They can make a test flaky. Source 13সোর্স 13 |
For a complex check that must retry, use expect.poll or expect.toPass. Source 13সোর্স 13 A soft assertion (expect.soft) does not stop the test, but it marks the test as failed. Source 13সোর্স 13
The wrong way and the right way: Source 10সোর্স 10
// Does not wait: checks once and returns
expect(await page.getByText('welcome').isVisible()).toBe(true);
// Waits and retries until the text is visible
await expect(page.getByText('welcome')).toBeVisible();Timeouts. The defaults are 5 seconds for each assertion Source 13সোর্স 13 and 30 seconds for each test. Source 14সোর্স 14
The tutorial's advice
Treat a fixed sleep, such as page.waitForTimeout(3000), as a smell in a generated test. It makes the test slow when the app is fast and flaky when the app is slow. Wait for the state that the user sees with a web-first assertion instead. If a test needs more time, find out why before you raise a timeout.
সহজ English
Before Playwright clicks, it waits. It checks that the element is there, visible, still, not covered, and enabled. Source 12সোর্স 12 If this takes too long, the action fails with a timeout error. Source 12সোর্স 12 So you do not need to add your own waits. Source 4সোর্স 4
An assertion is a check, like "the welcome text is visible". Web-first assertions try again and again until the check is true or time runs out. Source 13সোর্স 13 Always put await before them. Source 13সোর্স 13
Some checks do not wait, like toBe() and toEqual(). They check one time only. This can make a test flaky. Source 13সোর্স 13
Do not write expect(await page.getByText('welcome').isVisible()).toBe(true). It does not wait. Write await expect(page.getByText('welcome')).toBeVisible(). Source 10সোর্স 10
An assertion waits 5 seconds by default. Source 13সোর্স 13 A test times out after 30 seconds by default. Source 14সোর্স 14
The tutorial's advice: a fixed sleep in a test is a warning sign. Wait for what the user sees instead.
বাংলা ব্যাখ্যা
কোনো action-এর আগে Playwright actionability check চালায় এবং সেগুলো pass করা পর্যন্ত অপেক্ষা করে। সময়ের মধ্যে pass না করলে action TimeoutError দিয়ে fail করে। Source 12সোর্স 12 click()-এর জন্য element-কে ঠিক একটি হতে হবে, visible, stable, enabled আর not inert হতে হবে, এবং event গ্রহণ করতে হবে। Source 12সোর্স 12 তাই নিজে থেকে manual wait যোগ করার দরকার নেই। Source 4সোর্স 4
Web-first assertion শর্তটি সত্য না হওয়া বা timeout শেষ না হওয়া পর্যন্ত বারবার check করে। এগুলো async, তাই অবশ্যই await দিন। Source 13সোর্স 13 toBe(), toEqual()-এর মতো non-retrying assertion অপেক্ষা করে না, তাই test flaky হতে পারে। Source 13সোর্স 13
ভুল উপায়: expect(await page.getByText('welcome').isVisible()).toBe(true), কারণ এটি একবার দেখেই ফেরত আসে। সঠিক উপায়: await expect(page.getByText('welcome')).toBeVisible()। Source 10সোর্স 10
Default timeout হলো প্রতিটি assertion-এর জন্য 5 সেকেন্ড Source 13সোর্স 13 আর প্রতিটি test-এর জন্য 30 সেকেন্ড। Source 14সোর্স 14
Tutorial-এর পরামর্শ: generated test-এ page.waitForTimeout(3000)-এর মতো fixed sleep দেখলে সন্দেহ করুন। এর বদলে user যা দেখে, সেই অবস্থার জন্য web-first assertion দিয়ে অপেক্ষা করুন।
Debug a failing test
This section covers four debug tools: UI mode, the trace viewer, the Inspector, and codegen. Learn them before you ask Claude to fix a test, so you can check its fix.
UI mode (npx playwright test --ui). You explore, run, and debug tests with time travel and a watch mode. Source 15সোর্স 15 The pick locator button shows the locator for each element of the DOM snapshot. Source 15সোর্স 15
Trace viewer. A trace records each action of a test. Source 16সোর্স 16
| Command or setting | What it does |
|---|---|
npx playwright test --trace on | Records a trace for each test, during development Source 16সোর্স 16 |
trace: 'on-first-retry' | Records a trace only on the first retry, for CI Source 16সোর্স 16 |
trace: 'retain-on-failure' | Records each test, but keeps only the traces of failed tests Source 16সোর্স 16 |
npx playwright show-trace path/to/trace.zip | Opens a saved trace Source 16সোর্স 16 |
trace: 'on' for every test is not recommended, because it is performance heavy. Source 16সোর্স 16 In the Actions tab, you see the locator and the duration of each action, with Before and After snapshots. Source 16সোর্স 16 The trace viewer loads the trace in your browser and sends no data out. Source 16সোর্স 16
Inspector (--debug). It opens the Playwright Inspector and a headed browser, with the default timeout set to 0. Source 17সোর্স 17 To debug one test, give the file and the line: npx playwright test example.spec.ts:10 --debug. Source 5সোর্স 5 Add page.pause() to stop at one point. Source 17সোর্স 17
Codegen (npx playwright codegen <url>). It records your actions as test code. Source 11সোর্স 11 From the toolbar, you can add three assertions: visibility, text, and value. Source 11সোর্স 11 --save-storage saves cookies, localStorage, and IndexedDB data at the end of the session. Source 11সোর্স 11
The tutorial's advice
Codegen code is a first draft. It records what you clicked, and it adds only the assertions that you choose. Add the checks that matter for the requirement before you keep the test.
সহজ English
Playwright gives you four tools to find out why a test fails.
- UI mode (
--ui) lets you run a test and step back and forward in time. It also has a pick locator button. Source 15সোর্স 15 - The trace viewer shows a recording of each step of a test. Source 16সোর্স 16 Use
--trace onwhile you work. Source 16সোর্স 16 In CI, record a trace only on the first retry, because recording every test is heavy. Source 16সোর্স 16 Open a trace withnpx playwright show-trace. It stays in your browser and sends no data out. Source 16সোর্স 16 - The Inspector opens with
--debug, together with a headed browser. Source 17সোর্স 17page.pause()stops the test at one point. Source 17সোর্স 17 - Codegen records your clicks and typing as test code. Source 11সোর্স 11 You can add simple checks from its toolbar. Source 11সোর্স 11
The tutorial's advice: codegen gives you a first draft. Add the real checks yourself.
বাংলা ব্যাখ্যা
Test কেন fail করছে, তা খুঁজতে Playwright-এর চারটি tool আছে। Claude-কে test ঠিক করতে বলার আগে এগুলো শিখুন, যাতে তার fix যাচাই করতে পারেন।
- UI mode (
--ui): time travel আর watch mode দিয়ে test explore, run ও debug করা যায়। Source 15সোর্স 15 Pick locator button দিয়ে DOM snapshot-এর element-এর locator দেখা যায়। Source 15সোর্স 15 - Trace viewer: development-এ
--trace onদিন। CI-তেtrace: 'on-first-retry'ব্যবহার করুন, এটি শুধু প্রথম retry-তে trace রাখে।trace: 'retain-on-failure'শুধু fail করা test-এর trace রাখে। প্রতিটি test-এ trace চালু রাখা performance-এর জন্য ভারী। Source 16সোর্স 16npx playwright show-traceদিয়ে trace খুলুন। Actions tab-এ প্রতিটি action-এর locator, সময় আর Before ও After snapshot দেখা যায়। Source 16সোর্স 16 - Inspector:
--debugদিলে headed browser আর Inspector খোলে, default timeout হয় 0। Source 17সোর্স 17 একটি নির্দিষ্ট জায়গায় থামতেpage.pause()দিন। Source 17সোর্স 17 - Codegen:
npx playwright codegen <url>আপনার action-কে test code হিসেবে record করে। Toolbar থেকে visibility, text আর value assertion যোগ করা যায়। Source 11সোর্স 11
Tutorial-এর পরামর্শ: codegen-এর code প্রথম খসড়া মাত্র। Requirement-এর জন্য দরকারি check নিজে যোগ করুন।
Use Claude to create, explain, and refactor tests
The tutorial's view: getting a draft from Claude is the easy part. Your job is to make Claude prove that the test is right.
Give Claude the live page. The tutorial's advice: a live page gives Claude the real roles and names to use, so do not let it guess locators from the source code alone. Two tools let it read the running app:
- Playwright MCP. Add it with
claude mcp add playwright npx @playwright/mcp@latest. It needs Node.js 20 or newer. Source 18সোর্স 18 It works through structured accessibility snapshots: the page elements with their roles and text. Source 18সোর্স 18 - Playwright CLI. Install it with
npm install -g @playwright/cli@latest, thenplaywright-cli install --skills. Source 19সোর্স 19playwright-cli snapshotcaptures the page, andplaywright-cli generate-locator <ref>gives a Playwright locator for one element. Source 19সোর্স 19 The README says that CLI calls use fewer tokens than MCP. Source 19সোর্স 19
Ask for evidence. Ask Claude for the command that it ran and its output. A claim that "all tests pass" alone tells you very little. Source 20সোর্স 20 Put the check in the prompt, for example "run the tests after, and keep going until they pass". Source 20সোর্স 20
Get a second reviewer. /code-review reviews the diff in a fresh subagent that never saw your conversation. Source 20সোর্স 20 A reviewer that you ask to find problems will always find some, so ask it to skip style. Source 20সোর্স 20
Example prompts. These prompts are the tutorial's own:
Explain this spec line by line. For each locator, say why you chose it
and what happens if two elements match. For each assertion, say what
change in the app would make it fail.Refactor tests/login.spec.ts into a LoginPage page object and a
loginPage fixture. Do not change any assertion. Run the tests before
and after, and show me both outputs and the git diff.The Test Agents. Playwright also ships three agents, planner, generator, and healer, which you set up with npx playwright init-agents --loop=claude. Source 21সোর্স 21 The Browser Testing with Playwright page covers them and Playwright MCP in depth.
সহজ English
The tutorial's view: getting a draft from Claude is the easy part. Your job is to make Claude show that the test is right.
The tutorial's advice: let Claude see the real page, not only the code, so it does not guess locators.
- Playwright MCP lets Claude read the running app. Add it with
claude mcp add playwright npx @playwright/mcp@latest. Source 18সোর্স 18 Claude reads a snapshot of the page: each element with its role and text. Source 18সোর্স 18 - The Playwright CLI is a command-line tool for the same job.
playwright-cli generate-locatorgives a locator for one element. Source 19সোর্স 19 Its README says that it uses fewer tokens than MCP. Source 19সোর্স 19
Ask Claude for proof: the command that it ran and the output. "All tests pass" alone tells you very little. Source 20সোর্স 20
/code-review checks the change with a fresh helper that did not see your chat. Source 20সোর্স 20 A reviewer that you ask to find problems will always find some, so tell it to skip style. Source 20সোর্স 20
Ask Claude to explain each line, and to say what would make each check fail. When it refactors, tell it not to change any check.
Playwright also has three test agents: planner, generator, and healer. Source 21সোর্স 21 The Browser Testing with Playwright page explains them.
বাংলা ব্যাখ্যা
Tutorial-এর মত: Claude-এর কাছ থেকে draft পাওয়া সহজ অংশ। আপনার কাজ হলো Claude-কে দিয়ে প্রমাণ করানো যে test-টি সঠিক।
Tutorial-এর পরামর্শ: চলমান page Claude-কে আসল role আর name দেয়, তাই শুধু source code থেকে locator অনুমান করতে না দিয়ে চলমান app দেখার সুযোগ দিন:
- Playwright MCP:
claude mcp add playwright npx @playwright/mcp@latestদিয়ে যোগ করুন। এর জন্য Node.js 20 বা নতুন version লাগে। Source 18সোর্স 18 এটি structured accessibility snapshot-এ কাজ করে, যেখানে প্রতিটি element-এর role আর text থাকে। Source 18সোর্স 18 - Playwright CLI:
npm install -g @playwright/cli@latestআর তারপরplaywright-cli install --skillsচালান। Source 19সোর্স 19playwright-cli generate-locator <ref>একটি element-এর locator দেয়। README বলে, CLI call-এ MCP-র চেয়ে কম token লাগে। Source 19সোর্স 19
Claude-এর কাছে প্রমাণ চান: কোন command চালিয়েছে আর তার output। শুধু "সব test pass করেছে" কথাটি প্রায় কিছুই বলে না। Source 20সোর্স 20 /code-review একটি নতুন subagent-এ diff review করে, যে আপনার conversation দেখেনি। Source 20সোর্স 20 Reviewer-কে সমস্যা খুঁজতে বললে সে সবসময় কিছু না কিছু পাবে, তাই style বাদ দিতে বলুন। Source 20সোর্স 20
Tutorial-এর নিজের prompt-এ Claude-কে প্রতিটি line ব্যাখ্যা করতে বলুন, আর refactor করার সময় কোনো assertion না বদলাতে বলুন।
Playwright-এর planner, generator আর healer নামে তিনটি agent-ও আছে, যা npx playwright init-agents --loop=claude দিয়ে set up হয়। Source 21সোর্স 21 বিস্তারিত Browser Testing with Playwright পেজে আছে।
Review a generated test
The tutorial's checklist
Use this checklist on every test that Claude writes or changes. Each row names the Playwright behavior that it protects.
| Check | Why it matters |
|---|---|
| Each test has at least one assertion on what the user sees. | A test with no assertion passes when the feature is broken. |
Each web-first assertion has await before expect. | Retrying assertions are async and must be awaited. Source 13সোর্স 13 A lint rule, @typescript-eslint/no-floating-promises, finds a missing await. Source 10সোর্স 10 |
No isVisible() inside a plain expect. | That pattern checks once and does not wait. Source 10সোর্স 10 |
No fixed sleep such as waitForTimeout. | Playwright already waits before actions and in web-first assertions. Source 4সোর্স 4 |
| Locators use role, label, text, or test id. No long CSS or XPath chain. | DOM-tied selectors break when the DOM changes. Source 9সোর্স 9 |
No nth(), first(), or last() without a reason in a comment. | They can click an element that you did not intend. Source 9সোর্স 9 |
No force: true without a reason. | It turns off non-essential actionability checks. Source 12সোর্স 12 |
| The test does not depend on another test or on run order. | Each test gets its own context. Source 7সোর্স 7 |
| No password, token, or real user data in the code. | Use environment variables and test accounts. |
| The assertion can fail. | Break the expected value on purpose and watch the test fail. |
| Claude showed the command and its output. | A claim of success alone tells you very little. Source 20সোর্স 20 |
Read the diff yourself before you merge. If a change to a test makes an expected value weaker, ask why, and check the requirement.
সহজ English
The tutorial gives this checklist for every test that Claude writes or changes:
- The test checks something that the user sees. A test with no check passes even when the feature is broken.
- Each web-first check has
awaitin front. Source 13সোর্স 13 A lint rule can find a missingawait. Source 10সোর্স 10 - The test does not use
isVisible()inside a plainexpect. That does not wait. Source 10সোর্স 10 - The test has no fixed sleep. Playwright already waits. Source 4সোর্স 4
- Locators use role, label, text, or test id, not long CSS or XPath. Source 9সোর্স 9
- The test does not use
nth(),first(), orlast()without a good reason. Source 9সোর্স 9 - The test does not use
force: truewithout a reason. It turns off some of the checks that Playwright makes before an action. Source 12সোর্স 12 - The test does not need another test to run first. Source 7সোর্স 7
- The code has no password, token, or real user data.
- The check can fail. Break it on purpose and see it fail.
- Claude showed the command and the output. Source 20সোর্স 20
Read the diff yourself before you merge.
বাংলা ব্যাখ্যা
Claude যে test লেখে বা বদলায়, তার প্রতিটিতে tutorial-এর এই checklist ব্যবহার করুন:
- প্রতিটি test-এ user যা দেখে তার ওপর অন্তত একটি assertion আছে। Assertion না থাকলে feature ভাঙা থাকলেও test pass করে।
- প্রতিটি web-first assertion-এর আগে
awaitআছে। Source 13সোর্স 13@typescript-eslint/no-floating-promiseslint rule বাদ পড়াawaitধরে। Source 10সোর্স 10 - সাধারণ
expect-এর ভেতরেisVisible()নেই, কারণ এটি অপেক্ষা করে না। Source 10সোর্স 10 waitForTimeout-এর মতো fixed sleep নেই। Playwright নিজেই অপেক্ষা করে। Source 4সোর্স 4- Locator হলো role, label, text বা test id। লম্বা CSS বা XPath নেই। Source 9সোর্স 9
- কারণ ছাড়া
nth(),first()বাlast()নেই। Source 9সোর্স 9 - কারণ ছাড়া
force: trueনেই, কারণ এটি non-essential actionability check বন্ধ করে। Source 12সোর্স 12 - Test অন্য কোনো test বা চালানোর ক্রমের ওপর নির্ভর করে না। Source 7সোর্স 7
- Code-এ কোনো password, token বা আসল user data নেই।
- Assertion fail করতে পারে: ইচ্ছা করে ভুল value দিয়ে দেখুন।
- Claude command আর তার output দেখিয়েছে। Source 20সোর্স 20
Merge করার আগে diff নিজে পড়ুন।
Practice
The tutorial's exercise
This exercise is the tutorial's own. It uses the web app of the dMoney practice repo, which the Codebase Tour describes. Run it only on your local copy, with a test account.
Where these facts come from
The facts about dMoney come from the files of the public repository at commit 51db34a. They have no cites, because a cite links to an official source. Facts about Playwright and Claude Code link to the official docs.
What dMoney has. The Playwright project is in dmoney-web-react/. Its config uses testDir: './e2e', keeps traces with trace: 'retain-on-failure', and has a setup project and a test project for each of the admin and agent roles (admin depends on admin-setup). Its webServer starts the web app and the API, or reuses running servers. e2e/pages/LoginPage.ts finds the fields with getByLabel and the button with getByRole. An admin login has no OTP step and lands on /profile. The admin credentials come from ADMIN_EMAIL and ADMIN_PASSWORD. The repo asks for @playwright/test ^1.63.0.
-
Explore. Start Claude Code in
dmoney-web-react/, with Playwright MCP added. Source 18সোর্স 18 Type:Open http://localhost:3000/login with Playwright MCP. List each field and button with its role and accessible name. Do not submit the form. -
Write. Type:
Write e2e/tests/admin/admin-login.spec.ts. Start logged out with test.use({ storageState: { cookies: [], origins: [] } }). Use LoginPage. Read the credentials from ADMIN_EMAIL and ADMIN_PASSWORD. Assert that the URL ends with /profile. Use no fixed waits. Run it with npx playwright test e2e/tests/admin/admin-login.spec.ts --project=admin and show me the output.test.use({ storageState: { cookies: [], origins: [] } })resets the storage state, so the test is not signed in. Source 22সোর্স 22 The tutorial expects one passing test in the output. -
Explain. Ask Claude to explain the spec line by line. Check each answer against the sections above.
-
Review. Go through the checklist of the section "Review a generated test". Fix each failed row.
-
Break it. Change the expected URL to
/dashboardand run the test again. It must fail after the assertion timeout of 5 seconds, unless the call sets its own timeout. Source 13সোর্স 13 -
Debug. Open the HTML report with
npx playwright show-report, then the trace of the failed test. Source 5সোর্স 5 In the Actions tab, find the failed assertion and look at the Before and After snapshots. Source 16সোর্স 16 -
Restore. Put
/profileback, run the test again, and readgit diff. Only the new spec should be in it.
সহজ English
This exercise is the tutorial's own. Use your local copy of the dMoney practice app and a test account. The Codebase Tour page describes the app.
The dMoney facts come from the public repo at commit 51db34a. Its Playwright project is in dmoney-web-react/. It keeps a trace when a test fails. The admin login has no OTP step and ends on /profile. The admin login details come from the variables ADMIN_EMAIL and ADMIN_PASSWORD.
- Add Playwright MCP to Claude Code. Source 18সোর্স 18 Ask Claude to open the login page and list each field and button. Tell it not to submit the form.
- Ask Claude to write
e2e/tests/admin/admin-login.spec.ts. The test starts logged out, usesLoginPage, reads the login details from the variables, and checks that the address ends with/profile. Ask it to run the test and show the output. - Ask Claude to explain each line.
- Go through the review checklist.
- Change
/profileto/dashboard. Run again. The test must fail. An assertion waits 5 seconds by default before it fails. Source 13সোর্স 13 - Open the report with
npx playwright show-report, then the trace. Source 5সোর্স 5 Look at the failed step and its Before and After pictures. Source 16সোর্স 16 - Put
/profileback. Run again, and readgit diff.
বাংলা ব্যাখ্যা
এই exercise tutorial-এর নিজের। dMoney practice app-এর local copy আর একটি test account ব্যবহার করুন। Codebase Tour পেজে app-টির বর্ণনা আছে।
dMoney-র তথ্য commit 51db34a-এর public repo থেকে নেওয়া। Playwright project আছে dmoney-web-react/-এ। Test fail করলে trace রাখা হয়। Admin login-এ কোনো OTP ধাপ নেই, এটি /profile-এ গিয়ে শেষ হয়। Admin-এর login তথ্য আসে ADMIN_EMAIL আর ADMIN_PASSWORD variable থেকে।
- Claude Code-এ Playwright MCP যোগ করুন। Source 18সোর্স 18 Claude-কে login page খুলে প্রতিটি field আর button-এর role ও নাম জানাতে বলুন। Form submit করতে নিষেধ করুন।
- Claude-কে
e2e/tests/admin/admin-login.spec.tsলিখতে বলুন: logged out অবস্থায় শুরু,LoginPageব্যবহার, variable থেকে login তথ্য, আর URL/profile-এ শেষ হয় কি না assert। Test চালিয়ে output দেখাতে বলুন। - Claude-কে প্রতিটি line ব্যাখ্যা করতে বলুন।
- Review checklist ধরে test-টি যাচাই করুন।
/profileবদলে/dashboardলিখে আবার চালান। Test-টি fail করতেই হবে। Default-ভাবে একটি assertion fail করার আগে 5 সেকেন্ড অপেক্ষা করে। Source 13সোর্স 13npx playwright show-reportদিয়ে report আর তারপর trace খুলুন। Source 5সোর্স 5 Actions tab-এ fail করা assertion আর তার Before ও After snapshot দেখুন। Source 16সোর্স 16/profileফিরিয়ে দিন, আবার চালান, আরgit diffপড়ুন।
Common mistakes
| Mistake | Fix |
|---|---|
| You accept a generated test because Claude says it passes. | Ask for the command and its output. A claim of success alone tells you very little. Source 20সোর্স 20 |
| The test uses a fixed sleep to "fix" a flaky step. | Remove it. Use a web-first assertion on the state that the user sees. Playwright waits before each action. Source 4সোর্স 4 |
The assertion does not wait or cannot fail: expect(await locator.isVisible()).toBe(true) checks once and does not wait, Source 10সোর্স 10 and a check on a value that the test set itself cannot fail. | Use an auto-retrying assertion with await. Source 10সোর্স 10 Then break the expected value on purpose and watch the test fail (the tutorial's advice). |
| Claude picks a long CSS or XPath selector from the source code. | Give Claude the live page with Playwright MCP or the Playwright CLI. Source 18সোর্স 18 Prefer role locators. Source 9সোর্স 9 |
A strict mode error is "fixed" with nth() or first(). | Make the locator unique with a filter or a chain. Source 9সোর্স 9 |
Traces are on for every test in CI. | Use on-first-retry or retain-on-failure. on is performance heavy. Source 16সোর্স 16 |
| A refactor quietly changes an expected value. | Ask Claude not to change assertions, and read the diff yourself (the tutorial's advice). |
সহজ English
- You trust "it passes". Ask Claude for the command and the output. Source 20সোর্স 20
- A fixed sleep hides a flaky step. Remove it. Wait for what the user sees. Playwright already waits before each action. Source 4সোর্স 4
- A weak check.
expect(await locator.isVisible()).toBe(true)checks once and does not wait, and a check on a value that the test set itself cannot fail. Use a web-first check withawait. Source 10সোর্স 10 Then break it on purpose and see it fail. - Claude guesses a long CSS path. Let Claude see the real page with Playwright MCP. Source 18সোর্স 18 Use role locators first. Source 9সোর্স 9
- Two elements match, and the test uses
nth(). Make the locator unique instead. Source 9সোর্স 9 - Traces are on for every test in CI. That is heavy. Use
on-first-retryorretain-on-failure. Source 16সোর্স 16 - A refactor changes a check. Tell Claude not to change checks. Read the diff yourself.
বাংলা ব্যাখ্যা
- "Pass করেছে" কথায় বিশ্বাস করা। Claude-এর কাছে command আর output চান। Source 20সোর্স 20
- Flaky ধাপ ঢাকতে fixed sleep। এটি সরিয়ে দিন। User যা দেখে, তার জন্য web-first assertion দিয়ে অপেক্ষা করুন। Playwright প্রতিটি action-এর আগে নিজেই অপেক্ষা করে। Source 4সোর্স 4
- দুর্বল assertion।
expect(await locator.isVisible()).toBe(true)একবার দেখেই ফেরত আসে, অপেক্ষা করে না। আর test নিজে যে value বসিয়েছে, তার check কখনো fail করে না।awaitসহ auto-retrying assertion ব্যবহার করুন। Source 10সোর্স 10 তারপর ইচ্ছা করে ভুল value দিয়ে fail হতে দেখুন। - Source code থেকে লম্বা CSS বা XPath। Playwright MCP দিয়ে Claude-কে চলমান page দেখান। Source 18সোর্স 18 Role locator আগে ব্যবহার করুন। Source 9সোর্স 9
- Strict mode error
nth()দিয়ে "ঠিক" করা। Filter বা chain দিয়ে locator-কে unique করুন। Source 9সোর্স 9 - CI-তে প্রতিটি test-এ trace
on। এটি ভারী।on-first-retryবাretain-on-failureব্যবহার করুন। Source 16সোর্স 16 - Refactor-এ চুপচাপ expected value বদলানো। Claude-কে assertion না বদলাতে বলুন এবং diff নিজে পড়ুন।
Key words
| Term | Meaning |
|---|---|
| Playwright Test | The Playwright test framework that this page teaches: test, expect, and fixtures. |
| Browser context | An isolated, incognito-like profile. Playwright creates one for each test. |
| Fixture | The environment that a test needs, such as page. You add your own with test.extend(). |
| Locator | A way to find an element on the page at the moment of each action. |
| Role locator | getByRole(): finds an element by its accessibility role and name. The docs recommend it first. |
| Strictness | An action on a locator throws an error if more than one element matches. |
| Actionability checks | The checks that Playwright waits for before an action, such as visible, stable, and enabled. |
| Web-first assertion | An await expect(...) that retries until the condition is true or the timeout ends. |
| Trace viewer | A tool that shows each recorded action of a test, with its locator and snapshots. |
| UI mode | --ui: run, watch, and debug tests with time travel. |
| Codegen | npx playwright codegen: records your actions in the browser as test code. |
| Playwright MCP | A tool that you add to Claude Code with claude mcp add. It reads the page through accessibility snapshots. |