|
1 | 1 | # Playwright Python Testing Framework |
2 | 2 |
|
3 | | -A modular, pytest-based Playwright framework for testing React applications (or any web apps) in Python. |
4 | | -This framework includes support for: |
| 3 | +A modular, pytest-based **Playwright (Python)** framework for web UI testing. |
5 | 4 |
|
6 | | -* **Page Object Model** (POM) for clean, maintainable test code |
7 | | -* **Data-driven testing** via JSON fixtures |
8 | | -* **Visual regression** with baseline snapshots and automatic diffs |
9 | | -* **Screenshots & videos** on failures for easier debugging |
10 | | -* **Environment configuration** using `.env` files |
11 | | -* **Authenticated session reuse** using Playwright's `storageState` |
12 | | -* **HTML & JUnit reporting** via `pytest-html` and JUnit XML |
13 | | -* **Optional Allure reporting** for advanced dashboards |
14 | | -* **CLI & CI integration** with GitHub Actions and GitHub Pages |
| 5 | +This repo is set up so that a new user can: |
| 6 | +1) **Sign up once** on the demo page, |
| 7 | +2) Automatically **save credentials to `.env`** and the **session to `auth/storage_state.json`**, and |
| 8 | +3) **Reuse the logged-in session** on every test run — no login code inside tests. |
15 | 9 |
|
16 | 10 | --- |
17 | | -x |
18 | | -## 🚀 Getting Started |
19 | 11 |
|
20 | | -Follow these steps to set up and verify that the framework is working correctly on your machine. |
| 12 | +## ✅ What’s Included |
21 | 13 |
|
22 | | -### ✅ Prerequisites |
| 14 | +- **Page Object Model (POM)** for clean, maintainable tests |
| 15 | +- **Signup-first flow** that persists credentials + session |
| 16 | +- **Global fixtures** via `conftest.py` (one place to manage browser/context/page) |
| 17 | +- **Sample tests** to verify setup |
| 18 | +- **HTML/JUnit reports** (via `pytest-html`, JUnit XML) |
| 19 | +- **CI-friendly** configuration (pytest, reports) |
23 | 20 |
|
24 | | -- Python 3.8 or later installed |
25 | | -- Git installed |
26 | | -- Google Chrome or Chromium installed |
27 | | -- Node.js (optional, for Git hooks with Husky) |
| 21 | +> Tested against **pytest 8+** and **Playwright 1.45+**. |
28 | 22 |
|
29 | 23 | --- |
30 | 24 |
|
31 | | -### 🪜 Step-by-Step Setup |
32 | | - |
33 | | -#### 1. **Clone the Repository** |
34 | | -This copies the framework into your machine. |
| 25 | +## 🧭 First-Time Setup — Step by Step (with explanations) |
35 | 26 |
|
| 27 | +### 0) Clone the repository |
36 | 28 | ```bash |
37 | 29 | git clone https://github.com/your-username/playwright_python_framework.git |
38 | 30 | cd playwright_python_framework |
39 | 31 | ``` |
| 32 | +**Why?** You need the project files locally. |
40 | 33 |
|
41 | | -#### 2. **Create a Virtual Environment** |
42 | | -Keeps your project dependencies isolated from your system Python. |
| 34 | +--- |
43 | 35 |
|
| 36 | +### 1) Create & activate a virtual environment |
44 | 37 | ```bash |
45 | 38 | python -m venv .venv |
46 | | -source .venv/bin/activate # macOS/Linux |
47 | | -# .\.venv\Scripts\activate # Windows |
| 39 | +# macOS/Linux: |
| 40 | +source .venv/bin/activate |
| 41 | +# Windows (PowerShell): |
| 42 | +# .venv\Scripts\Activate.ps1 |
48 | 43 | ``` |
| 44 | +**Why?** Keeps your project’s Python packages isolated from system Python. |
49 | 45 |
|
50 | | -#### 3. **Install Dependencies** |
51 | | -Installs all required Python libraries and Playwright browsers. |
| 46 | +--- |
52 | 47 |
|
| 48 | +### 2) Install Python deps and Playwright browsers |
53 | 49 | ```bash |
54 | 50 | pip install -r requirements.txt |
55 | 51 | playwright install |
56 | 52 | ``` |
| 53 | +**Why?** Installs pytest, Playwright, and helpers. `playwright install` downloads the browser binaries Playwright drives. |
57 | 54 |
|
58 | | -#### 4. **Bootstrap Signup** |
59 | | -Before running any tests, you must create a test account. |
60 | | -This script will **sign up on the demo app** and save your credentials into a `.env` file automatically. |
| 55 | +--- |
61 | 56 |
|
| 57 | +### 3) Bootstrap the first user (signup → writes `.env` + saves session) |
62 | 58 | ```bash |
63 | | -python scripts/bootstrap_signup.py --name "Test User" --email testuser@example.com --password "MyPass123!" |
| 59 | +python scripts/bootstrap_signup.py --name "Jane Doe" --email "jane@example.com" --password "StrongPass123" |
64 | 60 | ``` |
| 61 | +> macOS tip: If your password contains `!` or `$`, wrap it in **single quotes**: `'StrongPass!23'`. |
| 62 | +
|
| 63 | +**What this does:** |
| 64 | +- Opens the demo **Sign Up** page: `https://faruk-hasan.com/automation/signup.html` |
| 65 | +- Fills **username** (`#username`), **email** (`#email`), **password** (`#password`), **confirm password** (`#confirmPassword`) |
| 66 | +- Clicks the **Sign Up** button (role: button, name: “Sign Up”) |
| 67 | +- Writes your credentials to **`.env`**: |
| 68 | + ```env |
| 69 | + SIGNUP_NAME=Jane Doe |
| 70 | + SIGNUP_EMAIL=jane@example.com |
| 71 | + SIGNUP_PASSWORD=StrongPass123 |
| 72 | + STORAGE_STATE=auth/storage_state.json |
| 73 | + ``` |
| 74 | +- Saves the logged-in session to **`auth/storage_state.json`** |
| 75 | + |
| 76 | +**Why?** From now on, tests start **already authenticated**. No login steps inside tests. |
| 77 | + |
| 78 | +--- |
65 | 79 |
|
66 | | -What happens here: |
67 | | -- Navigates to the signup page. |
68 | | -- Fills in your provided name, email, and password. |
69 | | -- Submits the form. |
70 | | -- Saves your credentials in `.env` so tests can use them. |
| 80 | +### 4) Confirm the bootstrap worked |
| 81 | +Ensure these now exist: |
| 82 | +- `.env` (contains `SIGNUP_NAME`, `SIGNUP_EMAIL`, `SIGNUP_PASSWORD`, `STORAGE_STATE`) |
| 83 | +- `auth/storage_state.json` (a non-empty JSON file) |
71 | 84 |
|
72 | | -#### 5. **Run the Sample Test** |
73 | | -Run the included test to confirm everything is set up correctly. |
| 85 | +**Why?** `conftest.py` uses these to build an authenticated Playwright context automatically. |
74 | 86 |
|
| 87 | +--- |
| 88 | + |
| 89 | +### 5) Run the sample test (quick health check) |
75 | 90 | ```bash |
76 | 91 | pytest tests/test_sample.py -vv |
77 | 92 | ``` |
| 93 | +**What this does:** Opens the signup page and asserts the title and required form elements exist. |
78 | 94 |
|
79 | | -Expected result: |
80 | | -- Test should **pass** ✅ if the framework is installed and configured properly. |
| 95 | +**Success looks like:** |
| 96 | +``` |
| 97 | +collected 1 item |
| 98 | +tests/test_sample.py::test_framework_setup PASSED [100%] |
| 99 | +``` |
81 | 100 |
|
82 | | -#### 6. **View Reports** |
83 | | -After running tests, open the HTML report: |
| 101 | +--- |
84 | 102 |
|
| 103 | +### 6) Run the full test suite |
85 | 104 | ```bash |
86 | | -open reports/html/report.html # macOS |
87 | | -start reports\html\report.html # Windows |
| 105 | +pytest -vv |
88 | 106 | ``` |
| 107 | +**Why?** Verifies the project runs end-to-end under pytest. |
| 108 | +If you kept the bootstrap test, see the next section to run it only on demand. |
89 | 109 |
|
90 | 110 | --- |
91 | 111 |
|
92 | | -## 🔐 Environment Variables |
| 112 | +## 🔁 Running the one-time signup test only when needed (recommended) |
93 | 113 |
|
94 | | -The `.env` file is created automatically during signup, but you can also edit it manually. |
| 114 | +If you keep `tests/test_signup_and_save_session.py`, mark it and skip by default so signup only runs when you ask for it: |
95 | 115 |
|
96 | | -```env |
97 | | -BASE_URL=https://faruk-hasan.com/automation/signup.html |
98 | | -LOGIN_EMAIL=testuser@example.com |
99 | | -LOGIN_PASSWORD=MyPass123! |
| 116 | +**`pytest.ini`** |
| 117 | +```ini |
| 118 | +[pytest] |
| 119 | +markers = |
| 120 | + sample: quick setup verification test |
| 121 | + bootstrap: one-time signup & session save |
| 122 | +addopts = -m "not bootstrap" |
100 | 123 | ``` |
101 | 124 |
|
102 | | -These values are used by tests to log in without signing up each time. |
| 125 | +- Daily runs: |
| 126 | + ```bash |
| 127 | + pytest |
| 128 | + ``` |
| 129 | + (Bootstrap test is excluded.) |
| 130 | +- When you actually need to re-bootstrap: |
| 131 | + ```bash |
| 132 | + pytest -m bootstrap -vv |
| 133 | + ``` |
103 | 134 |
|
104 | 135 | --- |
105 | 136 |
|
106 | | -## 🧪 Writing Your Own Tests |
| 137 | +## 🔒 Headless vs. Headed (seeing the browser) |
107 | 138 |
|
108 | | -- Add new tests inside the `tests/` folder. |
109 | | -- Use the `page` fixture provided by Playwright to interact with the browser. |
110 | | -- Example: |
| 139 | +The browser mode is controlled in `conftest.py` within the `browser` fixture. |
| 140 | +To **watch the browser**, set `headless=False` there: |
111 | 141 |
|
112 | 142 | ```python |
113 | | -def test_example(page): |
114 | | - page.goto("https://example.com") |
115 | | - assert page.title() == "Example Domain" |
| 143 | +browser = p.chromium.launch(headless=False) |
| 144 | +``` |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +## 🔐 Rotating credentials / re-signing up |
| 149 | + |
| 150 | +If login fails or the session is stale, quickly create a new user + session: |
| 151 | + |
| 152 | +```bash |
| 153 | +python scripts/bootstrap_signup.py --force --random-email --name "New User" --password 'NewStrongPass!23' |
| 154 | +``` |
| 155 | + |
| 156 | +**What `--force` does:** deletes old `auth/storage_state.json` and overwrites creds in `.env`. |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +## 📊 Test Reports (HTML) |
| 161 | + |
| 162 | +### One-time setup |
| 163 | +```bash |
| 164 | +pip install pytest-html pytest-metadata |
| 165 | +``` |
| 166 | + |
| 167 | +### Generate a report (one-off) |
| 168 | +```bash |
| 169 | +pytest -vv --html=reports/html/report.html --self-contained-html |
| 170 | +``` |
| 171 | + |
| 172 | +Open the report in your browser: |
| 173 | +```bash |
| 174 | +open reports/html/report.html # macOS |
| 175 | +start reports\html\report.html # Windows |
| 176 | +``` |
| 177 | + |
| 178 | +### Always generate reports automatically |
| 179 | +Add this to your `pytest.ini`: |
| 180 | +```ini |
| 181 | +[pytest] |
| 182 | +addopts = --html=reports/html/report.html --self-contained-html |
| 183 | +``` |
| 184 | + |
| 185 | +--- |
| 186 | + |
| 187 | +## 🧩 Project Structure |
| 188 | + |
| 189 | +``` |
| 190 | +playwright_python_framework/ |
| 191 | +├─ auth/ |
| 192 | +│ └─ storage_state.json |
| 193 | +├─ pages/ |
| 194 | +│ └─ signup_page.py |
| 195 | +├─ scripts/ |
| 196 | +│ └─ bootstrap_signup.py |
| 197 | +├─ tests/ |
| 198 | +│ ├─ test_sample.py |
| 199 | +│ └─ test_signup_and_save_session.py |
| 200 | +├─ .env.example |
| 201 | +├─ conftest.py |
| 202 | +├─ pytest.ini |
| 203 | +├─ requirements.txt |
| 204 | +└─ README.md |
116 | 205 | ``` |
117 | 206 |
|
118 | 207 | --- |
119 | 208 |
|
120 | | -## 🤖 Common Commands |
| 209 | +## 🧾 .gitignore |
121 | 210 |
|
122 | | -| Action | Command | |
123 | | -|---------------------------|----------------------------------------| |
124 | | -| Run all tests | `pytest` | |
125 | | -| Run one test file | `pytest tests/test_sample.py` | |
126 | | -| Run with detailed output | `pytest -vv` | |
127 | | -| View HTML report | Open `reports/html/report.html` | |
| 211 | +``` |
| 212 | +.env |
| 213 | +auth/storage_state.json |
| 214 | +.pytest_cache/ |
| 215 | +__pycache__/ |
| 216 | +.venv/ |
| 217 | +reports/ |
| 218 | +``` |
128 | 219 |
|
129 | 220 | --- |
130 | 221 |
|
131 | | -## 🎉 You’re Ready! |
| 222 | +## 🔗 Handy Commands |
| 223 | + |
| 224 | +```bash |
| 225 | +pytest tests/test_sample.py -vv |
| 226 | +pytest -vv |
| 227 | +python scripts/bootstrap_signup.py --name "Jane Doe" --email "jane@example.com" --password "StrongPass123" |
| 228 | +python scripts/bootstrap_signup.py --force --random-email --name "New User" --password 'NewStrongPass!23' |
| 229 | +pytest -vv --html=reports/html/report.html --self-contained-html |
| 230 | +``` |
132 | 231 |
|
133 | | -Now you can: |
134 | | -- Run tests to validate your setup |
135 | | -- Extend the framework with new pages and tests |
136 | | -- Use `.env` to manage login credentials automatically |
| 232 | +--- |
137 | 233 |
|
138 | | -This project is **beginner-friendly** — just follow the steps above, and you’ll be up and running with a professional-grade Playwright testing framework. |
| 234 | +**You’re set!** |
0 commit comments