This test suite provides a robust foundation for writing, executing, and maintaining automated tests that validate trade-imports-animals application functionality from a user perspective, ensuring quality and reliability across the application lifecycle.
- Prerequisites
- Installation
- Editor Setup
- Running Tests
- Local Testing
- Visual Regression Tests
- Security Testing
- Running Tests on GitHub
- Running Tests via CDP Portal
- Developer Workflow
- Troubleshooting
- Resources
- Licence
- Node.js v24
- npm package manager
-
Clone the repository:
git clone <repository-url> cd trade-imports-animals-tests
-
Use the correct version of Node.js:
nvm use
-
Install dependencies:
npm install
-
Install Playwright browsers:
npx playwright install
Or install only Chromium (for faster setup):
npx playwright install chromium
To keep TypeScript checks and editor behaviour consistent with this repository and CI, use the workspace TypeScript version in your editor:
- Open any
.tsor.tsxfile. - Open Command Palette (
Cmd+Shift+Pon macOS). - Run
TypeScript: Select TypeScript Version. - Select
Use Workspace Version.
This project uses Playwright Test as the test runner, with TypeScript for type-safe test development.
| Command | Test scope | Target | Config | Generates Report |
|---|---|---|---|---|
npm test |
E2E suite, excluding @compose and @a11y |
CDP | playwright.config.ts |
✓ |
npm run test:a11y |
Accessibility (@a11y) test suite |
CDP | playwright.config.ts |
✓ |
npm run test:docker-compose |
E2E + E2E integration (@compose) test suites |
docker-compose stack | playwright.docker-compose.config.ts |
✓ |
npm run test:docker-compose:a11y |
Accessibility (@a11y) test suite |
docker-compose stack | playwright.docker-compose.config.ts |
✓ |
npm run test:docker-compose:security |
ZAP passive scan against the e2e suite | docker-compose stack | playwright.docker-compose.config.ts |
✓ |
npm run test:docker-compose:security:active |
Security (@active, ZAP passive + active scan) suite |
docker-compose stack | playwright.docker-compose.config.ts |
✓ |
npm run test:docker-compose:ci |
E2E, for the workspace CI stack job | docker-compose stack | playwright.docker-compose.config.ts |
✓ |
Optional: append these Playwright parameters to the command you're running (e.g. npm test) when needed.
| Playwright Parameters | Action |
|---|---|
-- --headed |
Run tests in headed mode (see the browser) |
-- tests/example.spec.ts |
Run a specific test file |
-- --grep "@smoke" |
Run tests with a specific tag |
-- --debug |
Run tests in debug mode |
-- --ui |
Run tests with UI mode |
-- --project=e2e |
Run tests in a specific project |
| Command | Report | Generates Report |
|---|---|---|
npx playwright show-report |
Open HTML report | n/a |
npm run report |
Generate Allure report | ✓ |
After tests run, Playwright results and report are generated automatically, and Allure results are also generated automatically. Run npm run report to generate the Allure report.
Shared settings (projects, reporters, retries: 1, trace: on-first-retry)
live in utils/playwright/shared-config.ts. The target-specific configs extend
those settings:
| File | Target |
|---|---|
playwright.config.ts |
CDP services |
playwright.docker-compose.config.ts |
docker-compose stack |
@a11y tests use the same configs; per-test timeout is longer in
fixtures/a11y.ts.
Each worker signs in once per project and its tests restore that session
instead of driving the identity provider every time (fixtures/auth-state.ts).
Saved state lives under playwright/.auth/ (gitignored, removed by _clean),
holds only the sid auth cookie, and is never written unless a fresh context
has proved it restores to a signed-in landing page. A spec that must start
unauthenticated opts out with test.use({ storageState: COLD_START }).
E2E_SESSION_REUSE=off is the kill switch: every test signs in for itself
again, so re-cap workers (e.g. -- --workers=4) to protect the auth stub.
Reuse is on by default against the docker-compose stack. On CDP it stays off
until ENVIRONMENT=<env> npm run probe:cdp-session-reuse has passed against
the target environment — the probe signs in once per service and proves
load-balanced replicas honour a session minted against another — after which a
lane opts in with E2E_SESSION_REUSE=on.
The docker-compose config targets localhost:3000 / localhost:3001, so
start the workspace stack first. CI runs npm run test:docker-compose:ci
against that stack via the workspace reusable workflow.
Both configs split tests across the same two Playwright projects:
| Project | Test scope |
|---|---|
e2e |
All tests excluding admin pages |
admin |
Admin pages only |
-
From the workspace root, start the locally built stack:
./scripts/stack/run-stack.sh -d
-
Run the E2E and admin projects with
npm run test:docker-compose.
npm run test:docker-compose targets the stack frontend on :3000 and the
admin service on :3001.
To debug, append Playwright flags, e.g.
npm run test:docker-compose -- --headed --workers=1.
The suite does not wipe the database before it runs, and does not need to. Every spec creates the state it asserts on through the front door (the backend API), scoped to that run, so the specs pass against a database that already holds the records of earlier runs.
For the security (OWASP ZAP) profiles against this stack, see Security Testing below.
| Command | Purpose |
|---|---|
./scripts/stack/run-stack.sh |
Start the full stack from published images |
./scripts/stack/run-stack.sh -d |
Start the stack built from local source under repos/ |
./scripts/stack/stop-stack.sh |
Stop the stack and wipe volumes |
./scripts/stack/bounce-backend.sh |
Recreate the backend container (picks up Java changes) |
See docker/stack/AGENTS.md in the workspace for the full flag reference.
To run tests against a CDP environment from your local machine:
- Set
PLAYWRIGHT_ENVIRONMENTto one ofdev,test, orperf-testin your.env. - Run tests with
npm test.
Use .env.example as a template.
When running via the CDP Portal, ENVIRONMENT is provided by the portal; use PLAYWRIGHT_ENVIRONMENT and avoid setting ENVIRONMENT locally.
Visual regression tests (tagged @visual) guard rendered composition — layout, spacing, colour, and typography as the user sees the page. They compare screenshots against committed baseline images and fail if any pixels differ outside the masked regions.
Baselines are stored alongside their spec files in *-snapshots/ directories and must be committed. Each platform requires its own baseline — update both when visual changes are intentional.
Regenerate the E2E baseline against the stack frontend with
npm run test:visual:update:macos for the host-rendered *-darwin.png image and
npm run test:visual:update:linux for the container-rendered *-linux.png image
used by CI. Both commands run the e2e project's @visual spec and write the
updated snapshot into the working tree for commit.
Two profiles run a DAST scan with OWASP ZAP as a proxy, driven by real Playwright journeys rather than a crawler:
security— passive scan across the e2e suite, cheap enough to run broadlysecurity:active— passive plus a scoped active scan against the@activesuite; docker-compose only, because that suite is destructive
See docs/security.md for how to run it, what is scanned and why, and how the run is gated.
E2E tests run in GitHub Actions via the workspace's reusable workflow, which starts the workspace stack with run-stack.sh --branch <branch> and runs this repo's published test image against it, with reports published to GitHub Pages.
The /.github/workflows/workspace-e2e-tests.yml workflow triggers after Publish Branch Image completes and calls DEFRA/trade-imports-workspace/.github/workflows/e2e-tests.yml@main with the branch name, then reports the result back to the PR.
.github/workflows/scheduled-security-scan.yml calls the workspace's security-active-scan.yml. Manual dispatch only for now — see docs/security.md.
Test Suite URL: https://portal.cdp-int.defra.cloud/test-suites/trade-imports-animals-tests (requires CCoE AWS OpenVPN).
In the CDP Portal, provide a PROFILE value to choose which test suite the container runs via entrypoint.sh.
If PROFILE is not set, the default profile is used.
| PROFILE | Test suite | NPM script |
|---|---|---|
default |
e2e test suite | npm test |
a11y |
accessibility test suite | npm run test:a11y |
security |
security test suite (ZAP passive scan) | npm run test:security |
security:active |
not supported on CDP — refused by entrypoint.sh; run it against the docker-compose stack |
— |
Tests are run from the CDP Portal under the Test Suites section. See the requirements below for how the portal run executes and publishes results.
- The CDP Portal run depends on the image being built/published by
/.github/workflows/publish.yml(from this repo'sDockerfile). - The container entrypoint (
entrypoint.sh) must exit0on success and a non-zero code on failure. - Reports are published to S3 by
npm run report:publish(which runs./bin/publish-tests.shand usesRESULTS_OUTPUT_S3_PATH).
This project uses ESLint and Prettier for code quality and formatting.
| Action | Command | Tool |
|---|---|---|
| Check for linting issues | npm run lint |
ESLint |
| Auto-fix linting | npm run lint:fix |
ESLint |
| Format code | npm run format |
Prettier |
| Check code formatting | npm run format:check |
Prettier |
| Type check TypeScript | npm run typecheck |
TypeScript |
Before committing changes:
- Run
npm run lint:fixto auto-fix linting issues - Run
npm run formatto format code - Run
npm run typecheckto check types (recommended)
This project uses Husky and lint-staged to automatically validate code quality before commits. The pre-commit hook checks linting (ESLint) and formatting (Prettier) on staged files only. If checks fail, the commit is blocked.
Run npx playwright install to install required browsers.
Ensure TypeScript is properly installed and tsconfig.json is configured correctly.
Increase timeout in playwright.config.ts or in individual tests using test.setTimeout().
Build with --platform=linux/amd64 due to the AWS CLI v2 dependency:
docker build --platform=linux/amd64 .THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT LICENCE found at:
http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3
The following attribution statement MUST be cited in your products and applications when using this information.
Contains public sector information licensed under the Open Government licence v3
The Open Government Licence (OGL) was developed by the Controller of Her Majesty's Stationery Office (HMSO) to enable information providers in the public sector to license the use and re-use of their information under a common open licence.
It is designed to encourage use and re-use of information freely and flexibly, with only a few conditions.