Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 37 ยท Testing and Performance

End-to-End Testing for Ionic Apps

Test whole user flows in an Ionic app with Playwright or Cypress in the browser, use stable selectors and page objects, and know when Appium or device tests are needed.

Unit tests prove that parts work. End-to-end (E2E) tests prove that the parts work together: a script opens the app in a real browser, taps buttons, types text and checks what appears. For an Ionic app the web build runs in any browser, so E2E tests are cheap to run in continuous integration. They cannot test a real camera or push notification, so the plan is: test the flows in the browser with fakes for native features, and check native features by hand or with device tests.

What E2E tests are good for

Choose a few critical journeys, the ones that would hurt most if broken:

  1. Sign in and see the home page.
  2. Create, edit and delete an item.
  3. The offline banner appears and the outbox empties when connectivity returns.
  4. Checkout or the single most valuable action in your app.

E2E tests are slower and more fragile than unit tests, so ten good ones beat a hundred flaky ones.

Playwright example

Playwright runs Chromium, Firefox and WebKit (Safari's engine), which matters because iOS web views use WebKit. Real test, read-only:

TypeScript
import { test, expect } from '@playwright/test';

test.use({ viewport: { width: 390, height: 844 } });   // phone size

test('adds a note', async ({ page }) => {
  await page.goto('http://localhost:4200/notes');

  await page.getByLabel('Title').fill('Pack tent');
  await page.getByRole('button', { name: 'Add' }).click();

  await expect(page.getByText('Pack tent')).toBeVisible();
  await expect(page.getByTestId('open-count')).toHaveText('1');
});

Cypress is similar (cy.get('[data-testid=open-count]').should('have.text', '1')). Both auto-wait: they retry assertions until the element appears or a timeout passes, so you do not sprinkle sleep(2000) calls through the tests.

Ionic specific details

  • Ionic Web Components render inside shadow DOM (parts of them). Playwright pierces shadow DOM by default; in Cypress set includeShadowDom: true.
  • ion-input wraps a native <input>. Targeting by label (getByLabel) or a data-testid on the ion-input and then using .locator('input') is more stable than CSS classes.
  • Page transitions are animated. Tests wait for the new page to be visible, and you can disable animations in test builds with IonicModule.forRoot({ animated: false }) or setupIonicReact({ animated: false }).

Stable selectors

Tests break when selectors depend on layout. In order of preference:

  1. Role and accessible name (getByRole('button', { name: 'Add' })), which also checks accessibility.
  2. Label or visible text.
  3. A dedicated data-testid, for elements without good text.
  4. Never: nth-child, generated class names, deep CSS paths.

Page objects

When many tests use the same screen, wrap the screen in a class: tests then read like a story, and a markup change is fixed in one place.

JS A page object over a fake app

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Waiting the right way

E2E flakiness almost always comes from timing. A button may exist before its data has loaded, or a toast may disappear before you check it. Tools retry assertions, so you should write the condition you wait for, not a duration.

JS Polling instead of sleeping

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Faking native features

In the browser build, native plugins use web fallbacks or are missing. For tests, provide fakes before the page loads. In Playwright you can inject them with page.addInitScript (for example setting navigator.geolocation through the geolocation context option, or replacing the Camera call with a prepared image). Another approach is a test build flag that swaps your own PhotoService for a fake one. It is cleaner because the rest of the app is still the real code.

Device tests

Tool Runs Use for
Playwright / Cypress browsers flows, forms, layout at phone sizes
Appium or WebdriverIO real devices and emulators native dialogs, permissions, camera sheet
Maestro, Detox devices scripted mobile flows (Detox targets React Native, so less suited)
Manual checklist any release smoke test on one iPhone and one Android phone

Appium drives the app through the native accessibility layer and can switch into the web view context. It is slower and needs more setup, so reserve it for the few things the browser cannot do. A short manual checklist before each release is often the best value.

Run E2E in continuous integration on every pull request, against a production-like build, with test data created by the test itself (or reset before each run). Tests that depend on leftover data fail mysteriously.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should Ionic E2E tests also run in WebKit?

    Rendering differences between engines can show up in WebKit first.
  2. 2. Which selector is the most stable?

    Roles and accessible names reflect what the user sees and survive markup changes.
  3. 3. What replaces a fixed sleep in a good E2E test?

    Auto-waiting assertions continue as soon as the condition is true and fail with a clear timeout.
  4. 4. Which feature needs a device or emulator test?

    Browsers cannot show native permission sheets, so these need Appium or manual checks.

Summary

  • E2E tests cover a few critical journeys in a real browser at phone size.
  • Prefer role, label and data-testid selectors; avoid layout-based ones.
  • Page objects keep tests readable and maintainable.
  • Wait for conditions, never for fixed times; disable animations in test builds.
  • Use Appium or manual checks for what only a device can do.

Next: Performance and Lazy Loading