Playwright MCP Extension: How to Use It
In this article
The Playwright MCP browser extension helps developers record, inspect, and generate automated browser tests directly from user interactions in Chrome. By capturing actions like clicks, form fills, and navigations, it translates real user behavior into executable Playwright scripts, speeding up test creation and improving accuracy. This guide walks you through setup, usage, and troubleshooting to ensure you get reliable, maintainable test code.
What You'll Need
- A compatible browser (Google Chrome or Microsoft Edge)
- Playwright installed in your development environment (via npm or another package manager)
- An active project directory where you want to save generated test scripts
- Basic familiarity with JavaScript or TypeScript
- Admin rights to install browser extensions (if required by your system)
Step-by-Step
- Open the Chrome Web Store or Microsoft Edge Add-ons store in your browser.
- Search for "Playwright MCP" using the search bar. Ensure the official extension by Microsoft is selected.
- Click "Add to Chrome" or "Get" to install the extension. Confirm any permission prompts.
- Once installed, click the puzzle piece icon in the browser toolbar to pin the Playwright MCP extension for quick access.
- Navigate to the web application you want to test. Make sure it’s fully loaded and interactive.
- Click the Playwright MCP icon in the toolbar. The control panel should appear, usually as a popup or sidebar.
- In the panel, select the programming language for output (JavaScript, TypeScript, or Python, depending on your project setup).
- Click the "Start Recording" button. The extension will now track your interactions on the page.
- Perform the user actions you want to automate—click buttons, fill forms, navigate between pages, and scroll as needed.
- Use the pause/resume feature in the panel if you need to skip irrelevant actions or add delays intentionally.
- When finished, click "Stop Recording" in the extension panel.
- Review the generated script in the preview window. The extension shows a step-by-step translation of your actions into Playwright commands.
- Check selectors for accuracy. The extension may use auto-generated selectors (like data-testid or CSS paths); verify they are stable and not prone to breaking on UI changes.
- Click "Copy Code" to copy the script to your clipboard, or use "Save As" to export it directly to your project directory if integrated with your file system.
- Paste the script into your test file (e.g.,
example.spec.ts) in your Playwright project. - Run the test using your Playwright test command (e.g.,
npx playwright test) to verify it works as expected. - Refactor the script if needed—add assertions, error handling, or custom logic to improve reliability.
Common Mistakes
- Recording on a development build with unstable IDs: Using elements with dynamically generated classes or IDs can cause flaky tests. Prefer elements with stable
data-testidattributes. - Ignoring generated selectors: The extension may use long CSS or XPath selectors. Always review and simplify them to make tests more maintainable.
- Recording too many actions in one test: Long, complex recordings create brittle tests. Break workflows into smaller, focused test cases.
- Forgetting to stop the recorder: Leaving the recorder running can capture unintended navigation or clicks, polluting the script.
- Not validating test output: Assuming the generated code works without running it can lead to false confidence. Always execute and debug the test immediately.
Tips
- Add
data-testidattributes to key elements in your app during development to make selector generation more reliable. - Use the extension in incognito mode to avoid interference from other extensions or cached data.
- Record one user journey at a time—like login, search, or checkout—to keep tests modular and easier to debug.
- Combine recorded scripts with manual enhancements: add
expect()assertions, retries, or custom waits for robustness. - Regularly update the Playwright MCP extension to benefit from bug fixes and new selector strategies.
- Use the "Pause" feature during recording to insert manual delays or comments in the script for clarity.
Troubleshooting
If the extension does not appear after installation, restart the browser and check that it’s enabled in chrome://extensions. If the recorder fails to capture actions, ensure you’re not in an iframe or a page with strict Content Security Policy (CSP) rules that block script injection. If generated selectors are flaky, manually edit them to use text content, ARIA roles, or unique attributes. If the script fails during playback, verify that the test environment matches the recording environment—same base URL, authentication state, and viewport size. For export issues, confirm file permissions in your project directory and ensure Playwright is correctly installed and configured. If the extension crashes or freezes, disable other browser extensions temporarily to rule out conflicts.
Frequently Asked Questions
Can I use Playwright MCP with Firefox or Safari?
No, the Playwright MCP extension only works with Chromium-based browsers like Chrome and Edge. However, Playwright itself supports Firefox and WebKit—just run the generated script in those browsers via code configuration.
Does the extension work on single-page applications (SPAs)?
Yes, it captures interactions in SPAs effectively, including client-side routing and dynamic content. Ensure the app is fully rendered before starting the recording.
Can I edit the generated script?
Absolutely. The generated code is a starting point. You should edit it to add assertions, improve readability, and handle edge cases.
Is the extension free to use?
Yes, the Playwright MCP extension is free and maintained by Microsoft as part of the open-source Playwright project.
Why are some clicks or inputs not recorded?
This can happen if elements are obscured, not interactive, or loaded asynchronously. Wait for elements to be fully visible and clickable before interacting.
Can I record file uploads?
The extension may not capture file uploads directly. You’ll need to manually add file upload logic using Playwright’s setInputFiles() method after recording.