How do I customize Playwright test options on a per-test basis similar to Protractor's onbeforelaunch and onprepare?


Customizing Playwright Test Options

To achieve functionality similar to Protractor's onBeforeLaunch and onPrepare, you can use the test.use() method in @playwright/test. This allows you to override test options dynamically on a per-test basis.

For instance, to set a specific locale for a particular test, use the following code:

import { test } from '@playwright/test';

test.use({ locale: 'fr-FR' });

test('French locale test', async ({ page }) => {
  await page.goto('https://ray.run/');
  // Your test code here

This overrides any global or project-level settings for that specific test.

You can also configure Playwright globally or per-project using the playwright.config.ts file. For example, to set a default browser name of Firefox for all tests in your project, add browserName: 'firefox' to your config file.

If you need more control over your configuration settings, such as setting up custom user agents or viewport sizes for different tests or projects, use explicit context creation and option inheritance. This involves calling browser.newContext() with custom options instead of relying on the built-in browser fixture.

By leveraging these features effectively, you can customize your testing environment and configuration settings based on your needs in @playwright/test.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.