User Agents

Web browsers and everyday web tech, explained

Run Playwright Scripts in Microsoft Edge Guide

· 5 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

Running a Playwright script in Microsoft Edge allows you to automate web testing and scraping specifically within the Chromium-based Edge environment. Because Playwright supports multiple browser engines, you can target Edge by specifying the channel property in your browser launch configuration, ensuring your scripts interact with the actual Edge binary installed on your system rather than a generic Chromium build.

What You'll Need

  • Node.js: Installed on your machine (LTS version recommended).
  • Microsoft Edge: The browser must be installed on the local system.
  • A Code Editor: Visual Studio Code is highly recommended for its Playwright extension.
  • Terminal Access: Command Prompt, PowerShell, or a Bash terminal.

Step-by-Step

  1. Initialize Your Project: Open your terminal and create a new directory for your project. Navigate into that directory and run npm init -y to create a package.json file.
  2. Install Playwright: Run the command npm init playwright@latest. During the setup process, you will be asked to choose between TypeScript or JavaScript; select your preference. When asked if you want to add a GitHub Actions workflow, choose yes or no based on your CI/CD needs.
  3. Install Browser Binaries: Although the init command usually handles this, ensure all necessary browsers are installed by running npx playwright install. This ensures the core drivers are present.
  4. Configure the Script for Edge: Create a new test file (e.g., edge-test.spec.ts). To force Playwright to use Microsoft Edge instead of the default Chromium, you must set the channel to 'msedge'. In a test file, it looks like this:
    import { test, expect } from '@playwright/test';
    
    test('Launch Edge Test', async ({ browserName }) => {
      // The configuration in playwright.config.ts handles the channel,
      // but you can also launch a browser instance manually:
    });
  5. Edit the Playwright Config: Open playwright.config.ts (or .js). Locate the projects array. To target Edge specifically, add or modify a project entry as follows:
    projects: [
      {
        name: 'Microsoft Edge',
        use: { 
          browserName: 'chromium', 
          channel: 'msedge' 
        },
      },
    ],
    Note that browserName remains 'chromium' because Edge is built on the Chromium engine, but the channel tells Playwright to use the Edge executable.
  6. Run the Script: Execute your test using the terminal. To run only the Edge project, use the command npx playwright test --project="Microsoft Edge".
  7. Verify Execution: If you are running in headless mode (default), the test will run in the background. To see the browser actually open and perform actions, run npx playwright test --project="Microsoft Edge" --headed.

Common Mistakes

  • Omitting the Channel: A common error is setting browserName: 'msedge'. This will fail because msedge is not a browser engine; it is a channel of the chromium engine. Always use browserName: 'chromium' and channel: 'msedge'.
  • Running Headless by Default: Beginners often think the script isn't working because no window appears. Playwright runs "headless" by default. Use the --headed flag to visualize the process.
  • Version Mismatch: Using a very old version of Playwright with a brand-new version of Edge can occasionally cause driver mismatches. Keep your dependencies updated.
  • Incorrect Project Naming: When running via the CLI, the project name must match the string defined in the config file exactly, including capitalization.

Tips

  • Use the Codegen Tool: If you aren't sure how to write the selectors for Edge, use npx playwright codegen --channel=msedge. This opens a browser window and records your actions, generating the code automatically.
  • Utilize Trace Viewer: For debugging failed Edge scripts, use npx playwright show-trace path/to/trace.zip to see a frame-by-frame recording of the execution.
  • Environment Variables: If you need to run tests across different Edge versions (like Edge Canary), you can configure the channel to 'msedge-canary'.
  • Slow Mo: When running in headed mode, add slowMo: 50 to your launch options to slow down the actions so you can follow along visually.

Troubleshooting

  • Browser Not Found: If Playwright reports that it cannot find the Edge executable, ensure Microsoft Edge is installed in the default system location. If it is installed in a custom path, you may need to specify the executablePath in the launch options.
  • Timeout Errors: If the script fails with a timeout, the page may be loading slower in Edge than in Chromium. Increase the timeout in playwright.config.ts using the timeout: 30000 (30 seconds) setting.
  • Permission Denied: On some systems, the terminal may not have permission to launch a browser. Try running the terminal as an Administrator or checking your OS security settings.
  • Selector Failures: If a script works in Chrome but fails in Edge, check for Edge-specific extensions or settings (like tracking prevention) that might be altering the DOM or blocking requests.

Frequently Asked Questions

Does Playwright need a separate driver for Edge?

No, Playwright uses the Chromium driver to communicate with Edge. You only need to specify the msedge channel to point it to the Edge installation.

Can I run Edge scripts in headless mode?

Yes, by default, Playwright runs in headless mode, meaning the browser operates in the background without a visible user interface.

Is there a difference between Chromium and Edge in Playwright?

Chromium is the open-source engine, while Edge is the branded browser by Microsoft. While they are very similar, testing in Edge ensures your app works with Microsoft's specific browser implementation and defaults.

How do I run a script in Edge Canary?

You can change the channel setting from 'msedge' to 'msedge-canary' in your configuration file, provided you have the Canary version installed.

Can I use Playwright with Edge on macOS and Linux?

Yes, Playwright supports Microsoft Edge across Windows, macOS, and Linux, as long as the browser is installed on the host machine.