Playwright Test Use: How to Configure Browser Settings
In this article
The test.use method in Playwright allows you to override default browser, device, and context configurations for specific tests or groups of tests. By using this function, you can switch between different browsers (Chromium, Firefox, WebKit), change the viewport size, emulate specific mobile devices, or set geographic locations and permissions without modifying the global configuration file. This is essential for cross-browser testing and ensuring your application remains responsive across various environments.
What You'll Need
- Node.js installed on your machine.
- A Playwright project initialized (via
npm init playwright@latest). - Basic familiarity with JavaScript or TypeScript.
- A code editor, such as Visual Studio Code.
Step-by-Step
- Open your test file: Navigate to your
testsfolder and open the specific.spec.tsor.spec.jsfile where you want to apply a browser override. - Import the test function: Ensure you have the necessary import at the top of your file:
import { test, expect } from '@playwright/test';. - Apply
test.useat the file level: To apply a setting to every test within a single file, place thetest.usecall at the top level of the script, outside of anytestblocks. For example, to force all tests in that file to run in Firefox:test.use({ browserName: 'firefox' });. - Apply
test.usewithin atest.describeblock: If you only want a subset of tests to use a specific browser or configuration, wrap them in atest.describeblock and placetest.useinside that block. This ensures that only the tests within that group inherit the override. - Configure specific browser options: Beyond just the browser name, you can pass an object to
test.useto customize the environment. Common options include:viewport: { width: 1280, height: 720 }to set the window size.locale: 'en-GB'to change the language and region.timezoneId: 'Europe/London'to simulate a specific time zone.permissions: ['geolocation']to grant browser permissions automatically.
- Run the test: Execute your tests using the command line:
npx playwright test. Playwright will now ignore the globalplaywright.config.tssettings for these specific tests and use the values defined in yourtest.usecall.
Common Mistakes
- Placing
test.useinside atestblock:test.useis designed to configure thebrowserContextbefore the test starts. If you place it inside thetest('...', async ({ page }) => { ... })block, it will not work because the context has already been created. - Confusing
test.usewithbrowser.newContext(): While both create contexts,test.useis a declarative way to tell the Playwright Test runner how to set up the environment automatically.newContext()is for manual context management within a test. - Overriding global configs unintentionally: Applying
test.useat the top of a file overrides theprojectsconfiguration inplaywright.config.ts. This can lead to confusion when you expect a test to run across all projects but it only runs in one specific browser.
Tips
- Use for Mobile Emulation: Instead of manually setting the viewport and user agent, use the built-in devices library:
import { devices } from '@playwright/test';and thentest.use({ ...devices['iPhone 13'] });. - Combine with
test.describe: Organize your tests by browser or device using describe blocks. For example, one block for "Desktop Chrome" and another for "Mobile Safari" within the same file. - Prefer Config for Global Settings: Only use
test.usefor exceptions. If every test in your suite needs a specific viewport, it is cleaner to set this inplaywright.config.ts.
Troubleshooting
If your browser settings are not being applied, first check the placement of the test.use call. It must be outside the test(...) function. If you are using a test.describe block, ensure the test.use call is the first thing inside that block.
If the browser is not switching as expected, check your playwright.config.ts. If you have multiple projects defined, Playwright may still attempt to run the test across all projects unless you specifically filter the run using --project=chromium or similar flags in the CLI.
For issues with permissions or geolocation, ensure that the options are passed as an array (e.g., permissions: ['notifications']) and that you are using a browser engine that supports those specific features.
Frequently Asked Questions
Can I use test.use to change the browser for just one single test?
No, test.use cannot be placed inside an individual test block. To apply it to a single test, wrap that one test in a test.describe block and place the test.use call inside that block.
Does test.use override the playwright.config.ts file?
Yes, settings defined in test.use take precedence over the global configuration defined in the config file for the tests they affect.
Can I use test.use to set the browser's storage state?
Yes, you can use test.use({ storageState: 'state.json' }) to load cookies and local storage, allowing you to skip login steps for specific test groups.
Can I define multiple browsers in one test.use call?
No, test.use sets the configuration for the current context. To run tests across multiple browsers, you should define projects in your global config file or run the tests multiple times with different project flags.
Is test.use available in both JavaScript and TypeScript?
Yes, it is a core part of the Playwright Test runner and works identically in both languages.