Skip to content

Commit

Permalink
Introduce playwright reporter
Browse files Browse the repository at this point in the history
This reporter does following:
- Publish html tests results to github pages
- Publish comment with summary and url link to test results
- When posting comments, previous comment is deleted

Also introducing github action to delete all tests results older than 7 days.
  • Loading branch information
marusak committed Feb 8, 2025
1 parent e1dd4e4 commit 2a4a9ec
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 36 deletions.
114 changes: 79 additions & 35 deletions .github/workflows/playwright-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ jobs:
.github/workflowScripts/waitForBackend.sh
- name: Run front-end Playwright tests
run: yarn playwright test
run: yarn playwright test > pw-output

- name: Store test summary
if: ${{ !cancelled() }}
run: tail -1 pw-output > playwright-report/summary

- name: Publish front-end Test Report
uses: ctrf-io/github-test-reporter@v1
Expand All @@ -203,39 +207,79 @@ jobs:
uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-ctrf-frontend
path: ./playwright-ctrf
name: playwright-report
path: playwright-report/
retention-days: 10
publish:
if: ${{ !cancelled() }}
needs: [playwright-tests]
runs-on: ubuntu-latest
outputs:
timestamp: ${{ steps.timestampid.outputs.timestamp }}
summary: ${{ steps.summaryid.outputs.summary }}

steps:
- name: Download report
uses: actions/download-artifact@v4
with:
name: playwright-report
path: downloaded-playwright-report

- name: Set timestamp
id: timestampid
run: echo "timestamp=$(date --utc +%Y%m%d_%H%M%SZ)" >> "$GITHUB_OUTPUT"

# - name: Create backend .env file
# working-directory: content-sources-backend/_playwright-tests
# run: |
# echo "BASE_URL=http://127.0.0.1:8000" >> .env
# echo "TOKEN=apple" >> .env
# echo "CI=true" >> .env
#
# - name: Install API playwright and dependencies
# working-directory: content-sources-backend/_playwright-tests
# run: yarn install
#
# - name: Install Playwright Browsers
# working-directory: content-sources-backend/_playwright-tests
# run: yarn playwright install --with-deps
#
# - name: Run API Playwright tests
# working-directory: content-sources-backend/_playwright-tests
# run: yarn playwright test
#
# - name: Publish API Test Report
# uses: ctrf-io/github-test-reporter@v1
# with:
# report-path: './content-sources-backend/_playwright-tests/playwright-ctrf/playwright-ctrf.json'
# if: ${{ !cancelled() }}
#
# - name: Store API Test report
# uses: actions/upload-artifact@v4
# if: ${{ !cancelled() }}
# with:
# name: playwright-ctrf-backend
# path: ./content-sources-backend/_playwright-tests/playwright-ctrf
# retention-days: 10
- name: Set summary
id: summaryid
run: echo "summary=$(cat downloaded-playwright-report/summary)" >> "$GITHUB_OUTPUT"

- name: Publish to github pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./downloaded-playwright-report
destination_dir: ${{ steps.timestampid.outputs.timestamp }}

- name: Write URL in summary
run: echo "### Test results - https://${{ github.repository_owner }}.github.io/pw-test/${{ steps.timestampid.outputs.timestamp }}/" >> $GITHUB_STEP_SUMMARY

