Back to Test Automation
2026-04-056 min read

Migrate from Playwright (Test Automation)

Learn Migrate from Playwright (Test Automation) step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on migrating your test automation suite from Playwright to Cypress using JavaScript! This tutorial will help you understand why this transition is crucial, prepare you with the necessary prerequisites, delve into the core concepts, provide a worked example, highlight common mistakes, offer practice questions, and answer frequently asked questions. Let's get started!

The Importance of Test Automation Migration

As your project grows, maintaining test automation becomes increasingly important to ensure software quality and catch regressions early. Both Playwright and Cypress are powerful tools for test automation, but each has its unique strengths. By migrating from Playwright to Cypress, you can use the advantages of both frameworks, such as Playwright's cross-browser testing capabilities with Cypress's faster execution times, real-time reloading, and better user experience (UX) testing capabilities. This transition will help you streamline your test automation process and improve overall project efficiency.

Prerequisites

To follow this tutorial, you should have a basic understanding of:

  1. JavaScript programming language
  2. Node.js (v14 or later) installed on your system
  3. Familiarity with Playwright test automation framework
  • Understand how to launch browsers, navigate pages, and perform actions using Playwright's APIs.
  1. Basic understanding of Cypress test automation framework
  • Know the difference between Cypress's command-based approach and Playwright's API-based approach.
  • Be familiar with Cypress's unique features like real-time reloading, faster execution times, and better UX testing capabilities.

Core Concept

Setting Up a New Cypress Project

To start a new Cypress project, you can use the npx create-cypress command:

npx create-cypress

Follow the prompts to configure your project. Once complete, navigate to the newly created project folder and run npm install to install dependencies.

Migrating Playwright Tests to Cypress (expanded)

To migrate from Playwright to Cypress, you'll need to rewrite your tests using Cypress commands and APIs. Here's a simple example of how to convert a Playwright test to Cypress:

Playwright Test (playwright.spec.js)

const { chromium } = require('playwright');

describe('My First Test', async () => {
let browser;
let page;

beforeAll(async () => {
browser = await chromium.launch();
page = await browser.newPage();
});

afterAll(async () => {
await browser.close();
});

test('Visit the Google homepage', async () => {
await page.goto('https://www.google.com');
const title = await page.title();
expect(title).toEqual('Google');
});
});

Cypress Test (cypress/integration/example.spec.js)

describe('My First Test', () => {
it('Visits the Google homepage', () => {
cy.visit('https://www.google.com');
cy.title().should('eq', 'Google');
});
});

In this example, we replaced Playwright's chromium.launch(), browser.newPage(), and page.goto() with Cypress's cy.visit(). Note that Cypress tests are written in a more declarative style, which can make them easier to read and maintain.

Migrating More Complex Tests

When migrating more complex tests involving multiple pages, forms, or network requests, you may encounter additional challenges. It's essential to understand how each framework handles these scenarios differently and adapt your tests accordingly. For example:

  • Handling multiple tabs: In Playwright, you can open new tabs using page.newTab() and switch between them using page.switchTo(). In Cypress, you can use the cy.open() function to open a new tab and navigate between them using cy.visit().
  • Handling forms: Both Playwright and Cypress provide APIs for filling out forms. However, Cypress offers a more user-friendly approach with its fill() function, which can be used to populate form fields based on their labels or names.
  • Mocking network requests: While both frameworks allow you to mock network requests, the implementation differs. Playwright uses page.route(), while Cypress offers a more powerful API with cy.intercept().

Worked Example

For a complete worked example of migrating a Playwright test suite to Cypress, you can refer to the Cypress Migration Guide. This guide provides detailed instructions and examples for converting your tests from Playwright to Cypress, including handling more complex scenarios like multiple tabs and forms.

Common Mistakes

  1. Assuming the same syntax and API between Playwright and Cypress: While there are similarities, you'll need to learn Cypress's unique APIs and commands.
  2. Ignoring test isolation in Cypress: Unlike Playwright, Cypress runs each test in an isolated environment by default. This means you should avoid using global variables or shared state between tests.
  3. Not setting up custom command aliases: In larger projects, it's helpful to create custom command aliases for reusable functions.
  4. Not handling network requests correctly: Cypress provides a powerful API for mocking and stubbing network requests. Make sure you understand how to use it effectively.
  5. Ignoring Cypress-specific best practices: Familiarize yourself with Cypress's best practices, such as writing tests in the integration folder, using the beforeEach(), afterEach(), and before() hooks for setup and teardown tasks, and more.

Common Mistakes (subheadings)

  • Not understanding the difference between Playwright's API and Cypress's command-based approach
  • Ignoring test isolation best practices in Cypress
  • Not setting up custom command aliases for reusable functions
  • Not handling network requests effectively in Cypress
  • Ignoring Cypress-specific best practices

Practice Questions

  1. How can you create a custom command alias in Cypress?
  2. What is the difference between Playwright's page.goto() and Cypress's cy.visit() functions?
  3. Why is it important to handle network requests differently in Cypress compared to Playwright?
  4. How can you ensure test isolation in a Cypress project?
  5. What are some best practices for writing tests in a Cypress project?
  6. How do you open new tabs and switch between them in Cypress compared to Playwright?
  7. How do you fill out forms in Cypress compared to Playwright?
  8. How do you mock network requests in Cypress compared to Playwright?
  9. What are the main differences between Playwright's API-based approach and Cypress's command-based approach?
  10. How can you maintain test isolation when working with multiple tabs or forms in a Cypress project?

FAQ

  1. Can I use the same test suite with both Playwright and Cypress?
  • No, each framework has its own syntax and APIs, so you'll need to maintain separate suites for each.
  1. What are the benefits of migrating from Playwright to Cypress?
  • Benefits include faster execution times, real-time reloading, better UX testing capabilities, and a more declarative testing style.
  1. How do I handle network requests in Cypress?
  • You can use Cypress's intercept() function to stub or mock network requests.
  1. What is the best way to structure my tests in a Cypress project?
  • Store your tests in the integration folder and use hooks like beforeEach(), afterEach(), and before() for setup and teardown tasks.
  1. How can I create custom command aliases in Cypress?
  • You can define custom command aliases by creating a file in the cypress/support folder and exporting functions with the desired alias names.
  1. What are the main differences between Playwright's API-based approach and Cypress's command-based approach?
  • Playwright uses an API-based approach, where you interact with browser objects directly using JavaScript methods. Cypress takes a more declarative approach, where you write commands that describe the actions to be performed on the page.
  1. How can I maintain test isolation when working with multiple tabs or forms in a Cypress project?
  • To ensure test isolation, you should use separate describe blocks for each tab or form and avoid sharing state between them. Additionally, consider using the beforeEach(), afterEach(), and before() hooks to set up and tear down data specific to each test.
  1. How do I open new tabs and switch between them in Cypress compared to Playwright?
  • In Cypress, you can use the cy.open() function to open a new tab and navigate between them using cy.visit(). In Playwright, you can open new tabs using page.newTab() and switch between them using page.switchTo().
Migrate from Playwright (Test Automation) | Test Automation | XQA Learn