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

Commit dce6d7a

Browse files
committed
Add test reporter to prevent stale screenshots
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
1 parent 2e56ca3 commit dce6d7a

File tree

6 files changed

+116
-3
lines changed

6 files changed

+116
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ jobs:
190190

191191
- name: Merge into HTML Report
192192
if: inputs.skip != true
193-
run: yarn playwright merge-reports --reporter=html,./playwright/flaky-reporter.ts ./all-blob-reports
193+
run: yarn playwright merge-reports --reporter=html,./playwright/flaky-reporter.ts,./playwright/stale-screenshot-reporter.ts ./all-blob-reports
194194
env:
195195
# Only pass creds to the flaky-reporter on main branch runs
196196
GITHUB_TOKEN: ${{ github.ref_name == 'develop' && secrets.ELEMENT_BOT_TOKEN || '' }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
"fake-indexeddb": "^6.0.0",
209209
"fetch-mock-jest": "^1.5.1",
210210
"fs-extra": "^11.0.0",
211+
"glob": "^11.0.0",
211212
"jest": "^29.6.2",
212213
"jest-canvas-mock": "^2.5.2",
213214
"jest-environment-jsdom": "^29.6.2",

playwright/element-web-test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,17 @@ export const expect = baseExpect.extend({
302302
async toMatchScreenshot(
303303
this: ExpectMatcherState,
304304
receiver: Page | Locator,
305-
name?: `${string}.png`,
305+
name: `${string}.png`,
306306
options?: {
307307
mask?: Array<Locator>;
308308
omitBackground?: boolean;
309309
timeout?: number;
310310
css?: string;
311311
},
312312
) {
313+
const testInfo = test.info();
314+
if (!testInfo) throw new Error(`toMatchScreenshot() must be called during the test`);
315+
313316
const page = "page" in receiver ? receiver.page() : receiver;
314317

315318
// We add a custom style tag before taking screenshots
@@ -346,6 +349,13 @@ export const expect = baseExpect.extend({
346349
await baseExpect(receiver).toHaveScreenshot(name, options);
347350

348351
await style.evaluate((tag) => tag.remove());
352+
353+
testInfo.annotations.push({
354+
// `_` prefix hides it from the HTML reporter
355+
type: "_screenshot",
356+
description: testInfo.snapshotPath(name),
357+
});
358+
349359
return { pass: true, message: () => "", name: "toMatchScreenshot" };
350360
},
351361
});

playwright/flaky-reporter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ limitations under the License.
2020
*/
2121

2222
import type { Reporter, TestCase } from "@playwright/test/reporter";
23+
import StaleScreenshotReporter from "./stale-screenshot-reporter";
2324

2425
const REPO = "element-hq/element-web";
2526
const LABEL = "Z-Flaky-Test";
2627
const ISSUE_TITLE_PREFIX = "Flaky playwright test: ";
2728

28-
class FlakyReporter implements Reporter {
29+
// TEMP TO DRY RUN
30+
class FlakyReporter implements StaleScreenshotReporter {
2931
private flakes = new Set<string>();
3032

3133
public onTestEnd(test: TestCase): void {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
* Test reporter which compares the reported screenshots vs those on disk to find stale screenshots
19+
* Only intended to run from within GitHub Actions
20+
*/
21+
22+
import path from "node:path";
23+
import { glob } from "glob";
24+
25+
import type { Reporter, TestCase } from "@playwright/test/reporter";
26+
27+
const snapshotRoot = path.join(__dirname, "snapshots");
28+
29+
class StaleScreenshotReporter implements Reporter {
30+
private screenshots = new Set<string>();
31+
32+
public onTestEnd(test: TestCase): void {
33+
for (const annotation of test.annotations) {
34+
if (annotation.type === "_screenshot") {
35+
this.screenshots.add(annotation.description);
36+
}
37+
}
38+
}
39+
40+
public async onExit(): Promise<void> {
41+
const screenshotFiles = new Set(await glob(`**/*.png`, { cwd: snapshotRoot }));
42+
for (const screenshot of this.screenshots) {
43+
screenshotFiles.delete(screenshot);
44+
}
45+
console.log("~~", screenshotFiles);
46+
}
47+
}
48+
49+
export default StaleScreenshotReporter;

yarn.lock

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5365,6 +5365,18 @@ glob@^10.3.7:
53655365
minipass "^7.0.4"
53665366
path-scurry "^1.11.0"
53675367

5368+
glob@^11.0.0:
5369+
version "11.0.0"
5370+
resolved "https://registry.yarnpkg.com/glob/-/glob-11.0.0.tgz#6031df0d7b65eaa1ccb9b29b5ced16cea658e77e"
5371+
integrity sha512-9UiX/Bl6J2yaBbxKoEBRm4Cipxgok8kQYcOPEhScPwebu2I0HoQOuYdIO6S3hLuWoZgpDpwQZMzTFxgpkyT76g==
5372+
dependencies:
5373+
foreground-child "^3.1.0"
5374+
jackspeak "^4.0.1"
5375+
minimatch "^10.0.0"
5376+
minipass "^7.1.2"
5377+
package-json-from-dist "^1.0.0"
5378+
path-scurry "^2.0.0"
5379+
53685380
glob@^7.1.3, glob@^7.1.4, glob@^7.2.0:
53695381
version "7.2.3"
53705382
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.3.tgz#b8df0fb802bbfa8e89bd1d938b4e16578ed44f2b"
@@ -6052,6 +6064,15 @@ jackspeak@^2.3.6:
60526064
optionalDependencies:
60536065
"@pkgjs/parseargs" "^0.11.0"
60546066

6067+
jackspeak@^4.0.1:
6068+
version "4.0.1"
6069+
resolved "https://registry.yarnpkg.com/jackspeak/-/jackspeak-4.0.1.tgz#9fca4ce961af6083e259c376e9e3541431f5287b"
6070+
integrity sha512-cub8rahkh0Q/bw1+GxP7aeSe29hHHn2V4m29nnDlvCdlgU+3UGxkZp7Z53jLUdpX3jdTO0nJZUDl3xvbWc2Xog==
6071+
dependencies:
6072+
"@isaacs/cliui" "^8.0.2"
6073+
optionalDependencies:
6074+
"@pkgjs/parseargs" "^0.11.0"
6075+
60556076
jest-canvas-mock@^2.5.2:
60566077
version "2.5.2"
60576078
resolved "https://registry.yarnpkg.com/jest-canvas-mock/-/jest-canvas-mock-2.5.2.tgz#7e21ebd75e05ab41c890497f6ba8a77f915d2ad6"
@@ -6769,6 +6790,11 @@ lru-cache@^10.2.0:
67696790
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.2.2.tgz#48206bc114c1252940c41b25b41af5b545aca878"
67706791
integrity sha512-9hp3Vp2/hFQUiIwKo8XCeFVnrg8Pk3TYNPIR7tJADKi5YfcF7vEaK7avFHTlSy3kOKYaJQaalfEo6YuXdceBOQ==
67716792

6793+
lru-cache@^11.0.0:
6794+
version "11.0.0"
6795+
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-11.0.0.tgz#15d93a196f189034d7166caf9fe55e7384c98a21"
6796+
integrity sha512-Qv32eSV1RSCfhY3fpPE2GNZ8jgM9X7rdAfemLWqTUxwiyIC4jJ6Sy0fZ8H+oLWevO6i4/bizg7c8d8i6bxrzbA==
6797+
67726798
lru-cache@^5.1.1:
67736799
version "5.1.1"
67746800
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
@@ -7004,6 +7030,13 @@ min-indent@^1.0.0:
70047030
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869"
70057031
integrity sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==
70067032

7033+
minimatch@^10.0.0:
7034+
version "10.0.1"
7035+
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-10.0.1.tgz#ce0521856b453c86e25f2c4c0d03e6ff7ddc440b"
7036+
integrity sha512-ethXTt3SGGR+95gudmqJ1eNhRO7eGEGIgYA9vnPatK4/etz2MEVDno5GMCibdMTuBMyElzIlgxMna3K94XDIDQ==
7037+
dependencies:
7038+
brace-expansion "^2.0.1"
7039+
70077040
minimatch@^3.0.4, minimatch@^3.0.5, minimatch@^3.1.1, minimatch@^3.1.2:
70087041
version "3.1.2"
70097042
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b"
@@ -7028,6 +7061,11 @@ minimist@^1.2.0, minimist@^1.2.5, minimist@^1.2.6, minimist@^1.2.8, minimist@~1.
70287061
resolved "https://registry.yarnpkg.com/minipass/-/minipass-7.1.1.tgz#f7f85aff59aa22f110b20e27692465cf3bf89481"
70297062
integrity sha512-UZ7eQ+h8ywIRAW1hIEl2AqdwzJucU/Kp59+8kkZeSvafXhZjul247BvIJjEVFVeON6d7lM46XX1HXCduKAS8VA==
70307063

7064+
minipass@^7.1.2:
7065+
version "7.1.2"
7066+
resolved "https://registry.yarnpkg.com/minipass/-/minipass-7.1.2.tgz#93a9626ce5e5e66bd4db86849e7515e92340a707"
7067+
integrity sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw==
7068+
70317069
mkdirp@1.0.4, mkdirp@^1.0.4:
70327070
version "1.0.4"
70337071
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
@@ -7327,6 +7365,11 @@ p-try@^2.0.0:
73277365
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
73287366
integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==
73297367

7368+
package-json-from-dist@^1.0.0:
7369+
version "1.0.0"
7370+
resolved "https://registry.yarnpkg.com/package-json-from-dist/-/package-json-from-dist-1.0.0.tgz#e501cd3094b278495eb4258d4c9f6d5ac3019f00"
7371+
integrity sha512-dATvCeZN/8wQsGywez1mzHtTlP22H8OEfPrVMLNr4/eGa+ijtLn/6M5f0dY8UKNrC2O9UCU6SSoG3qRKnt7STw==
7372+
73307373
pako@^2.0.3:
73317374
version "2.1.0"
73327375
resolved "https://registry.yarnpkg.com/pako/-/pako-2.1.0.tgz#266cc37f98c7d883545d11335c00fbd4062c9a86"
@@ -7409,6 +7452,14 @@ path-scurry@^1.11.0:
74097452
lru-cache "^10.2.0"
74107453
minipass "^5.0.0 || ^6.0.2 || ^7.0.0"
74117454

7455+
path-scurry@^2.0.0:
7456+
version "2.0.0"
7457+
resolved "https://registry.yarnpkg.com/path-scurry/-/path-scurry-2.0.0.tgz#9f052289f23ad8bf9397a2a0425e7b8615c58580"
7458+
integrity sha512-ypGJsmGtdXUOeM5u93TyeIEfEhM6s+ljAhrk5vAvSx8uyY/02OvrZnA0YNGUrPXfpJMgI1ODd3nwz8Npx4O4cg==
7459+
dependencies:
7460+
lru-cache "^11.0.0"
7461+
minipass "^7.1.2"
7462+
74127463
path-to-regexp@0.1.7:
74137464
version "0.1.7"
74147465
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"

0 commit comments

Comments
 (0)