Skip to content

Commit 42b46cc

Browse files
authored
adds script for running secifiec cypress tests until failure and save logs (for flaky tests) (#3623)
1 parent 4fd4751 commit 42b46cc

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

cypress.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const CYPRESS_WINDOW_HEIGHT = env.parsed?.CYPRESS_WINDOW_HEIGHT || 1080;
1111
module.exports = defineConfig({
1212
e2e: {
1313
setupNodeEvents(on, config) {
14+
require('cypress-terminal-report/src/installLogsPrinter')(on, { printLogsToConsole: 'always' });
1415
on('before:browser:launch', (browser, launchOptions) => {
1516
if (browser.name === 'electron') {
1617
launchOptions.preferences.width = CYPRESS_WINDOW_WIDTH;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"cypress-multi-reporters": "2.0.5",
7878
"cypress-network-idle": "1.15.0",
7979
"cypress-plugin-tab": "1.0.5",
80+
"cypress-terminal-report": "^7.2.1",
8081
"cypress-wait-until": "3.0.2",
8182
"dotenv": "17.2.1",
8283
"esbuild-loader": "4.3.0",

scripts/debug-flaky-cypress.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
3+
# Script to run specified Cypress test a certain number of times (defaults to 50) or until failure.
4+
# Writes logs to the $logs_folder
5+
6+
spec_arg=${1:-""} # Optional spec argument- defaults to running all tests if not specified
7+
max_runs=${2:-50} # Default to 50 if no argument provided
8+
counter=1
9+
logs_folder=scripts/debug-flaky-cypress-logs
10+
11+
# Handle Ctrl+C gracefully
12+
cleanup() {
13+
echo ""
14+
echo "Interrupted by user after $((counter-1)) runs"
15+
# Kill any running cypress processes
16+
pkill -f "cypress"
17+
exit 0
18+
}
19+
20+
trap cleanup INT TERM
21+
22+
if [ -n "$spec_arg" ]; then
23+
echo "Running test up to $max_runs times with spec: $spec_arg"
24+
else
25+
echo "Running test up to $max_runs times..."
26+
fi
27+
28+
# If logs_folder doesn't exist, create it
29+
mkdir -p $logs_folder;
30+
31+
while [ $counter -le $max_runs ]; do
32+
echo "=== Run #$counter/$max_runs ==="
33+
34+
# Run the command and capture output
35+
if [ -n "$spec_arg" ]; then
36+
yarn cy:run --spec "$spec_arg" 2>&1 | tee "$logs_folder/run-$counter.log"
37+
else
38+
yarn cy:run 2>&1 | tee "$logs_folder/run-$counter.log"
39+
fi
40+
exit_code=$?
41+
echo "Exit code for run #$counter: $exit_code"
42+
43+
# Check for test failures in the output
44+
if grep -q "Failing:.*[1-9]" "$logs_folder/run-$counter.log" || \
45+
grep -q "✖.*failed" "$logs_folder/run-$counter.log" || \
46+
grep -q "failed (100%)" "$logs_folder/run-$counter.log"; then
47+
echo ""
48+
echo "==================================="
49+
echo "FAILURE DETECTED ON RUN #$counter"
50+
echo "==================================="
51+
echo "Test failed after $counter attempts"
52+
echo "Check $logs_folder/run-$counter.log for details"
53+
echo ""
54+
exit 0
55+
fi
56+
57+
# Also check exit code as backup
58+
if [ $exit_code -ne 0 ]; then
59+
echo ""
60+
echo "==================================="
61+
echo "FAILURE DETECTED ON RUN #$counter"
62+
echo "==================================="
63+
echo "Test failed with exit code $exit_code on the $counter attempt"
64+
echo "Check $logs_folder/run-$counter.log for details"
65+
echo ""
66+
exit 0 # Exit the function, not the script
67+
fi
68+
69+
echo "Run #$counter passed"
70+
counter=$((counter + 1))
71+
done
72+
73+
echo "All $max_runs runs passed!"
74+
return 0

test/e2e/support/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import '@percy/cypress';
1212

1313
import { register as registerSnapshot } from '@cypress/snapshot';
1414
import failOnConsoleError from 'cypress-fail-on-console-error';
15+
import installLogsCollector from 'cypress-terminal-report/src/installLogsCollector';
1516
import type { ConsoleMessage } from 'cypress-fail-on-console-error';
1617

1718
import { AppFrontend } from 'test/e2e/pageobjects/app-frontend';
@@ -58,3 +59,5 @@ Cypress.Commands.add('ignoreConsoleMessages', (consoleMessages: ConsoleMessage[]
5859
consoleMessages: config?.consoleMessages ? [...config.consoleMessages, ...consoleMessages] : consoleMessages,
5960
});
6061
});
62+
63+
installLogsCollector();

yarn.lock

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5575,6 +5575,7 @@ __metadata:
55755575
cypress-network-idle: "npm:1.15.0"
55765576
cypress-parallel: "npm:0.15.0"
55775577
cypress-plugin-tab: "npm:1.0.5"
5578+
cypress-terminal-report: "npm:^7.2.1"
55785579
cypress-wait-until: "npm:3.0.2"
55795580
date-fns: "npm:4.1.0"
55805581
dompurify: "npm:3.2.6"
@@ -7050,6 +7051,13 @@ __metadata:
70507051
languageName: node
70517052
linkType: hard
70527053

7054+
"compare-versions@npm:^6.1.1":
7055+
version: 6.1.1
7056+
resolution: "compare-versions@npm:6.1.1"
7057+
checksum: 10c0/415205c7627f9e4f358f571266422980c9fe2d99086be0c9a48008ef7c771f32b0fbe8e97a441ffedc3910872f917a0675fe0fe3c3b6d331cda6d8690be06338
7058+
languageName: node
7059+
linkType: hard
7060+
70537061
"compressible@npm:~2.0.18":
70547062
version: 2.0.18
70557063
resolution: "compressible@npm:2.0.18"
@@ -7523,6 +7531,19 @@ __metadata:
75237531
languageName: node
75247532
linkType: hard
75257533

7534+
"cypress-terminal-report@npm:^7.2.1":
7535+
version: 7.2.1
7536+
resolution: "cypress-terminal-report@npm:7.2.1"
7537+
dependencies:
7538+
chalk: "npm:^4.0.0"
7539+
compare-versions: "npm:^6.1.1"
7540+
superstruct: "npm:0.14.2"
7541+
peerDependencies:
7542+
cypress: ">=10.0.0"
7543+
checksum: 10c0/8396407f0fc5d8300c71e9026973fa38f370c22a28b08d1b53fa35a4595f7d626d975c304ac9f88c6bb4a239cdc42da163684fb8560ba7bc3bd43bcfce416515
7544+
languageName: node
7545+
linkType: hard
7546+
75267547
"cypress-wait-until@npm:3.0.2":
75277548
version: 3.0.2
75287549
resolution: "cypress-wait-until@npm:3.0.2"
@@ -16823,6 +16844,13 @@ __metadata:
1682316844
languageName: node
1682416845
linkType: hard
1682516846

16847+
"superstruct@npm:0.14.2":
16848+
version: 0.14.2
16849+
resolution: "superstruct@npm:0.14.2"
16850+
checksum: 10c0/e5518f6701524fb8cbae504a84dc9c304bf3fe01616230a5eb4e14af9bfc4e3518b94bfe457e57a5d1b99a2b54f82881b4a39e0b266caa6053f84aa294613b94
16851+
languageName: node
16852+
linkType: hard
16853+
1682616854
"supports-color@npm:^2.0.0":
1682716855
version: 2.0.0
1682816856
resolution: "supports-color@npm:2.0.0"

0 commit comments

Comments
 (0)