- name: Comment PR
if: github.event_name == 'pull_request'
uses: thollander/actions-comment-pull-request@v3
with:
message: ${{steps.summaryid.outputs.summary}} - [results](https://${{ github.repository_owner }}.github.io/pw-test/${{ steps.timestampid.outputs.timestamp }})
comment-tag: pw-summary
mode: recreate
# Running API tests as part of `playwright-tests`
# - name: Create backend .env file
# working-directory: content-sources-backend/_playwright-tests
# run: |
# echo "BASE_URL=http://127.0.0.1:8000" >> .env
# echo "TOKEN=apple" >> .env
# echo "CI=true" >> .env
#
# - name: Install API playwright and dependencies
# working-directory: content-sources-backend/_playwright-tests
# run: yarn install
#
# - name: Install Playwright Browsers
# working-directory: content-sources-backend/_playwright-tests
# run: yarn playwright install --with-deps
#
# - name: Run API Playwright tests
# working-directory: content-sources-backend/_playwright-tests
# run: yarn playwright test
#
# - name: Publish API Test Report
# uses: ctrf-io/github-test-reporter@v1
# with:
# report-path: './content-sources-backend/_playwright-tests/playwright-ctrf/playwright-ctrf.json'
# if: ${{ !cancelled() }}
#
# - name: Store API Test report
# uses: actions/upload-artifact@v4
# if: ${{ !cancelled() }}
# with:
# name: playwright-ctrf-backend
# path: ./content-sources-backend/_playwright-tests/playwright-ctrf
# retention-days: 10
41 changes: 41 additions & 0 deletions .github/workflows/pw-cleanup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Delete old playwright reports

on:
schedule:
# Run every Monday 2:30 at night
- cron: "30 2 * * 1"
workflow_dispatch:

jobs:
delete_old_folders:
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: gh-pages
fetch-depth: 0

- name: Delete all folders older than 7 days
run: |
current_time=$(date +%s)
for dir in `find ./ -maxdepth 1 -type d -name '20*'`; do
dir_creation_time=$(git log -1 --format='%at' "$dir")
time_diff=$((current_time - dir_creation_time))
# If the directory is older than 7 days
if [ "$time_diff" -gt 604800 ]; then
echo "Deleting $dir"
rm -rf "$dir"
else
echo "Keeping $dir"
fi
done
- name: Commit all removed folders
uses: stefanzweifel/git-auto-commit-action@v5
with:
branch: gh-pages
commit_message: Delete folders older than 7 days
68 changes: 68 additions & 0 deletions ci-reporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type {
Reporter, FullConfig, Suite, TestCase, TestResult, FullResult
} from '@playwright/test/reporter';

class MyReporter implements Reporter {
suite!: Suite

onBegin(config: FullConfig, suite: Suite) {
this.suite = suite;
}

onEnd(result: FullResult) {
let passed = 0;
let failed = 0;
let flaky = 0;
let skipped = 0;
let internal_error = false;

this.suite.suites.forEach((project) => {
project.allTests().forEach((test) => {
if (test.results[0].status === "passed")
passed += 1;
else if (test.results[0].status === "skipped")
skipped += 1;
else if (test.retries === 0) {
if (test.results[0].status === "failed")
failed += 1;
else
internal_error = true;
} else if (test.retries > 0) {
if (test.retries + 1 === test.results.length) {
if (test.results[test.retries].status === "passed")
flaky += 1;
else if (test.results[test.retries].status === "failed")
failed += 1;
else
internal_error = true;
} else if (test.results[test.results.length - 1].status === "passed")
flaky += 1;
else
internal_error = true;
}
})
});

if (internal_error) {
console.log("Failed to parse test results!")
return;
}

let skipped_msg = "";
if (skipped > 0)
skipped_msg = `${skipped} skipped`;

let flaky_msg = "";
if (flaky > 0)
flaky_msg = `${flaky} flaky`;

let msg = ""
if (failed > 0)
msg = `❌ ${failed} failed test(s)`;
else
msg = `✅ ${passed} passed test(s)`;

console.log([msg, flaky_msg, skipped_msg].filter(Boolean).join(", "));
}
}
export default MyReporter;
4 changes: 3 additions & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default defineConfig({
{ useDetails: true, outputDir: 'playwright-ctrf', outputFile: 'playwright-ctrf.json' },
],
['html', { outputFolder: 'playwright-report' }],
['./ci-reporter'],
]
: 'list',
timeout: process.env.CI ? 60000 : 30000,
Expand All @@ -34,9 +35,10 @@ export default defineConfig({
}
: {}),
baseURL: process.env.BASE_URL,
trace: 'on-first-retry',
trace: 'retain-on-failure',
ignoreHTTPSErrors: true,
testIdAttribute: 'data-ouia-component-id'
screenshot: 'only-on-failure',
},
projects: [
{ name: 'setup', testMatch: /.*\.setup\.ts/ },
Expand Down

0 comments on commit 2a4a9ec

Please sign in to comment.