Configuration, environments and secrets
Configuration is kept as a separate layer so one framework runs on any environment without code changes. A single module reads the environment variables and validates them at startup, so a missing variable fails before the first test and is named. Environments are data rather than branches in code, secrets come from the CI secret store, and GitHub Actions does not pass them to pull request runs from forks.
System design · Framework
The test is green locally and fails in CI: BASE_URL is empty. Where should configuration live?
A CI run starts, passes three hundred tests and fails with Invalid URL because one environment variable is not set. On the local machine it was in the developer's shell. One framework must run locally, on a PR environment and on staging without code changes, so configuration is a separate layer. This lesson covers how to keep it, where secrets live and how to share a core between teams.
How it works and why. The environment changes more often than scenarios, and a mistake in it is expensive: it shows up late and looks like a test failure. Three principles follow. First: all environment variables are read in one place and validated at startup. Second: secrets never reach the repository. Third: execution settings (timeouts, retries, workers) live in the Playwright config, not scattered over tests.
Where the environment is read
process.env in tests
Dozens of files read the variable. BASE_URL forgotten: a failure at the 300th test with a confusing error.
One point with validation
One module reads and checks everything at startup. A variable is missing: the run does not start and the error names it.
In practice this is a schema (for example with the zod library): a module describes the required variables and fails on import if something is missing. Since the Playwright config imports this module, the error occurs before the first test.
import { z } from 'zod';
const schema = z.object({
BASE_URL: z.url(),
API_URL: z.url(),
TEST_USER_PASSWORD: z.string().min(1),
});
// throws at startup, listing the missing variables
export const env = schema.parse(process.env);
The config uses the typed env rather than process.env:
import { defineConfig, devices } from '@playwright/test';
import { env } from './config/env';
export default defineConfig({
fullyParallel: true,
retries: process.env.CI ? 1 : 0,
workers: process.env.CI ? 8 : undefined,
reporter: process.env.CI ? 'blob' : 'html',
use: { baseURL: env.BASE_URL, trace: 'on-first-retry' },
projects: [
{ name: 'setup', testMatch: /.*\.setup\.ts/ },
{
name: 'chromium',
use: { ...devices['Desktop Chrome'], storageState: 'playwright/.auth/user.json' },
dependencies: ['setup'],
},
],
});
A setup project with a dependency is the standard way to log in once and save a storageState that all tests of the project then use. Projects separate suites (api, chromium, mobile), and they can be run separately with --project.
Environments as data. Addresses, flags and test account logins per environment are data, not code. They are set with environment variables or a separate file per environment (an .env.example template in the repository, the real .env files in .gitignore). The framework code has no branches like «if staging, then…», because each such branch is one more way to break a run.
Secrets. API keys and test user passwords come from the CI secret store or a vault and are not in the repository. Three nuances that set a thoughtful answer apart: GitHub Actions does not pass secrets to pull request runs from forks, so tests that need them get an empty value and must be skipped rather than left to fail (the env schema then makes such a secret optional, or the run stops at startup); values in the log are masked only when they match a known secret verbatim (a transformed or split secret ends up in the log), so they are not printed at all; where the provider supports it, short-lived credentials (OIDC) are better than a long key in a variable. Test accounts have minimal rights and are not the accounts of real people.
Several teams. When five teams write tests, the shared core (clients, fixtures, config, utilities) is worth separating, while tests stay next to the teams' services. Then the core develops centrally and the teams do not block each other.
A shared core for several teams
A versioned package
Teams pull a version, the core develops centrally. The price: core and tests drift apart in versions, releases are needed.
One repo with workspaces
A core change and the tests in one PR, no versions. The price: a shared CI and discipline of code owners.
Limits and alternatives. Validation does not know whether a variable is right: BASE_URL may point to production and the schema will not catch it. For dangerous actions an explicit guard helps: the framework refuses to write data if the URL matches a list of forbidden ones. For a small project a separate module with zod is overkill: a function that throws on a missing variable is enough. Per-environment config files are handy while there are a few; when there are dozens, one template with overrides through variables is better.
Common mistakes.
- Reading
process.envin tests and helpers: a missing variable shows up mid-run and with the wrong error. - Defaults that point to staging or, worse, production: a forgotten
BASE_URLdoes not fail but quietly runs the tests against someone else's environment. - Committing
.envwith real passwords: even after deletion they stay in the history. - Scattering timeouts over tests: the run cannot be sped up or slowed down with one change.
- Sharing a core between teams with no owner and no versions: one team's change breaks everyone.
Check yourself
Why is it better to catch a missing BASE_URL by validation on config import than at the moment of first use?
Answer
Validation fails before the first test and names the variable, while an error at the moment of use appears mid-run (say, at the 300th test) and looks like a test failure. Diagnosis takes far longer.
A CI test from a fork did not get a secret and failed on login. Is it a framework bug?
Answer
No. GitHub Actions does not pass secrets to pull request runs from forks. Tests that need a secret must not start there, or must be marked as skipped, not fail. The solution: a separate suite without secrets for forks, the full suite after review in a trusted branch.