|
| 1 | +#!/bin/bash |
| 2 | +set -e # Exit on error |
| 3 | +set -x # Print commands |
| 4 | + |
| 5 | +############################################################################### |
| 6 | +# Android Emulator Test Runner |
| 7 | +# |
| 8 | +# This script installs apps on Android emulator, captures debug info, |
| 9 | +# and runs Maestro tests. |
| 10 | +# |
| 11 | +# Usage: ./run-android-tests.sh <working-directory> |
| 12 | +############################################################################### |
| 13 | + |
| 14 | +WORKING_DIR="${1:-.}" |
| 15 | +DEBUG_DIR="$WORKING_DIR/debug-artifacts" |
| 16 | +APP_PACKAGE="com.reown.appkit.expomultichain" |
| 17 | +WALLET_PACKAGE="com.walletconnect.web3wallet.rnsample.internal" |
| 18 | + |
| 19 | +############################################################################### |
| 20 | +# Setup and Preparation |
| 21 | +############################################################################### |
| 22 | + |
| 23 | +echo "Setting up debug directory..." |
| 24 | +mkdir -p "$DEBUG_DIR" |
| 25 | + |
| 26 | +echo "Waiting for emulator to be ready..." |
| 27 | +adb wait-for-device |
| 28 | + |
| 29 | +echo "Waiting for boot completion..." |
| 30 | +adb shell 'while [ "$(getprop sys.boot_completed)" != "1" ]; do sleep 2; done' |
| 31 | +echo "Boot completed!" |
| 32 | +adb devices |
| 33 | + |
| 34 | +############################################################################### |
| 35 | +# Start Logcat Capture |
| 36 | +############################################################################### |
| 37 | + |
| 38 | +echo "Starting logcat capture..." |
| 39 | +adb logcat -c |
| 40 | +adb logcat > "$DEBUG_DIR/full-logcat.txt" & |
| 41 | +LOGCAT_PID=$! |
| 42 | + |
| 43 | +# Ensure logcat is killed on script exit |
| 44 | +trap "kill $LOGCAT_PID 2>/dev/null || true" EXIT |
| 45 | + |
| 46 | +############################################################################### |
| 47 | +# Install Applications |
| 48 | +############################################################################### |
| 49 | + |
| 50 | +echo "Installing APKs..." |
| 51 | +adb install -r "$WORKING_DIR/app-dev.apk" |
| 52 | +adb install -r "$WORKING_DIR/wallet.apk" |
| 53 | + |
| 54 | +echo "Verifying app installation..." |
| 55 | +if ! adb shell pm list packages | grep -q "$APP_PACKAGE"; then |
| 56 | + echo "ERROR: App not installed correctly" |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | + |
| 60 | +if ! adb shell pm list packages | grep -q "$WALLET_PACKAGE"; then |
| 61 | + echo "ERROR: Wallet not installed correctly" |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | + |
| 65 | +echo "Checking APK details:" |
| 66 | +adb shell dumpsys package "$APP_PACKAGE" | grep -A 5 "versionName" || true |
| 67 | + |
| 68 | +echo "Clearing app data for clean state..." |
| 69 | +adb shell pm clear "$APP_PACKAGE" || true |
| 70 | + |
| 71 | +echo "Waiting for system to settle..." |
| 72 | +sleep 5 |
| 73 | + |
| 74 | +############################################################################### |
| 75 | +# Capture Debug Information |
| 76 | +############################################################################### |
| 77 | + |
| 78 | +echo "=== CAPTURING DEBUG INFO ===" |
| 79 | + |
| 80 | +# Network state (critical for WalletConnect) |
| 81 | +echo "Capturing network state..." |
| 82 | +{ |
| 83 | + echo "Network state:" |
| 84 | + adb shell dumpsys connectivity |
| 85 | + adb shell settings get global airplane_mode_on |
| 86 | +} > "$DEBUG_DIR/network_state.txt" |
| 87 | + |
| 88 | +# Memory info |
| 89 | +echo "Capturing memory info..." |
| 90 | +adb shell dumpsys meminfo "$APP_PACKAGE" > "$DEBUG_DIR/meminfo.txt" || true |
| 91 | + |
| 92 | +# Process info |
| 93 | +echo "Capturing process info..." |
| 94 | +adb shell ps | grep reown > "$DEBUG_DIR/processes.txt" || true |
| 95 | + |
| 96 | +# Manual app launch test |
| 97 | +echo "Attempting manual app launch..." |
| 98 | +adb shell monkey -p "$APP_PACKAGE" -c android.intent.category.LAUNCHER 1 |
| 99 | +sleep 5 |
| 100 | + |
| 101 | +# Verify app is running |
| 102 | +if ! adb shell ps | grep -q "$APP_PACKAGE"; then |
| 103 | + echo "WARNING: App may not be running" |
| 104 | +fi |
| 105 | + |
| 106 | +# Screenshot before tests |
| 107 | +echo "Taking screenshot..." |
| 108 | +adb shell screencap /sdcard/app_state_before_maestro.png |
| 109 | +adb pull /sdcard/app_state_before_maestro.png "$DEBUG_DIR/" || true |
| 110 | + |
| 111 | +# Current activity |
| 112 | +echo "Current activity before Maestro:" |
| 113 | +adb shell dumpsys activity activities | grep -E "mResumedActivity|mFocusedActivity" || true |
| 114 | + |
| 115 | +############################################################################### |
| 116 | +# Run Maestro Tests |
| 117 | +############################################################################### |
| 118 | + |
| 119 | +echo "=== RUNNING MAESTRO TESTS ===" |
| 120 | + |
| 121 | +# Track test results for GitHub Actions output |
| 122 | +# Define tests array: filename:display_name |
| 123 | +tests=( |
| 124 | + "basic-smoke-test:Basic Smoke Test" |
| 125 | + "wallet-qr-load:Wallet QR Load" |
| 126 | + "switch-network:Switch Network" |
| 127 | + "account-activity:Account Activity" |
| 128 | + "send:Send" |
| 129 | + "swaps:Swaps" |
| 130 | + "onramp:Onramp" |
| 131 | +) |
| 132 | + |
| 133 | +# Initialize result tracking |
| 134 | +test_results="" |
| 135 | +failed_tests="" |
| 136 | +passed_count=0 |
| 137 | +failed_count=0 |
| 138 | + |
| 139 | +# Run each test and track results |
| 140 | +MAX_RETRIES=2 |
| 141 | + |
| 142 | +for test_pair in "${tests[@]}"; do |
| 143 | + test_file="${test_pair%%:*}" |
| 144 | + test_name="${test_pair##*:}" |
| 145 | + |
| 146 | + echo "" |
| 147 | + echo "📱 Running $test_name..." |
| 148 | + |
| 149 | + # Record start time |
| 150 | + start_time=$(date +%s) |
| 151 | + |
| 152 | + # Retry logic |
| 153 | + attempt=0 |
| 154 | + test_passed=false |
| 155 | + |
| 156 | + while [ $attempt -le $MAX_RETRIES ]; do |
| 157 | + if [ $attempt -gt 0 ]; then |
| 158 | + echo "⚠️ Retry attempt $attempt for $test_name..." |
| 159 | + sleep 3 # Brief pause between retries |
| 160 | + fi |
| 161 | + |
| 162 | + if (cd "$WORKING_DIR/.maestro" && maestro test "$test_file.yaml"); then |
| 163 | + test_passed=true |
| 164 | + break |
| 165 | + else |
| 166 | + attempt=$((attempt + 1)) |
| 167 | + fi |
| 168 | + done |
| 169 | + |
| 170 | + end_time=$(date +%s) |
| 171 | + duration=$((end_time - start_time)) |
| 172 | + |
| 173 | + if [ "$test_passed" = true ]; then |
| 174 | + echo "✅ $test_name passed (${duration}s)" |
| 175 | + if [ $attempt -gt 0 ]; then |
| 176 | + test_results="${test_results}✅ $test_name (${duration}s, passed after $attempt retries)\n" |
| 177 | + else |
| 178 | + test_results="${test_results}✅ $test_name (${duration}s)\n" |
| 179 | + fi |
| 180 | + passed_count=$((passed_count + 1)) |
| 181 | + else |
| 182 | + echo "❌ $test_name failed after $MAX_RETRIES retries (${duration}s)" |
| 183 | + test_results="${test_results}❌ $test_name (${duration}s, failed after $MAX_RETRIES retries)\n" |
| 184 | + failed_tests="${failed_tests}• $test_name\n" |
| 185 | + failed_count=$((failed_count + 1)) |
| 186 | + fi |
| 187 | +done |
| 188 | + |
| 189 | +# Output results to GitHub Actions |
| 190 | +if [ -n "$GITHUB_OUTPUT" ]; then |
| 191 | + echo "test_results<<EOF" >> "$GITHUB_OUTPUT" |
| 192 | + echo -e "$test_results" >> "$GITHUB_OUTPUT" |
| 193 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 194 | + |
| 195 | + echo "failed_tests<<EOF" >> "$GITHUB_OUTPUT" |
| 196 | + echo -e "$failed_tests" >> "$GITHUB_OUTPUT" |
| 197 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 198 | + |
| 199 | + echo "passed_count=$passed_count" >> "$GITHUB_OUTPUT" |
| 200 | + echo "failed_count=$failed_count" >> "$GITHUB_OUTPUT" |
| 201 | +fi |
| 202 | + |
| 203 | +# Print summary |
| 204 | +echo "" |
| 205 | +echo "=== TEST SUMMARY ===" |
| 206 | +echo "Passed: $passed_count" |
| 207 | +echo "Failed: $failed_count" |
| 208 | +echo "" |
| 209 | +echo -e "$test_results" |
| 210 | + |
| 211 | +# Exit with error if any tests failed |
| 212 | +if [ $failed_count -gt 0 ]; then |
| 213 | + echo "Some tests failed" |
| 214 | + exit 1 |
| 215 | +fi |
| 216 | + |
| 217 | +echo "All tests passed! ✓" |
| 218 | + |
| 219 | +############################################################################### |
| 220 | +# Post-Test Diagnostics |
| 221 | +############################################################################### |
| 222 | + |
| 223 | +echo "Checking for crashes..." |
| 224 | +if adb logcat -d | grep -E "FATAL EXCEPTION|AndroidRuntime|Process:.*com.reown" > "$DEBUG_DIR/crashes.txt"; then |
| 225 | + echo "WARNING: Crashes detected, check $DEBUG_DIR/crashes.txt" |
| 226 | +else |
| 227 | + echo "No crashes found" > "$DEBUG_DIR/crashes.txt" |
| 228 | +fi |
| 229 | + |
| 230 | +echo "=== TEST RUN COMPLETE ===" |
| 231 | + |
0 commit comments