Skip to content

Commit

Permalink
strata test env e2e test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
deetz99 committed Jan 13, 2025
1 parent 605eede commit d422dc6
Show file tree
Hide file tree
Showing 15 changed files with 611 additions and 386 deletions.
2 changes: 1 addition & 1 deletion strr-platform-web/tests/e2e/platform-smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ loginMethods.forEach((loginMethod) => {
const businessDetails = getFakeBusinessDetails()
const platformDetails = getFakePlatformDetails()

test.only('Complete Application Flow', async ({ page }) => {
test('Complete Application Flow', async ({ page }) => {
// choose account
await chooseAccount(page, loginMethod)

Expand Down
3 changes: 2 additions & 1 deletion strr-strata-web/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ PLAYWRIGHT_TEST_BCSC_PASSWORD=""
PLAYWRIGHT_TEST_BCSC_PREMIUM_ACCOUNT_NAME=""
PLAYWRIGHT_TEST_BCEID_USERNAME=""
PLAYWRIGHT_TEST_BCEID_PASSWORD=""
PLAYWRIGHT_TEST_BCEID_PREMIUM_ACCOUNT_NAME=""
PLAYWRIGHT_TEST_BCEID_PREMIUM_ACCOUNT_NAME=""
PLAYWRIGHT_TEST_BCEID_OTP_SECRET=""
69 changes: 69 additions & 0 deletions strr-strata-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,72 @@ Startup the development environment.
```bash
pnpm run dev
```

## E2E Testing

To run Playwright in the terminal:
```bash
pnpm test:e2e
```

To run Playwright in ui mode:
```bash
pnpm test:e2e:ui
```

### Playwright Config

The globalSetup option will create and save an auth user state which can then be used inside other tests:
```js
globalSetup: './tests/e2e/test-utils/global-setup',
```

To run tests in headless mode, set the headless property in the Playwright config:
```js
use: {
headless: true
}
```

### Environment Variables Setup

Before running any e2e tests, ensure the `.env` file has the correct values. Missing or incorrect values will lead to test failures. Below are the required environment variables:

```
# Configures the login steps, anything other than 'Development' will try to login using the prod steps.
NUXT_ENVIRONMENT_HEADER="Development"
# playwright login, account name and BCEID secret
PLAYWRIGHT_TEST_BCSC_USERNAME=""
PLAYWRIGHT_TEST_BCSC_PASSWORD=""
PLAYWRIGHT_TEST_BCSC_PREMIUM_ACCOUNT_NAME=""
PLAYWRIGHT_TEST_BCEID_USERNAME=""
PLAYWRIGHT_TEST_BCEID_PASSWORD=""
PLAYWRIGHT_TEST_BCEID_PREMIUM_ACCOUNT_NAME=""
PLAYWRIGHT_TEST_BCEID_OTP_SECRET=""
# The full url the tests will run against (local/dev/test/sandbox)
NUXT_BASE_URL=""
```

### Authentication

The test runner creates a saved authentication state (storageState) stored in JSON files located at tests/e2e/.auth/. This state is used by the current smoke tests (as of 2025/01/10) to maintain the authenticated session without re-entering credentials or OTPs repeatedly.

To use this auth state in other tests:
```js
test.describe('Describe Block', () => {
test.use({ storageState: `tests/e2e/.auth/${loginMethod.toLowerCase()}-user.json` })

test('My Test', async ({ page }) => {
// stuff
})
})
```

### Playwright Quirks

- Tests using BCEID login that rely on a OTP (test/prod environments) should use the saved auth user state. Generating a new OTP for each test on login can cause the tests to fail.
- A maximum of 4 workers seems to be the sweet spot for tests to pass without colliding with each other.
- Setting to 1 worker will fully disable running tests in parallel.
- Using the Playwright extension/testing tab does not execute the global setup and save the auth state. You must run `pnpm:e2e` to at least create the auth files before running tests with the extension.
3 changes: 2 additions & 1 deletion strr-strata-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"playwright-core": "^1.49.1",
"sass": "^1.77.6",
"typescript": "^5.5.3",
"vitest": "^1.6.0"
"vitest": "^1.6.0",
"otpauth": "^9.3.6"
},
"dependencies": {
"@daxiom/nuxt-core-layer-test": "^0.0.20",
Expand Down
19 changes: 12 additions & 7 deletions strr-strata-web/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { fileURLToPath } from 'node:url'
import { defineConfig, devices } from '@playwright/test'
import type { ConfigOptions } from '@nuxt/test-utils/playwright'
import { config as dotenvConfig } from 'dotenv'
// load default env
dotenvConfig()

const devicesToTest = [
'Desktop Chrome'
Expand All @@ -15,27 +18,29 @@ const devicesToTest = [
] satisfies Array<string | typeof devices[string]>

export default defineConfig<ConfigOptions>({
// globalSetup: './tests/e2e/test-utils/auth-setup', // setup when booting test runner
globalSetup: './tests/e2e/test-utils/global-setup', // setup when booting test runner
testDir: './tests/e2e',
testIgnore: ['./tests/e2e/test-utils/**'],
reporter: 'line',
// Fail the build on CI if you accidentally left test.only in the source code.
forbidOnly: !!process.env.CI,
// Retry on CI only.
// retries: process.env.CI ? 2 : 0,
retries: 3, // a11y tests are flaky
maxFailures: 1,
// Opt out of parallel tests on CI.
// workers: process.env.CI ? 1 : undefined,
// maxFailures: 1,
// workers: process.env.CI ? 1 : undefined, // Opt out of parallel tests on CI.
// setting workers to 1 disables running tests in parallel
workers: 4, // 4 seems to be the sweet spot
use: {
nuxt: {
rootDir: fileURLToPath(new URL('.', import.meta.url))
},
actionTimeout: 0,
baseURL: 'http://localhost:3000',
actionTimeout: 2000,
baseURL: process.env.NUXT_BASE_URL,
trace: 'on-first-retry',
screenshot: 'off',
// do not open browser
headless: false
headless: true
},
projects: devicesToTest.map(p => typeof p === 'string' ? ({ name: p, use: devices[p] }) : p)
// webServer: {
Expand Down
14 changes: 14 additions & 0 deletions strr-strata-web/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions strr-strata-web/tests/e2e/enums/login-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum LoginSource {
BCSC = 'BCSC',
BCEID = 'BCEID'
}
Loading

0 comments on commit d422dc6

Please sign in to comment.