Rayrun

How do I configure Playwright to run tests on different devices based on environment variables?

Answer

Sure, let's dive into how you can use environment variables to run your tests on different devices with @playwright/test.

First, you'll need to define different projects in your Playwright configuration file. Each project should be associated with a unique name and a specific browser or device. For instance, you can set up a project named "chromium" for desktop Chrome and another named "android" for Android WebView.

// playwright.config.ts
module.exports = {
  projects: [
    {
      name: 'chromium',
      use: { browserName: 'chromium' },
    },
    {
      name: 'android',
      use: { browserName: 'chromium', ...devices['Pixel 2'] },
    },
  ],
};

Next, set an environment variable to specify which project to run. If you want to run tests on desktop Chrome, set BROWSER=chromium. To run tests on Android WebView, set ANDROID=1.

Finally, run your tests with the npx playwright test command. Playwright's test runner will automatically pick up the environment variable and run your tests on the specified device.

BROWSER=chromium npx playwright test
// or
ANDROID=1 npx playwright test

That's it! With this setup, you can easily switch between running your tests on different devices just by changing an environment variable. Happy testing with Playwright!

For more tips on efficient testing with Playwright, check out this blog post.

References

Thank you!
Was this helpful?
Still have questions?

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

Related Questions

TwitterGitHubLinkedIn
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 [email protected].