User Agents

Web browsers and everyday web tech, explained

Playwright Test Use: How to Configure Browser Settings

· 4 min read · Settings and Privacy

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Open your test file: Navigate to your tests folder and open the specific .spec.ts or .spec.js file where you want to apply a browser override.
  2. Import the test function: Ensure you have the necessary import at the top of your file: import { test, expect } from '@playwright/test';.
  3. Apply test.use at the file level: To apply a setting to every test within a single file, place the test.use call at the top level of the script, outside of any test blocks. For example, to force all tests in that file to run in Firefox: test.use({ browserName: 'firefox' });.
  4. Apply test.use within a test.describe block: If you only want a subset of tests to use a specific browser or configuration, wrap them in a test.describe block and place test.use inside that block. This ensures that only the tests within that group inherit the override.
  5. Configure specific browser options: Beyond just the browser name, you can pass an object to test.use to 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.
  6. Run the test: Execute your tests using the command line: npx playwright test. Playwright will now ignore the global playwright.config.ts settings for these specific tests and use the values defined in your test.use call.

Common Mistakes

  • Placing test.use inside a test block: test.use is designed to configure the browserContext before the test starts. If you place it inside the test('...', async ({ page }) => { ... }) block, it will not work because the context has already been created.
  • Confusing test.use with browser.newContext(): While both create contexts, test.use is 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.use at the top of a file overrides the projects configuration in playwright.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 then test.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.use for exceptions. If every test in your suite needs a specific viewport, it is cleaner to set this in playwright.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.