|
| 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 |
0 commit comments