Set Default Browser in Playwright MCP
In this article
Setting the default browser in Playwright MCP (Multi-Client Protocol) determines which browser your automated tests or scripts launch by default—such as Chromium, Firefox, or WebKit. This is essential for consistent test execution, debugging, and ensuring compatibility across browser engines without manually specifying the browser each time. You configure this at the project or test suite level using configuration files or launch options. No external tools are required, but you must have Playwright installed and initialized in your project.
What You'll Need
- Node.js installed (version 14 or higher)
- Playwright library installed via npm (
npm install @playwright/test) - An initialized Playwright configuration file (typically
playwright.config.tsorplaywright.config.js) - Basic understanding of JavaScript or TypeScript
- Terminal or command-line access to run tests
Step-by-Step
- Open your terminal and navigate to your project directory where Playwright is installed. Ensure the
playwright.config.ts(or.js) file exists. If not, generate it by runningnpx playwright installornpx playwright test --ui, which creates the config file if missing. - Open the
playwright.config.tsfile in your preferred code editor. This file controls global settings for your Playwright tests. - Locate the
projectsarray in the configuration. Each project defines a test environment, often tied to a specific browser. By default, Playwright includes three projects: one for Chromium, Firefox, and WebKit. - To set a default browser, reorder the projects array so your preferred browser appears first. For example, to make Firefox the default, move its project to the top:
When runningprojects: [ { name: 'firefox', use: { ...devices['Desktop Firefox'] }, }, { name: 'chromium', use: { ...devices['Desktop Chrome'] }, }, { name: 'webkit', use: { ...devices['Desktop Safari'] }, }, ],npx playwright testwithout specifying a project, the first one (Firefox) will execute by default. - Alternatively, you can define a custom project named 'default' and set it as the first entry. This improves clarity:
Now, the default project uses Chromium (Desktop Chrome) unless another is specified.projects: [ { name: 'default', use: { ...devices['Desktop Chrome'] }, }, { name: 'firefox', use: { ...devices['Desktop Firefox'] }, }, ] - Save the configuration file. Run your tests using
npx playwright test. The default browser will now launch automatically. - To override the default temporarily, use the
--projectflag:npx playwright test --project=webkit. This runs tests in WebKit without changing the default. - (Optional) If using TypeScript, rebuild your project with
tscif type checking is enabled, to ensure no syntax errors were introduced.
Common Mistakes
- Editing the wrong config file: Ensure you are modifying the correct
playwright.config.tsor.jsfile in your project root. Multiple config files can exist in monorepos. - Not reordering projects: Simply changing the
usesettings within a project without moving it to the top won’t make it the default. The order in theprojectsarray is what determines the default execution. - Forgetting case sensitivity: Browser names like 'chromium' and 'Chrome' are not interchangeable. Use exact names as defined in the Playwright documentation: 'chromium', 'firefox', 'webkit'.
- Running tests with explicit project flags: If you always use
--project=firefoxin your scripts, the default setting won’t matter. Review CI/CD or npm scripts to ensure they don’t override the default. - Assuming launch options set the default: Using
browserType.launch()in a test file with a specific browser doesn’t change the global default. Configuration-level settings take precedence.
Tips
- Use descriptive project names: Instead of generic names, label your default project clearly, such as 'default-chrome' or 'ci-default', to improve readability in logs and CI outputs.
- Leverage environment variables: In CI pipelines, use
process.envin the config file to dynamically set the default browser based on deployment context. - Test locally first: After changing the default, run a simple test locally to confirm the correct browser launches before pushing changes.
- Document your default: Add a comment in the config file noting which browser is default and why, especially in team environments.
- Use Playwright UI mode: Run
npx playwright test --uito visually confirm which browser launches by default and interactively debug.
Troubleshooting
If the default browser doesn’t launch as expected, first verify the projects array order in the config file. Run npx playwright test --list to see the execution order of projects. If the wrong browser runs, check for npm scripts or CI configurations that include --project flags. Clear any cached configurations by deleting the node_modules/.cache directory and reinstalling Playwright if issues persist. Ensure no typos exist in device names or project syntax. If using TypeScript, check for compilation errors that might prevent the config from loading. Finally, confirm Playwright is updated to the latest version using npm outdated @playwright/test and update if necessary with npm update @playwright/test, as older versions may handle defaults differently.
Frequently Asked Questions
Can I set the default browser without using the projects array?
No. The default browser is determined solely by the order of entries in the projects array in the Playwright config file. There is no global defaultBrowser option outside of this structure.
Does setting a default browser affect all test files?
Yes. Unless a specific project is targeted using the --project flag, all test files will run using the first project in the projects array, regardless of their content.
What happens if I remove two of the three default browsers?
The remaining project becomes the default automatically, as it is the only and therefore first entry. This simplifies execution but limits cross-browser testing unless manually invoked.
Can I use Chrome instead of Chromium as the default?
Yes. Use devices['Desktop Chrome'] in the use property of your default project. Note that Chromium is the default engine, but Chrome can be specified if installed on the system.
Is the default browser setting respected in headed mode?
Yes. Whether running in headed (--headed) or headless mode, the default browser from the config file is used unless overridden by a project flag.
Do I need to reinstall browsers after changing the default?
No. Playwright downloads browser binaries once. Changing the default only affects launch behavior, not installation. Binaries remain available for all configured browsers.