From 92a16517aadaabdcba20b72601d1fba18ca4ec3a Mon Sep 17 00:00:00 2001 From: "microsoft-playwright-automation[bot]" <203992400+microsoft-playwright-automation[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:20:51 +0000 Subject: [PATCH] docs(component-testing): split guides into a dedicated category Fixes: https://github.com/microsoft/playwright/issues/44328 --- nodejs/docs/test-components-faq.mdx | 136 ++++++++++ .../docs/test-components-getting-started.mdx | 210 +++++++++++++++ nodejs/docs/test-components-introduction.mdx | 165 ++++++++++++ nodejs/docs/test-components-migration.mdx | 183 +++++++++++++ nodejs/docs/test-components-stories.mdx | 255 ++++++++++++++++++ nodejs/docs/test-components.mdx | 1 + nodejs/sidebars.js | 13 +- .../version-stable/test-components-faq.mdx | 136 ++++++++++ .../test-components-getting-started.mdx | 210 +++++++++++++++ .../test-components-introduction.mdx | 165 ++++++++++++ .../test-components-migration.mdx | 183 +++++++++++++ .../test-components-stories.mdx | 255 ++++++++++++++++++ .../version-stable/test-components.mdx | 1 + .../version-stable-sidebars.json | 31 ++- src/generator.js | 57 ++++ tests/nodejs.spec.ts | 29 ++ 16 files changed, 2025 insertions(+), 5 deletions(-) create mode 100644 nodejs/docs/test-components-faq.mdx create mode 100644 nodejs/docs/test-components-getting-started.mdx create mode 100644 nodejs/docs/test-components-introduction.mdx create mode 100644 nodejs/docs/test-components-migration.mdx create mode 100644 nodejs/docs/test-components-stories.mdx create mode 100644 nodejs/versioned_docs/version-stable/test-components-faq.mdx create mode 100644 nodejs/versioned_docs/version-stable/test-components-getting-started.mdx create mode 100644 nodejs/versioned_docs/version-stable/test-components-introduction.mdx create mode 100644 nodejs/versioned_docs/version-stable/test-components-migration.mdx create mode 100644 nodejs/versioned_docs/version-stable/test-components-stories.mdx diff --git a/nodejs/docs/test-components-faq.mdx b/nodejs/docs/test-components-faq.mdx new file mode 100644 index 00000000000..a02913621f6 --- /dev/null +++ b/nodejs/docs/test-components-faq.mdx @@ -0,0 +1,136 @@ +--- +id: test-components-faq +title: "Frequently asked questions" +--- +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; +import HTMLCard from '@site/src/components/HTMLCard'; + +## Frequently asked questions + +### How do I access the component's methods or its instance? + +Accessing a component's internal methods or its instance within test code is neither recommended nor supported. Instead, focus on observing and interacting with the component from a user's perspective — click it, look at the page, and record internal effects into the DOM through the story. Tests become less fragile and more valuable when they avoid implementation details. If a test fails when run from a user's perspective, it likely means the automated test has uncovered a genuine bug. + +### Can I keep using my bundler plugins, aliases and CSS setup? + +Yes — that is the core of the design. The gallery is served by your own dev server, so whatever your app can render, your stories can render. There is no second bundler config to keep in sync. + +### What about frameworks other than React and Vue? + +Ask your coding agent to implement the gallery contract for your framework: resolve a story id to a component, render it into `#root`, reuse the root across calls so `update()` preserves state. The `mount` fixture does not know or care which framework is on the other side. + + +[APIRequest]: /api/class-apirequest.mdx "APIRequest" +[APIRequestContext]: /api/class-apirequestcontext.mdx "APIRequestContext" +[APIResponse]: /api/class-apiresponse.mdx "APIResponse" +[APIResponseAssertions]: /api/class-apiresponseassertions.mdx "APIResponseAssertions" +[Browser]: /api/class-browser.mdx "Browser" +[BrowserContext]: /api/class-browsercontext.mdx "BrowserContext" +[BrowserServer]: /api/class-browserserver.mdx "BrowserServer" +[BrowserType]: /api/class-browsertype.mdx "BrowserType" +[CDPSession]: /api/class-cdpsession.mdx "CDPSession" +[Clock]: /api/class-clock.mdx "Clock" +[ConsoleMessage]: /api/class-consolemessage.mdx "ConsoleMessage" +[Coverage]: /api/class-coverage.mdx "Coverage" +[Credentials]: /api/class-credentials.mdx "Credentials" +[Debugger]: /api/class-debugger.mdx "Debugger" +[Dialog]: /api/class-dialog.mdx "Dialog" +[Disposable]: /api/class-disposable.mdx "Disposable" +[Download]: /api/class-download.mdx "Download" +[ElementHandle]: /api/class-elementhandle.mdx "ElementHandle" +[FileChooser]: /api/class-filechooser.mdx "FileChooser" +[Frame]: /api/class-frame.mdx "Frame" +[FrameLocator]: /api/class-framelocator.mdx "FrameLocator" +[GenericAssertions]: /api/class-genericassertions.mdx "GenericAssertions" +[JSHandle]: /api/class-jshandle.mdx "JSHandle" +[Keyboard]: /api/class-keyboard.mdx "Keyboard" +[Locator]: /api/class-locator.mdx "Locator" +[LocatorAssertions]: /api/class-locatorassertions.mdx "LocatorAssertions" +[Logger]: /api/class-logger.mdx "Logger" +[Mouse]: /api/class-mouse.mdx "Mouse" +[Page]: /api/class-page.mdx "Page" +[PageAssertions]: /api/class-pageassertions.mdx "PageAssertions" +[Playwright]: /api/class-playwright.mdx "Playwright" +[PlaywrightAssertions]: /api/class-playwrightassertions.mdx "PlaywrightAssertions" +[Request]: /api/class-request.mdx "Request" +[Response]: /api/class-response.mdx "Response" +[Route]: /api/class-route.mdx "Route" +[Screencast]: /api/class-screencast.mdx "Screencast" +[Selectors]: /api/class-selectors.mdx "Selectors" +[SnapshotAssertions]: /api/class-snapshotassertions.mdx "SnapshotAssertions" +[TimeoutError]: /api/class-timeouterror.mdx "TimeoutError" +[Touchscreen]: /api/class-touchscreen.mdx "Touchscreen" +[Tracing]: /api/class-tracing.mdx "Tracing" +[Video]: /api/class-video.mdx "Video" +[WebError]: /api/class-weberror.mdx "WebError" +[WebMCP]: /api/class-webmcp.mdx "WebMCP" +[WebSocket]: /api/class-websocket.mdx "WebSocket" +[WebSocketRoute]: /api/class-websocketroute.mdx "WebSocketRoute" +[WebStorage]: /api/class-webstorage.mdx "WebStorage" +[Worker]: /api/class-worker.mdx "Worker" +[Electron]: /api/class-electron.mdx "Electron" +[ElectronApplication]: /api/class-electronapplication.mdx "ElectronApplication" +[Android]: /api/class-android.mdx "Android" +[AndroidDevice]: /api/class-androiddevice.mdx "AndroidDevice" +[AndroidInput]: /api/class-androidinput.mdx "AndroidInput" +[AndroidSocket]: /api/class-androidsocket.mdx "AndroidSocket" +[AndroidWebView]: /api/class-androidwebview.mdx "AndroidWebView" +[Fixtures]: /api/class-fixtures.mdx "Fixtures" +[FullConfig]: /api/class-fullconfig.mdx "FullConfig" +[FullProject]: /api/class-fullproject.mdx "FullProject" +[Location]: /api/class-location.mdx "Location" +[Test]: /api/class-test.mdx "Test" +[TestConfig]: /api/class-testconfig.mdx "TestConfig" +[TestInfo]: /api/class-testinfo.mdx "TestInfo" +[TestInfoError]: /api/class-testinfoerror.mdx "TestInfoError" +[TestOptions]: /api/class-testoptions.mdx "TestOptions" +[TestProject]: /api/class-testproject.mdx "TestProject" +[TestStepInfo]: /api/class-teststepinfo.mdx "TestStepInfo" +[WorkerInfo]: /api/class-workerinfo.mdx "WorkerInfo" +[Reporter]: /api/class-reporter.mdx "Reporter" +[Suite]: /api/class-suite.mdx "Suite" +[TestCase]: /api/class-testcase.mdx "TestCase" +[TestError]: /api/class-testerror.mdx "TestError" +[TestResult]: /api/class-testresult.mdx "TestResult" +[TestRun]: /api/class-testrun.mdx "TestRun" +[TestStep]: /api/class-teststep.mdx "TestStep" +[Element]: https://developer.mozilla.org/en-US/docs/Web/API/element "Element" +[EvaluationArgument]: /evaluating.mdx#evaluation-argument "EvaluationArgument" +[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise" +[iterator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols "Iterator" +[origin]: https://developer.mozilla.org/en-US/docs/Glossary/Origin "Origin" +[selector]: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors "selector" +[Serializable]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable" +[UIEvent.detail]: https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail "UIEvent.detail" +[UnixTime]: https://en.wikipedia.org/wiki/Unix_time "Unix Time" +[xpath]: https://developer.mozilla.org/en-US/docs/Web/XPath "xpath" + +[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal "AbortSignal" +[Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array" +[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean" +[Buffer]: https://nodejs.org/api/buffer.html#buffer_class_buffer "Buffer" +[ChildProcess]: https://nodejs.org/api/child_process.html "ChildProcess" +[Date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date "Date" +[Error]: https://nodejs.org/api/errors.html#errors_class_error "Error" +[EventEmitter]: https://nodejs.org/api/events.html#events_class_eventemitter "EventEmitter" +[function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function "Function" +[FormData]: https://developer.mozilla.org/en-US/docs/Web/API/FormData "FormData" +[Map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map "Map" +[Metadata]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object<string, any>" +[null]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null "null" +[number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number" +[Object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object" +[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise" +[Readable]: https://nodejs.org/api/stream.html#stream_class_stream_readable "Readable" +[ReadStream]: https://nodejs.org/api/fs.html#class-fsreadstream "ReadStream" +[RegExp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp "RegExp" +[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "string" +[void]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined "void" +[URL]: https://nodejs.org/api/url.html "URL" +[URLPattern]: https://developer.mozilla.org/en-US/docs/Web/API/URLPattern "URLPattern" +[URLSearchParams]: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams "URLSearchParams" + +[all available image tags]: https://mcr.microsoft.com/en-us/product/playwright/about "all available image tags" +[Microsoft Artifact Registry]: https://mcr.microsoft.com/en-us/product/playwright/about "Microsoft Artifact Registry" +[Dockerfile.noble]: https://github.com/microsoft/playwright/blob/main/utils/docker/Dockerfile.noble "Dockerfile.noble" diff --git a/nodejs/docs/test-components-getting-started.mdx b/nodejs/docs/test-components-getting-started.mdx new file mode 100644 index 00000000000..934ff39a2e9 --- /dev/null +++ b/nodejs/docs/test-components-getting-started.mdx @@ -0,0 +1,210 @@ +--- +id: test-components-getting-started +title: "Getting started" +--- +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; +import HTMLCard from '@site/src/components/HTMLCard'; + +## Getting started + +### Step 1: Point your coding agent at the skill + +The gallery is application code — it belongs to you, not to Playwright. The fastest way to get one is to not write it yourself: Playwright ships this entire methodology as an agent skill. Install the skills and ask your coding agent (Claude Code, GitHub Copilot or similar) to do the setup: + +```bash +npx playwright init-skills +``` + +```txt +Set up component testing using the playwright-component-testing skill. +``` + +The agent detects your framework and bundler, implements the gallery for your stack, adds a Playwright project to the config, and writes the first story and spec. + +The contract the gallery fulfills is small and worth knowing, even if you never open the file: +- It is a single page under `playwright/gallery/`, served by **your own dev server** — Vite apps serve it with the dev server they already run; other setups run a small standalone Vite server next to the app. +- It discovers your `*.story.*` files and exposes two functions: `window.mount({ story, props })` renders the story with the given id into a `#root` element, and `window.unmount()` tears it down. An unknown story or a render error rejects, which surfaces as the test's `mount()` call throwing. +- It reuses the rendering root across calls, so `component.update(props)` reconciles instead of remounting and component state is preserved. +- It imports your global CSS the same way the app entry does, and the body of `window.mount` is the natural place for app-wide setup — the equivalent of the old `beforeMount`/`afterMount` hooks. + +If you prefer to write the gallery by hand, the installed skill contains the full specification with worked React and Vue examples in `references/gallery-spec.md` — the whole page is a few dozen lines. + +### Step 2: Configure Playwright + +Add a project to your `playwright.config.ts` and point `baseURL` at the gallery: + +```js title="playwright.config.ts" +import { defineConfig, devices } from '@playwright/test'; + +export default defineConfig({ + projects: [ + { + name: 'components', + testDir: './tests/components', + use: { + ...devices['Desktop Chrome'], + baseURL: 'http://localhost:5173/playwright/gallery/index.html', + serviceWorkers: 'block', + reuseContext: true, + }, + }, + ], + webServer: { + command: 'npm run dev', + url: 'http://localhost:5173/playwright/gallery/index.html', + reuseExistingServer: !process.env.CI, + }, +}); +``` + +`mount` navigates to `baseURL`, so it must point at the gallery. `serviceWorkers: 'block'` keeps the app's own service worker from serving cached responses that would shadow your `page.route()` mocks. `reuseContext: true` reuses the browser context between tests in a worker — a large speedup for component suites, and the same optimization the experimental packages applied implicitly. + +### Step 3: Write a story + +Stories live next to the component they exercise. Each named export is one scenario: + +```js title="src/components/Button.story.tsx" +import { Button } from './Button'; + +export const Primary = () =>