@@ -63,6 +63,11 @@ take_simulator_screenshot() {
6363 xcrun simctl io booted screenshot " $screenshot_name " 2> /dev/null || true
6464}
6565
66+ # Check if the app is currently running
67+ is_app_running () {
68+ xcrun simctl spawn booted launchctl list | grep " $BUNDLE_ID " > /dev/null 2>&1
69+ }
70+
6671log " Removing previous screenshots directory."
6772rm -rf " $SCREENSHOTS_DIR "
6873
@@ -96,15 +101,19 @@ log "Launching app with expected crash."
96101xcrun simctl launch $DEVICE_ID $BUNDLE_ID
97102
98103# Check every 100ms for 5 seconds if the app is still running.
99- for i in {1..50}; do
100- if xcrun simctl listapps $DEVICE_ID | grep " $BUNDLE_ID " | grep -q " Running" ; then
101- sleep 0.1
104+ start_time=$( date +%s)
105+ while true ; do
106+ if is_app_running; then
107+ log " ⏳ App is still running"
102108 else
103- log " ✅ App crashed as expected after $( echo " scale=1; $i * 0.1 " | bc ) seconds. "
109+ log " ✅ App crashed as expected 🔥 🚀 💥 "
104110 break
105111 fi
112+
113+ current_time=$( date +%s)
114+ elapsed=$(( current_time - start_time))
106115
107- if [ " $i " -eq 50 ]; then
116+ if [ $elapsed -ge 5 ]; then
108117 log " ❌ App is still running after 5 seconds but it should have crashed instead."
109118 exit 1
110119 fi
@@ -118,19 +127,39 @@ log "Removing crash flag..."
118127xcrun simctl spawn $DEVICE_ID defaults delete $BUNDLE_ID $USER_DEFAULT_KEY
119128
120129log " Relaunching app after crash."
121- xcrun simctl launch $DEVICE_ID $BUNDLE_ID
130+
131+ # We do this in the background because the command could block indefinitely.
132+ # Instead, we iterate below to check if the app is running.
133+ xcrun simctl launch $DEVICE_ID $BUNDLE_ID &
122134
123135take_simulator_screenshot " after-crash-check"
124136
125- log " Waiting for 5 seconds to check if the app is still running."
126- sleep 5
137+ # Check for 5 seconds if the app is running.
138+ start_time=$( date +%s)
139+ while true ; do
140+ if is_app_running; then
141+ log " ⏳ App is still running."
142+ else
143+ log " ❌ App is not running."
144+ fi
145+
146+ current_time=$( date +%s)
147+ elapsed=$(( current_time - start_time))
148+
149+ if [ $elapsed -ge 5 ]; then
150+ log " ✅ Completed checking if app is still running."
151+ break
152+ fi
153+ done
127154
128155take_simulator_screenshot " after-crash-check-after-sleep"
129156
130- if xcrun simctl spawn booted launchctl list | grep " $BUNDLE_ID " ; then
131- log " ✅ App is still running"
157+ log " Checking if app is still running one more time."
158+
159+ if is_app_running; then
160+ log " ✅ App is still running."
132161else
133- log " ❌ App is not running"
162+ log " ❌ App is not running. "
134163 exit 1
135164fi
136165
0 commit comments