User Agents

Web browsers and everyday web tech, explained

Run Playwright Scripts in Google Chrome: Step-by-Step

· 4 min read · Browsers

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

This guide shows you how to execute Playwright test scripts specifically in Google Chrome (instead of the default Chromium browser). Playwright supports Chrome via the channel: 'chrome' option, which uses your installed Chrome browser. This is useful when you need to test against the exact Chrome version your users have, or when you want to leverage Chrome-specific features. You'll need Node.js, npm, and a basic Playwright project set up.

What You'll Need

  • Node.js (version 16 or later) installed on your system.
  • Google Chrome installed (any recent stable version).
  • A code editor (like VS Code) and a terminal.
  • Basic familiarity with JavaScript/Node.js.

Step-by-Step

  1. Create a new project directory (if you don't have one):
    mkdir playwright-chrome-example
    cd playwright-chrome-example
  2. Initialize a Node.js project (accept defaults):
    npm init -y
  3. Install Playwright:
    npm install @playwright/test

    This installs the Playwright test runner and its libraries.

  4. Install the Chrome browser (if not already installed):
    npx playwright install chrome

    This ensures Playwright can find Chrome. It downloads the browser if needed, but it will use your system Chrome when you specify channel.

  5. Create a test file (e.g., example.spec.js):
    const { test, expect } = require('@playwright/test');
    
    test('open Google in Chrome', async ({ page }) => {
      await page.goto('https://www.google.com');
      await expect(page).toHaveTitle(/Google/);
    });
  6. Configure Playwright to use Chrome in playwright.config.js (create if missing):
    const { defineConfig } = require('@playwright/test');
    
    module.exports = defineConfig({
      use: {
        channel: 'chrome', // this tells Playwright to use Google Chrome
      },
    });

    Alternatively, you can specify the channel per project or per test.

  7. Run the test:
    npx playwright test

    Playwright will launch Google Chrome (not Chromium) and execute the test.

  8. Run in headed mode (to see the browser window):
    npx playwright test --headed

Common Mistakes

  • Forgetting to set channel: 'chrome' – without this, Playwright uses bundled Chromium.
  • Running npx playwright install without specifying chrome – this only downloads Chromium, not Chrome. Use npx playwright install chrome.
  • Using channel: 'chrome-beta' when you have stable Chrome installed – that will fail. Use the correct channel name for your installed version.
  • Not closing the browser properly – Playwright handles this automatically, but if you manually launch browsers, ensure you call close().

Tips

  • Use channel: 'chrome' in your config to make all tests run in Chrome by default.
  • For debugging, run with --headed and add page.pause() in your test to open the Playwright inspector.
  • Use --project=chromium if you have multiple projects defined, but ensure that project has channel: 'chrome'.
  • If you need to test Chrome on a specific operating system, use Playwright's Docker images or CI environments with Chrome pre-installed.

Troubleshooting

  • Error: "Executable doesn't exist" or "browserType.launch: Executable doesn't exist" – This means Playwright can't find Chrome. Run npx playwright install chrome again, or ensure Chrome is installed in a standard location.
  • Error: "Channel 'chrome' not found" – This usually means Chrome is not installed or the channel name is wrong. Check your Chrome installation and use chrome for stable, chrome-beta for beta, chrome-dev for dev, chrome-canary for canary.
  • Chrome launches but tests fail with network errors – This may be due to Chrome's sandbox. If you're running as root or in a container, you may need to launch with --no-sandbox (not recommended for production). In Playwright, you can pass launch options in config: use: { launchOptions: { args: ['--no-sandbox'] } }.
  • Tests run in Chromium instead of Chrome – Double-check your config file is being read. Ensure you're in the project root and the config file is named playwright.config.js or playwright.config.ts.

Frequently Asked Questions

Can I use Chrome for only some tests?

Yes, you can define multiple projects in playwright.config.js. One project can use channel: 'chrome' and another can use the default Chromium. Then run tests with --project=chrome.

What's the difference between Chromium and Chrome in Playwright?

Chromium is the open-source browser that Chrome is based on. Playwright downloads its own Chromium build. Chrome is Google's proprietary browser with additional codecs and features. Using channel: 'chrome' ensures you're testing against the exact Chrome you have installed.

Do I need to install Chrome separately?

Yes, you need Google Chrome installed on your system. Playwright will not download Chrome automatically; you must install it or run npx playwright install chrome which will download it if you don't have it.

Can I use Chrome in headless mode?

Yes, Playwright runs Chrome in headless mode by default. To see the UI, use --headed.

Why does my test run slower in Chrome than Chromium?

Chrome is a heavier browser with more features. Performance differences are usually minimal, but if you notice significant slowdowns, check for extensions or background processes in your Chrome profile. You can also use a clean profile by setting launchOptions: { args: ['--user-data-dir=/tmp/playwright-chrome'] }.

How do I run a single test file in Chrome?

Use npx playwright test example.spec.js – it will use the config's channel setting.