We will take one slow suite and fix it from beginning to end. The suite belongs to an imaginary HR portal. It has 200 browser tests: apply for leave, download payslip, update bank details, approve a request as a manager and so on. Each test starts the same way. Open the login page, type the username, type the password, wait for the OTP-free test account to be accepted, and land on the dashboard.

Step 1: put a number on the waste

Time the login alone. Say it takes 6 seconds through the browser: page load, typing, server check, redirect. Across 200 tests that is 200 × 6 = 1,200 seconds, which is 20 minutes of pure login in a serial run. With four parallel workers, each worker still logs in fifty times, so about 5 minutes of the wall-clock time goes on logging in. And it is not just time. The login page is now exercised 200 times as a side effect, so when the login page is slow or has a bug, every one of the 200 tests fails for a reason that has nothing to do with what it is meant to test.

Keep one dedicated test for the login screen itself, with its wrong-password, locked-account and empty-field cases. All the other tests do not need to prove login works; they need to be logged in.

Step 2: log in without the browser

The portal’s login is, underneath, a call to an authentication endpoint that returns either a session cookie or a token. Instead of driving the form, a setup step calls that endpoint directly with the test account’s credentials. That takes perhaps half a second, not six. What happens next depends on how the app remembers you:

  • If it uses a session cookie, the browser context needs that cookie set before the first page loads.
  • If it keeps a token in the browser’s local storage, the context needs that entry populated.
  • Some apps use both, plus a CSRF token, so inspect what the real login leaves behind in the browser’s storage tools before guessing.

Step 3: save the state, load it per test

Modern tools package exactly this idea. Playwright can save a browser context’s cookies and local storage to a file (its storageState), and a project-level setup step can create that file once before the tests start; each test or worker then begins with a context created from the file, already logged in. Cypress offers cy.session for caching a login between tests. Selenium has no built-in equivalent, but you can do the same by adding saved cookies to the driver before the first navigation. The mechanics differ; the shape is identical: authenticate once, store, reuse.

The saved file contains live credentials in effect, so treat it as a secret. Generate it into a folder that is ignored by version control, never commit it, and read the test account’s password from your CI secret store rather than from the repository.

Step 4: more than one user

The HR portal has employees, managers and HR admins. Create one saved state per role, named for the role, and let each test say which role it needs. The manager-approval test can then use an employee state to raise a leave request through the API, and the manager state to approve it in the browser, without any interface login at all. Keep role accounts separate from real people’s accounts and make sure each has only the permissions its role should have, otherwise you are testing as a superuser and will miss permission bugs.

Step 5: expiry, the thing that bites at 3 p.m.

Tokens and sessions expire. Suppose the access token lives 30 minutes and the suite takes 45. Tests from minute 31 start failing with authorisation errors, or the app redirects them to the login page, which looks exactly like a product bug. Three defences, in increasing effort:

  1. Create the state fresh at the start of every run and make the suite shorter than the lifetime, where that is realistic.
  2. Check the expiry time stored in the token or the cookie before each test or worker, and refresh when it is close.
  3. If the system supports refresh tokens, use that flow in the helper, the way a real client would.

When a test fails with an authorisation error, make the failure message say so plainly, so nobody wastes an hour on it.

Step 6: shared session, shared state

Reusing a login means many tests act as the same user, at once if they run in parallel. That creates collisions the repeated login used to hide. Two tests both change the same user’s phone number. A test that logs the user out, which destroys the shared session on the server side, breaks all tests that follow. A test that applies for leave leaves the leave balance reduced for the next one. The fixes: have tests that modify data create their own records or use their own users, keep logout and session-expiry tests in a separate group that creates its own session, and clean up what you create. If a few tests need complete isolation, give them a dedicated account instead of the shared one.

The result

After the change, the suite performs three API logins in setup (employee, manager, admin) at roughly half a second each, so about 1.5 seconds in place of 1,200 seconds of repeated form logins, and one dedicated browser test continues to check the login screen. Time saved is about 20 minutes of serial execution, or about 5 minutes per worker with four workers. More important, a login outage no longer turns 200 tests red.

One honest limit: if the app’s login is genuinely risky, say it relies on a third-party single sign-on that changes often, keep a periodic full-journey test that goes through the real interface, so the shortcut does not blind you to a broken front door.

Figures and names in the examples are illustrative.

Leave a Reply

Trending