← Back to Angular, page 9

angular-0177 · Medium mid

How do Cypress and Playwright compare for end-to-end testing of an Angular app, and what makes an e2e test reliable?

Both run real browser tests and wait automatically for elements. Playwright drives Chromium, Firefox and WebKit from outside the page, runs tests in parallel, supports several tabs and users, and has a trace viewer. Cypress runs inside the browser with a strong interactive runner. Protractor was removed, so new projects pick one of these. Reliable tests use stable selectors, no fixed sleeps and controlled data.

Full answer

End-to-end (e2e) tests open the real app in a real browser and act like a user: type, click, check the screen. They cover the full path including routing, the template, the API and the backend (or a mocked backend).

Protractor, the old Angular e2e tool, was deprecated and removed (Angular 15 dropped it from the CLI), so teams choose Cypress or Playwright. ng e2e has no built-in default runner: you add one with a schematic or by installing the package.

Playwright:

  • Controls browsers from outside through a protocol. Chromium, Firefox and WebKit are supported, so you can test Safari-like behaviour.
  • Tests run in parallel by default, in isolated browser contexts (fast, clean state).
  • Several tabs, several users and file downloads are easy. Auto-waiting for actionability and web-first assertions (expect(locator).toHaveText(...) retries).
  • Trace viewer, screenshots, video, codegen to record tests.
  • Written in TypeScript, with its own test runner.

Cypress:

  • Runs the test code inside the browser next to the app, so it has direct access to the page and network stubbing (cy.intercept).
  • A very good interactive runner with time travel snapshots, which many people find friendly for beginners.
  • Commands are queued and retried automatically. It is single-tab by design, and cross-origin flows need cy.origin.
  • Component testing mode exists for Angular components.

Choose by needs: cross-browser and parallel speed favours Playwright. Developer experience in a single app and a big plugin world favours Cypress. Many teams now start with Playwright.

Making e2e reliable:

  1. Select by role or test id (getByRole('button', { name: 'Save' })), not by CSS classes or position.
  2. Never use fixed sleeps. Rely on auto-waiting and assertions that retry.
  3. Control data: seed the backend, or mock the network for unrelated services.
  4. Keep tests independent, and log in through an API or stored state, not through the screen each time.
  5. Test a few critical journeys, because e2e is slow. Leave details to unit and component tests.
  6. Run in CI with retries only as a safety net, and record traces for failures.
typescript
import { expect, test } from '@playwright/test';

test('student can log in and see the report card', async ({ page }) => {
  await page.goto('http://localhost:4200/login');

  await page.getByLabel('Email').fill('asha@example.com');
  await page.getByLabel('Password').fill('correct-horse');
  await page.getByRole('button', { name: 'Log in' }).click();

  // Web-first assertions retry until the text appears. No fixed sleeps.
  await expect(page.getByRole('heading', { name: 'Report card' })).toBeVisible();
  await expect(page.getByTestId('total-marks')).toHaveText('412');
});

`@playwright/test` is installed for type checking only. The spec compiles, but it was not run, because no browser download or running app was available here.

Key points

  • Protractor is gone. Playwright and Cypress are the usual choices, with no default in `ng e2e`.
  • Playwright: multi-browser, parallel, multiple tabs, traces. Cypress: in-browser runner, strong interactive tooling.
  • Use role or test id selectors, auto-waiting, controlled data and no fixed sleeps.
  • Keep e2e for critical journeys.

Common mistakes

  • Using `sleep(2000)` instead of waiting for a condition.
  • Selecting by CSS class names that change.
  • Logging in through the UI in every test.

Follow-ups

  • How do you test a flow that opens a second tab?
  • How would you speed up an e2e suite in CI?
  • What belongs in e2e and what in unit tests?

Tips

  • Pick a side and give a reason, but show you know the other.
  • Say what you do against flaky tests.
  • Mention the test pyramid.

Evaluation rubric

  • Compares both tools accurately.
  • Knows the status of Protractor and `ng e2e`.
  • Lists reliability practices.