This repository was archived by the owner on Jan 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
386 lines (336 loc) · 16.3 KB
/
playwright.yml
File metadata and controls
386 lines (336 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
name: Playwright Tests + Prettier (reviewdog) + test-flow chart
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
#---------------------------------------------------
# 0 – Checkout
#---------------------------------------------------
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }} # Crucial for pushing back to PR branch
#---------------------------------------------------
# 1 – reviewdog CLI
#---------------------------------------------------
- name: Setup reviewdog
uses: reviewdog/action-setup@v1
with: { reviewdog_version: latest }
#---------------------------------------------------
# 2 – Prettier → inline review comments
#---------------------------------------------------
- name: Prettier style check (reviewdog)
shell: bash
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
npx prettier --write '**/*.{js,ts,tsx,jsx,json,yml,yaml,md}'
git diff -U0 --no-color > prettier.patch || true
if [ -s prettier.patch ]; then
cat prettier.patch | reviewdog -f=diff \
-name="prettier" \
-reporter=github-pr-review \
-filter-mode=diff_context \
-level=warning
else
echo "No Prettier issues found."
fi
#---------------------------------------------------
# 3 – Node & deps
#---------------------------------------------------
- name: Set up Node.js
uses: actions/setup-node@v4
with: { node-version: 18 }
- name: Install dependencies
run: npm install
# --- IMPORTANT FIX: Install Playwright browsers early ---
- name: Install Playwright and browsers
run: npx playwright install --with-deps
## NEW STEP: Create a directory for "before" screenshots (this will be the Playwright output dir)
- name: Create "before" screenshots directory
run: mkdir -p published-screenshots-before
## NEW STEP: Save current PR's test files (before checking out main)
- name: Save PR test files
run: |
mkdir -p tests_pr_backup/
cp -r tests/* tests_pr_backup/ # Copy contents, not the directory itself
ls -la tests_pr_backup/ # Debugging: verify backup content
## NEW STEP: Checkout Main branch for "before" tests
- name: Checkout Main branch for "before" screenshots
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git fetch origin main # Ensure main branch is fetched
git reset --hard origin/main # Reset to main, discard local changes (tests_pr_backup is safe outside)
## NEW STEP: Run Playwright tests on Main (for "before" screenshots)
- name: Run Playwright "before" tests
run: |
# Playwright will output its results (including screenshots) directly into PLAYWRIGHT_SCREENSHOT_DIR.
# Ensure your playwright.config.js uses process.env.PLAYWRIGHT_SCREENSHOT_DIR for 'output' in 'use'.
PLAYWRIGHT_SCREENSHOT_DIR="published-screenshots-before" npx playwright test
## NEW STEP: Restore PR test files
- name: Restore PR test files
run: |
git reset --hard ${{ github.event.pull_request.head.ref }} # Reset to PR branch's state
# Now, replace the 'tests/' directory with the backup
rm -rf tests/ # Remove current tests/ directory
mv tests_pr_backup/ tests/ # Move the backup directory to become the new tests/
ls -la tests/ # Debugging: verify restored content
#---------------------------------------------------
# 5 – Run Playwright tests (This now runs on the PR branch's tests, generates to 'published-screenshots/')
#---------------------------------------------------
- name: Run Playwright tests
run: npx playwright test --output=playwright-report-pr/ --reporter=html,json # This will create results.json
# Playwright will output screenshots into the default location defined in playwright.config.js (e.g., test-results/)
# We will then move them to 'published-screenshots/' in the next step.
# --- Debugging: Verify Playwright Report Existence AFTER tests ---
- name: Verify Playwright Report Existence
run: |
echo "Listing contents of the workspace:"
ls -la
echo "Listing contents of playwright-report-pr/:"
ls -la playwright-report-pr/
# Check if the results.json exists
if [ -f "playwright-report-pr/results.json" ]; then # FIXED: Check for results.json
echo "playwright-report-pr/results.json exists."
else
echo "ERROR: playwright-report-pr/results.json DOES NOT EXIST."
exit 1
fi
# --- End Debugging ---
# --- START OF SCREENSHOT PUBLISHING FOR BOTH BEFORE AND AFTER ---
# Prepare directories for PR-specific screenshots on GitHub Pages
- name: Prepare PR Screenshots for GitHub Pages (After)
if: github.event_name == 'pull_request'
run: |
PR_DIR="docs/pr/${{ github.event.pull_request.number }}/screenshots"
mkdir -p "$PR_DIR"
# Find all .png files within the default Playwright output directory (e.g., test-results/)
# and copy them to the target PR_DIR.
# This assumes your playwright.config.js sets `output` to 'test-results' for the main run.
find test-results/ -name "*.png" -exec cp {} "$PR_DIR/" \; || true # The '|| true' prevents failure if no files are found
echo "Copied 'after' screenshots to $PR_DIR/"
ls -lh "$PR_DIR/" # For debugging
- name: Prepare PR Screenshots for GitHub Pages (Before)
if: github.event_name == 'pull_request'
run: |
PR_BEFORE_DIR="docs/pr/${{ github.event.pull_request.number }}/before-screenshots"
mkdir -p "$PR_BEFORE_DIR"
# Find all .png files within the 'published-screenshots-before/' directory (Playwright's output for before tests)
# and copy them to the target PR_BEFORE_DIR.
find published-screenshots-before/ -name "*.png" -exec cp {} "$PR_BEFORE_DIR/" \; || true
echo "Copied 'before' screenshots to $PR_BEFORE_DIR/"
ls -lh "$PR_BEFORE_DIR/" # For debugging
# Commit and Push PR Screenshots to GitHub Pages
- name: Commit and Push PR Screenshots to GitHub Pages
if: github.event_name == 'pull_request'
uses: EndBug/add-and-commit@v9
with:
add: 'docs/pr/${{ github.event.pull_request.number }}/screenshots/ docs/pr/${{ github.event.pull_request.number }}/before-screenshots/'
message: 'Docs: Publish Playwright screenshots for PR #${{ github.event.pull_request.number }} [skip ci]'
default_author: github_actions
push: true
# Generate Markdown for screenshots with their public URLs
- name: Generate Screenshot Markdown for PR Comment
id: generate_screenshot_markdown
if: github.event_name == 'pull_request'
run: |
REPO_OWNER=$(echo "${{ github.repository }}" | cut -d'/' -f1)
REPO_NAME=$(echo "${{ github.repository }}" | cut -d'/' -f2)
PR_NUMBER="${{ github.event.pull_request.number }}"
PR_SCREENSHOTS_LOCAL_DIR_AFTER="docs/pr/${PR_NUMBER}/screenshots"
PR_SCREENSHOTS_LOCAL_DIR_BEFORE="docs/pr/${PR_NUMBER}/before-screenshots"
GITHUB_PAGES_BASE_URL_AFTER="https://digitalproductinnovationanddevelopment.github.io/Code-Reviews-of-GUI-Tests/pr/${PR_NUMBER}/screenshots/"
GITHUB_PAGES_BASE_URL_BEFORE="https://digitalproductinnovationanddevelopment.github.io/Code-Reviews-of-GUI-Tests/pr/${PR_NUMBER}/before-screenshots/"
IMAGE_MARKDOWN=""
# Add "Before" screenshots section
IMAGE_MARKDOWN+="### Before (Main Branch)\n\n"
if [ -d "$PR_SCREENSHOTS_LOCAL_DIR_BEFORE" ]; then
shopt -s nullglob
# Look for .png files directly in the directory
declare -a IMG_FILES_BEFORE=("$PR_SCREENSHOTS_LOCAL_DIR_BEFORE"/*.png)
shopt -u nullglob
if [ ${#IMG_FILES_BEFORE[@]} -gt 0 ]; then
for img_path in "${IMG_FILES_BEFORE[@]}"; do
img_name=$(basename "$img_path")
img_url="${GITHUB_PAGES_BASE_URL_BEFORE}${img_name}"
IMAGE_MARKDOWN+="\n\n"
done
else
IMAGE_MARKDOWN+="*No 'before' screenshots found.*\n\n"
fi
else
IMAGE_MARKDOWN+="*'Before' screenshots directory not found.*\n\n"
fi
# Add "After" screenshots section
IMAGE_MARKDOWN+="### After (PR Branch)\n\n"
if [ -d "$PR_SCREENSHOTS_LOCAL_DIR_AFTER" ]; then
shopt -s nullglob
# Look for .png files directly in the directory
declare -a IMG_FILES_AFTER=("$PR_SCREENSHOTS_LOCAL_DIR_AFTER"/*.png)
shopt -u nullglob
if [ ${#IMG_FILES_AFTER[@]} -gt 0 ]; then
for img_path in "${IMG_FILES_AFTER[@]}"; do
img_name=$(basename "$img_path")
img_url="${GITHUB_PAGES_BASE_URL_AFTER}${img_name}"
IMAGE_MARKDOWN+="\n\n"
done
else
IMAGE_MARKDOWN+="*No 'after' screenshots found.*\n\n"
fi
else
IMAGE_MARKDOWN+="*'After' screenshots directory not found.*\n\n"
fi
echo "pr_screenshots_markdown<<EOF" >> $GITHUB_OUTPUT
echo "$IMAGE_MARKDOWN" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
shell: bash
# --- END OF SCREENSHOT PUBLISHING FOR BOTH BEFORE AND AFTER ---
#---------------------------------------------------
# 6 – Upload HTML report
#---------------------------------------------------
- name: Upload HTML report
if: always()
uses: actions/upload-artifact@v4
with:
name: playwright-report
path: playwright-report-pr/
#---------------------------------------------------
# 7 – Extract test summary
#---------------------------------------------------
- name: Extract test summary
id: summary
run: |
# Playwright's JSON report contains the summary statistics
REPORT_FILE=playwright-report-pr/results.json # FIXED: Path to results.json
if [ ! -f "$REPORT_FILE" ]; then
echo "Error: Playwright report file not found at $REPORT_FILE"
exit 1
fi
TOTAL=$(jq '.stats.total' "$REPORT_FILE")
PASSED=$(jq '.stats.expected' "$REPORT_FILE")
FAILED=$(jq '.stats.failures' "$REPORT_FILE")
SKIPPED=$(jq '.stats.skipped' "$REPORT_FILE")
DURATION=$(jq '.stats.duration' "$REPORT_FILE") # Playwright duration is already in ms
PASS_RATE=$(awk "BEGIN{printf \"%.2f\", ($TOTAL==0)?0:($PASSED/$TOTAL)*100}")
echo "total=$TOTAL" >> $GITHUB_OUTPUT
echo "passed=$PASSED" >> $GITHUB_OUTPUT
echo "failed=$FAILED" >> $GITHUB_OUTPUT
echo "skipped=$SKIPPED" >> $GITHUB_OUTPUT
echo "duration=$DURATION" >> $GITHUB_OUTPUT
echo "passrate=$PASS_RATE" >> $GITHUB_OUTPUT
#---------------------------------------------------
# 8 – ESLint (tests only)
#---------------------------------------------------
- name: Run ESLint on GUI tests
shell: bash
run: |
# Run ESLint and redirect output to a file. The `|| true` prevents the step from failing if lint issues are found.
npx eslint "tests/**/*.{js,ts,tsx}" -f stylish > eslint-tests.txt || true
- name: Upload ESLint report
if: always()
uses: actions/upload-artifact@v4
with:
name: eslint-test-report
path: eslint-tests.txt
- name: Read ESLint report (preview)
id: lint_summary
run: |
# Only read if the file exists, to prevent errors
if [ -f "eslint-tests.txt" ]; then
echo 'summary<<EOF' >> $GITHUB_OUTPUT
head -n 20 eslint-tests.txt >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
else
echo 'summary<<EOF' >> $GITHUB_OUTPUT
echo 'ESLint report not generated or found.' >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
fi
#---------------------------------------------------
# 9 – Generate Suite→Spec Mermaid chart (flowchart.png)
#---------------------------------------------------
- name: Generate test-flow chart
shell: bash
run: |
set -e
REPORT_FILE=playwright-report-pr/results.json # FIXED: Path to results.json
if [ ! -f "$REPORT_FILE" ]; then
echo "Error: Playwright report file not found at $REPORT_FILE for flowchart generation. Skipping chart."
exit 0 # Exit successfully if report not found, but log message
fi
echo "graph TD" > flowchart.mmd
jq -r '
.suites[] as $file |
($file.title // "NO_FILE_TITLE") as $fileTitle |
$file.suites[]? as $suite |
($suite.title // "NO_SUITE_TITLE") as $suiteTitle |
$suite.specs[]? as $spec |
($spec.title // "NO_SPEC_TITLE") as $specTitle |
[$fileTitle, $suiteTitle, $specTitle] | @tsv
' "$REPORT_FILE" |
while IFS=$'\t' read -r fileTitle suiteTitle specTitle; do
# Build unique, safe IDs by combining parent and child
fileId=$(echo "$fileTitle" | tr -c 'A-Za-z0-9' '_' | sed 's/^_*\|_*$//g')
suiteId=$(echo "${fileTitle}_${suiteTitle}" | tr -c 'A-Za-z0-9' '_' | sed 's/^_*\|_*$//g')
specId=$(echo "${fileTitle}_${suiteTitle}_${specTitle}" | tr -c 'A-Za-Z0-9' '_' | sed 's/^_*\|_*$//g')
# File node
if ! grep -q "^ ${fileId}\[" flowchart.mmd; then
echo " ${fileId}[\"${fileTitle}\"]" >> flowchart.mmd
fi
# Suite node
if ! grep -q "^ ${suiteId}\[" flowchart.mmd; then
echo " ${suiteId}[\"${suiteTitle}\"]" >> flowchart.mmd
echo " ${fileId} --> ${suiteId}" >> flowchart.mmd
fi
# Spec node/edge
echo " ${suiteId} --> ${specId}[\"${specTitle}\"]" >> flowchart.mmd
done
printf '{ "args": ["--no-sandbox","--disable-setuid-sandbox"] }\n' > puppeteer.json
npx -y @mermaid-js/mermaid-cli@10.6.1 \
-p puppeteer.json \
-i flowchart.mmd \
-o flowchart.png
ls -lh flowchart.png
- name: Show flowchart.mmd for debugging
run: cat flowchart.mmd
- name: Upload test-flow chart
if: always() # Removed fileExists: This ensures the step runs and actions/upload-artifact will warn if file is missing.
uses: actions/upload-artifact@v4
with:
name: test-flow-chart
path: flowchart.png
#---------------------------------------------------
# 10 – Sticky PR comment
#---------------------------------------------------
- name: Comment on PR with results
uses: marocchino/sticky-pull-request-comment@v2
with:
message: |
## Playwright Test Metrics
*Total:* **${{ steps.summary.outputs.total }}**
*Passed:* **${{ steps.summary.outputs.passed }}**
*Failed:* **${{ steps.summary.outputs.failed }}**
*Skipped:* **${{ steps.summary.outputs.skipped }}**
Duration: **${{ steps.summary.outputs.duration }} ms**
Pass Rate: **${{ steps.summary.outputs.passrate }} %**
## ESLint (GUI tests)
```
${{ steps.lint_summary.outputs.summary }}
```
## Test-Flow Chart
# Note: The flowchart.png is uploaded as an artifact. To embed it directly like screenshots,
# you would also need to copy it into the 'published-screenshots' directory in step 9,
# and then reference it using the PR-specific GitHub Pages URL.
Artifact: **test-flow-chart → flowchart.png**
## Playwright Screenshots for this PR
${{ steps.generate_screenshot_markdown.outputs.pr_screenshots_markdown }}
_Full run details:_ [link](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})