Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 86469bd

Browse files
authored
Add custom reporter to auto-report flaky Playwright tests (#12290)
1 parent 2402cd5 commit 86469bd

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

.github/workflows/end-to-end-tests.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ jobs:
156156
merge-multiple: true
157157

158158
- name: Merge into HTML Report
159-
run: yarn playwright merge-reports --reporter=html,github ./all-blob-reports
159+
run: yarn playwright merge-reports --reporter=html,github,./playwright/flaky-reporter.ts ./all-blob-reports
160+
env:
161+
# Only pass creds to the flaky-reporter on main branch runs
162+
GITHUB_TOKEN: ${{ github.event.workflow_run.head_branch == 'develop' && secrets.ELEMENT_BOT_TOKEN || '' }}
160163

161164
- name: Upload HTML report
162165
uses: actions/upload-artifact@v4

playwright/flaky-reporter.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2024 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/**
18+
* Flaky test reporter, creating & updating GitHub issues
19+
* Only intended to run from within GitHub Actions
20+
*/
21+
22+
import type { Reporter, TestCase } from "@playwright/test/reporter";
23+
24+
const REPO = "element-hq/element-web";
25+
const LABEL = "Z-Flaky-Test";
26+
const ISSUE_TITLE_PREFIX = "Flaky playwright test: ";
27+
28+
class FlakyReporter implements Reporter {
29+
private flakes = new Set<string>();
30+
31+
public onTestEnd(test: TestCase): void {
32+
const title = `${test.location.file.split("playwright/e2e/")[1]}: ${test.title}`;
33+
if (test.outcome() === "flaky") {
34+
this.flakes.add(title);
35+
}
36+
}
37+
38+
public async onExit(): Promise<void> {
39+
if (this.flakes.size === 0) {
40+
console.log("No flakes found");
41+
return;
42+
}
43+
44+
console.log("Found flakes: ");
45+
for (const flake of this.flakes) {
46+
console.log(flake);
47+
}
48+
49+
const { GITHUB_TOKEN, GITHUB_API_URL, GITHUB_SERVER_URL, GITHUB_REPOSITORY, GITHUB_RUN_ID } = process.env;
50+
if (!GITHUB_TOKEN) return;
51+
52+
const body = `${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}`;
53+
54+
const headers = { Authorization: `Bearer ${GITHUB_TOKEN}` };
55+
// Fetch all existing issues with the flaky-test label.
56+
const issuesRequest = await fetch(`${GITHUB_API_URL}/repos/${REPO}/issues?labels=${LABEL}`, { headers });
57+
const issues = await issuesRequest.json();
58+
for (const flake of this.flakes) {
59+
const title = ISSUE_TITLE_PREFIX + "`" + flake + "`";
60+
const existingIssue = issues.find((issue) => issue.title === title);
61+
62+
if (existingIssue) {
63+
console.log(`Found issue ${existingIssue.number} for ${flake}, adding comment...`);
64+
await fetch(`${existingIssue.url}/comments`, {
65+
method: "POST",
66+
headers,
67+
body: JSON.stringify({ body }),
68+
});
69+
} else {
70+
console.log(`Creating new issue for ${flake}...`);
71+
await fetch(`${GITHUB_API_URL}/repos/${REPO}/issues`, {
72+
method: "POST",
73+
headers,
74+
body: JSON.stringify({
75+
title,
76+
body,
77+
labels: [LABEL],
78+
}),
79+
});
80+
}
81+
}
82+
}
83+
}
84+
85+
export default FlakyReporter;

0 commit comments

Comments
 (0)