Skip to content

Commit fa106de

Browse files
authored
Merge pull request #920 from cisagov/CRASM-2288_Document_Playwright_README
Add Playwright Test README for documentation
2 parents 2295ed6 + 72c9a7a commit fa106de

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

playwright/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# **Playwright Testing for Crossfeed**
2+
3+
## **Overview**
4+
5+
This project uses **Playwright** for automated end-to-end testing. The Playwright testing workflow operates in three distinct modes:
6+
7+
1. **Local Testing via Terminal** - Running tests directly from the terminal.
8+
2. **Local Testing via Docker** - Running tests within a Docker container locally.
9+
3. **AWS Regression Workflow** - Running tests on AWS ECS and uploading results to an S3 bucket.
10+
11+
This README will guide you through setting up, configuring, and running Playwright tests in each mode, as well as handling the deployment process for AWS.
12+
13+
---
14+
15+
## **Table of Contents**
16+
17+
- [Overview](#overview)
18+
- [Prerequisites](#prerequisites)
19+
- [Playwright Configuration for Crossfeed](#playwright-configuration-for-crossfeed)
20+
- [Local Testing via Terminal](#local-testing-via-terminal)
21+
- [Environment Variables for Local Testing](#environment-variables-for-local-testing)
22+
- [Environment Variables for Local Testing in VS Code](#environment-variables-for-local-testing-in-vs-code)
23+
- [Local Testing via Docker](#local-testing-via-docker)
24+
- [Logging into Crossfeed and Preserving Browser State](#logging-into-crossfeed-and-preserving-browser-state)
25+
- [Adding Test Cases](#adding-test-cases)
26+
- [Test Results](#test-results)
27+
28+
---
29+
30+
## **Prerequisites**
31+
32+
## Playwright configuration for Crossfeed
33+
34+
Since Playwright is intended to run in 3 different modes `[localhost, local Docker, Github Actions/AWS]`, a configuration tool at `utils/env.ts` is created to help set default URLs and headless mode options.
35+
36+
The `PW_XFD_URL` environment variable is to be set by the build processes. If no variable has been set, by default it will try to test against `http://localhost` . The pertinent environment variables are located in `xfd/.env` and are prefixed with `PW_*`
37+
38+
## **Local Testing via Terminal**
39+
40+
This mode is intended for frontend developers to write their own feature test cases during development. To run Playwright tests locally from the terminal:
41+
42+
1. Install Playwright and its dependencies by following the official [Playwright installation guide](https://playwright.dev/docs/intro#installing-playwright).
43+
44+
2. Run Playwright Tests from the `xfd/playwright` folder
45+
46+
```bash
47+
npx playwright test
48+
```
49+
50+
Tests are defined in the `e2e/global-admin` folder and denoted by a `.spec.ts` file extension.
51+
52+
3. Test Results are written to `playwright-report/results.json` for JSON data, and `playwright-report/html` for HTML reports.
53+
54+
### **Environment Variables for Local Testing**
55+
56+
For local testing, the following variables need to be loaded into your environment.
57+
58+
```env
59+
PW_XFD_USERNAME
60+
PW_XFD_PASSWORD
61+
PW_XFD_2FA_ISSUER
62+
PW_XFD_2FA_SECRET
63+
PW_XFD_USER_ROLE
64+
PW_XFD_LOGIN
65+
```
66+
67+
### **Environment Variables for Local Testing in VS Code**
68+
69+
If you are using testing in VS Code using the Playwright extension, add the following lines you your `settings.json`, when is accessed from `Extension Settings->Edit in settings.json`
70+
71+
```json
72+
"playwright.env": {
73+
"PW_XFD_URL":"",
74+
"PW_XFD_USERNAME":"",
75+
"PW_XFD_PASSWORD":"",
76+
"PW_XFD_2FA_ISSUER":"",
77+
"PW_XFD_2FA_SECRET":"",
78+
"PW_XFD_USER_ROLE":"",
79+
"PW_XFD_LOGIN" : ""
80+
}
81+
```
82+
83+
## **Local Testing via Docker**
84+
85+
This mode is intended to kick off as part of the build process. A docker container will be created using the same Playwright test.
86+
87+
A wait feature will listen for the frontend to begin accepting requests, as the frontend will take longer to compile than Playwright to be ready for testing. When a code 200 is received from the frontend, the Playwright tests will begin.
88+
89+
## **Logging into Crossfeed and Preserving Browser State**
90+
91+
The global setup script located at `xfd/playwright/global-setup.ts` performs the task of logging into Crossfeed and storing the browsers state to `xfd/playwright/storageState.json`. This script works by manually performing the steps to login to Crossfeed through the browser.
92+
93+
This process does not use the PIV card certificate process, but a username/password process with 2FA tokens. The necessary environment variables are not stored in code, but populated by the build process (manually setting environment variables, set by docker-compose, or populated by Github Actions).
94+
95+
The login process also uses `waitForFrontend()` to listen for a response code 200 from Crossfeed's frontend before performing the login procedure.
96+
97+
The `OTPAuth` module is used to generate the 2FA token needed for login, using a 2FA secret string that is not released publically.
98+
99+
If the global setup script fails to login, manually check the login process by logging in with the service account username/password and 2FA combo. If the Okta login process is slightly different than from what the script is anticipating, it will fail. Sometimes logging in can resolve some extra menus or checkboxes that may occassionally pop up. Unfortunately since we don't own the process, our ability to login in an automated manner is somewhat fragile.
100+
101+
## **Adding Test Cases**
102+
103+
Test cases are added by adding `*.spec.ts` files under the `xfd/playwright/e2e` folder.
104+
105+
Tests are defined with a `.beforeEach()` and `.afterEach()` method which create a new browser instance for each individual test case. Each test case is its own discrete task, and the success or failure state of one case should not affect the status of other cases (unless otherwise intended).
106+
107+
```typescript
108+
test.describe('home', () => {
109+
test.beforeEach(async ({ browser }) => {
110+
const context = await browser.newContext();
111+
page = await context.newPage();
112+
await page.goto('/');
113+
});
114+
115+
test.afterEach(async () => {
116+
await page.close();
117+
});
118+
```
119+
120+
## **Test Results**
121+
122+
Test results are written out to `xfd/playwright/playwright-report`. The most recent run is written out to `html/index.html` for the HTML version, and `results.json` for the latest JSON test report data.

0 commit comments

Comments
 (0)