Prompt
How do I automate the critical user journeys in my web app without writing a ton of brittle code?
Latest observation
A good way to automate critical user journeys without writing lots of brittle UI code is to favor tests at the right layer and abstract the repetitive parts.
Here’s a practical approach:
1) Test the journey, not every click
Focus on the few flows that matter most:
- sign up / login
- checkout / purchase
- create / edit / delete core objects
- password reset
- subscription upgrade / cancellation
Keep these as high-value end-to-end tests, and don’t try to cover every edge case through the browser.
2) Use a stable test strategy pyramid
Use:
- Unit tests for business logic
- API/integration tests for core workflows
- A small number of E2E browser tests for the critical paths
This reduces reliance on flaky UI automation.
3) Prefer selectors that won’t break often
Avoid selectors based on layout or text that changes often.
Use:
data-testid- semantic roles/labels when possible
- stable IDs or attributes
Example:
<button data-testid="checkout-submit">Submit order</button>
4) Build reusable helpers/page objects
Don’t repeat login, navigation, or form-filling logic in every test.
Example:
async function login(page, email, password) {
await page.goto('/login');
await page.getByLabel('Email').fill(email);
await page.getByLabel('Password').fill(password);
await page.getByRole('button', { name: 'Log in' }).click();
}
Then your test stays focused:
test('user can complete checkout', async ({ page }) => {
await login(page, 'user@example.com', 'secret');
await page.goto('/cart');
await page.getByTestId('checkout-submit').click();
await expect(page.getByText('Order confirmed')).toBeVisible();
});
5) Set up state directly when possible
Instead of clicking through every step to create test data:
- seed the database
- use API calls to create users/orders/etc.
- log in by setting auth cookies or tokens
This makes tests faster and less brittle.
6) Keep assertions meaningful and few
Assert on outcomes:
- confirmation message
- record exists in the database
- API response status
- email sent
- payment status updated
Avoid lots of tiny UI assertions that are sensitive to copy or styling changes.
7) Make tests deterministic
Reduce flakiness by controlling:
- time
- random data
- network calls
- feature flags
- background jobs
Use mocks/stubs for third-party services like email or payments where appropriate.
8) Run critical flows in CI
Run a small suite:
- on every PR: smoke tests
- nightly: broader critical journey suite
- before release: full regression on key paths
9) Consider Playwright if you’re starting fresh
For browser automation, Playwright is a strong default because it has:
- auto-waiting
- good selectors
- parallel runs
- cross-browser support
- easier setup than many older tools
10) Keep the suite small and curated
If a journey is not business-critical, don’t automate it in the browser first. Add more coverage via API tests or unit tests instead.
A simple rule of thumb
If you want to avoid brittle code:
- Use the UI sparingly
- Use APIs/fixtures for setup
- Use stable selectors
- Wrap repeated steps in helpers
- Only automate the most important user journeys end-to-end
If you want, I can also give you:
- a recommended test architecture for a modern web app, or
- a Playwright example showing how to structure these tests cleanly.
Brands