-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_automation.sh
More file actions
executable file
Β·230 lines (185 loc) Β· 7.92 KB
/
test_automation.sh
File metadata and controls
executable file
Β·230 lines (185 loc) Β· 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/bash
# iOS App Testing Automation Script
# This script builds your app and installs it on multiple simulators
set -e # Exit on any error
# Cleanup function
cleanup() {
echo -e "\n${YELLOW}π§Ή Cleaning up...${NC}"
if [ -d "./DerivedData" ]; then
rm -rf ./DerivedData
fi
exit 1
}
# Handle interruptions
trap cleanup INT TERM
# Configuration - Auto-detect project path
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_PATH="$SCRIPT_DIR/Hostage.xcodeproj"
SCHEME_NAME="Hostage"
BUILD_CONFIG="Debug"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}π Starting iOS App Testing Automation${NC}"
echo "======================================"
# Check if project exists
if [ ! -d "$PROJECT_PATH" ]; then
echo -e "${RED}β Project not found at: $PROJECT_PATH${NC}"
echo -e "${YELLOW}π‘ Make sure you're running this script from the project root directory${NC}"
exit 1
fi
echo -e "${GREEN}β
Project found at: $PROJECT_PATH${NC}"
# Function to close all simulators
close_all_simulators() {
echo -e "${YELLOW}π Closing all open simulators...${NC}"
local booted_simulators
booted_simulators=$(xcrun simctl list devices | grep "(Booted)" | grep -o '([A-F0-9-]*)' | tr -d '()')
if [ -z "$booted_simulators" ]; then
echo -e "${GREEN}β
No simulators currently running${NC}"
return
fi
while IFS= read -r udid; do
[ -n "$udid" ] && xcrun simctl shutdown "$udid" 2>/dev/null || true
done <<< "$booted_simulators"
sleep 2
echo -e "${GREEN}β
All simulators closed${NC}"
}
# Function to launch Simulator app
launch_simulator_app() {
echo -e "${YELLOW}π Launching Simulator app...${NC}"
open -a Simulator
sleep 3 # Give the app time to launch
# Close any default simulator that might have opened
local default_booted
default_booted=$(xcrun simctl list devices | grep "(Booted)" | grep -o '([A-F0-9-]*)' | tr -d '()')
if [ -n "$default_booted" ]; then
echo -e "${YELLOW}π Closing default simulator that opened...${NC}"
while IFS= read -r udid; do
[ -n "$udid" ] && xcrun simctl shutdown "$udid" 2>/dev/null || true
done <<< "$default_booted"
sleep 2
fi
echo -e "${GREEN}β
Simulator app launched and ready${NC}"
}
# Close any existing simulators before starting
close_all_simulators
# Launch the Simulator app (this will open it in the background)
launch_simulator_app
# Function to check if simulator exists
check_simulator() {
local device_name="$1"
xcrun simctl list devices | grep -E "^[[:space:]]*${device_name}[[:space:]]*\(" | grep -v "unavailable" | head -1 > /dev/null
}
# Boot simulator
boot_simulator() {
local device_udid="$1"
local device_name="$2"
local state
state=$(xcrun simctl list devices | grep "$device_udid" | grep -o "([^)]*)" | tail -1 | tr -d "()")
if [ "$state" != "Booted" ]; then
echo -e "${YELLOW}β³ Booting $device_name...${NC}"
xcrun simctl boot "$device_udid"
sleep 5
else
echo -e "${GREEN}β
$device_name is already booted${NC}"
fi
}
# Get bundle ID
get_bundle_id() {
xcodebuild -project "$PROJECT_PATH" -scheme "$SCHEME_NAME" -showBuildSettings | grep PRODUCT_BUNDLE_IDENTIFIER | head -1 | cut -d'=' -f2 | xargs
}
# Install app
install_app() {
local device_udid="$1"
local device_name="$2"
local app_path="$3"
local bundle_id="$4"
echo -e "${YELLOW}π± Installing app on $device_name...${NC}"
xcrun simctl install "$device_udid" "$app_path"
echo -e "${YELLOW}π Launching app on $device_name...${NC}"
xcrun simctl launch "$device_udid" "$bundle_id"
}
# NYC locations spaced ~40 feet apart around Times Square area
# Each coordinate is approximately 40-50 feet from the next
declare -a NYC_LOCATIONS=(
"40.758896,-73.985130" # Times Square North
"40.758775,-73.985130" # 40ft South
"40.758896,-73.985250" # 40ft West
"40.758775,-73.985250" # 40ft Southwest
"40.759017,-73.985130" # 40ft North
"40.758896,-73.985010" # 40ft East
"40.759017,-73.985250" # 40ft Northwest
"40.758775,-73.985010" # 40ft Southeast
"40.759138,-73.985130" # 50ft North
"40.758654,-73.985130" # 50ft South
)
# Set location for simulator
set_simulator_location() {
local device_udid="$1"
local device_name="$2"
local location_index="$3"
if [ "$location_index" -ge "${#NYC_LOCATIONS[@]}" ]; then
# If we run out of predefined locations, cycle back to the beginning
location_index=$((location_index % ${#NYC_LOCATIONS[@]}))
fi
local coordinates="${NYC_LOCATIONS[$location_index]}"
local latitude=$(echo "$coordinates" | cut -d',' -f1)
local longitude=$(echo "$coordinates" | cut -d',' -f2)
echo -e "${YELLOW}π Setting location for $device_name to ($latitude, $longitude)...${NC}"
xcrun simctl location "$device_udid" set "$latitude,$longitude"
echo -e "${GREEN}β
Location set for $device_name${NC}"
}
# Build project
echo -e "${YELLOW}π Detecting bundle identifier...${NC}"
BUNDLE_ID=$(get_bundle_id)
[ -z "$BUNDLE_ID" ] && echo -e "${RED}β Could not detect bundle identifier${NC}" && exit 1
echo -e "${GREEN}β
Bundle ID: $BUNDLE_ID${NC}"
[ -d "./DerivedData" ] && rm -rf ./DerivedData
echo -e "${YELLOW}π§Ή Cleaning project...${NC}"
xcodebuild clean -project "$PROJECT_PATH" -scheme "$SCHEME_NAME" -configuration "$BUILD_CONFIG" -sdk iphonesimulator
echo -e "${YELLOW}π¨ Building for simulator...${NC}"
xcodebuild build -project "$PROJECT_PATH" -scheme "$SCHEME_NAME" -configuration "$BUILD_CONFIG" -sdk iphonesimulator -derivedDataPath ./DerivedData || exit 1
APP_PATH=$(find ./DerivedData -name "Hostage.app" -type d | head -1)
[ -z "$APP_PATH" ] && echo -e "${RED}β Could not find built app${NC}" && exit 1
echo -e "${GREEN}β
App built successfully at: $APP_PATH${NC}"
# Get available simulators
declare -a AVAILABLE_IPHONES
while IFS= read -r line; do
[ -n "$line" ] && AVAILABLE_IPHONES+=("$line")
done < <(xcrun simctl list devices | grep -E "iPhone.*\(" | grep -v "unavailable" | sed 's/[[:space:]]*\(iPhone[^(]*\)[[:space:]]*(.*/\1/' | sort -u)
[ ${#AVAILABLE_IPHONES[@]} -eq 0 ] && echo -e "${RED}β No iPhone simulators found${NC}" && exit 1
echo -e "${GREEN}β
Found ${#AVAILABLE_IPHONES[@]} iPhone simulators:${NC}"
for iphone in "${AVAILABLE_IPHONES[@]}"; do
echo " β’ $iphone"
done
# Ask how many simulators to open
echo -n "How many simulators would you like to open? "
read num_sims
if ! [[ "$num_sims" =~ ^[0-9]+$ ]] || [ "$num_sims" -le 0 ]; then
echo -e "${RED}β Please enter a valid positive number${NC}"
exit 1
fi
if [ "$num_sims" -gt "${#AVAILABLE_IPHONES[@]}" ]; then
echo -e "${YELLOW}β οΈ Only ${#AVAILABLE_IPHONES[@]} simulators available. Using all.${NC}"
num_sims=${#AVAILABLE_IPHONES[@]}
fi
SIMULATORS_TO_USE=("${AVAILABLE_IPHONES[@]:0:$num_sims}")
# Launch requested simulators
location_index=0
for device_name in "${SIMULATORS_TO_USE[@]}"; do
echo -e "\n${BLUE}π± Processing $device_name${NC}"
check_simulator "$device_name" || { echo -e "${RED}β Simulator '$device_name' not found. Skipping...${NC}"; continue; }
device_udid=$(xcrun simctl list devices | grep -E "^[[:space:]]*${device_name}[[:space:]]*\(" | grep -v "unavailable" | head -1 | grep -o '([A-F0-9-]*)')
device_udid=${device_udid//[()]/}
[ -z "$device_udid" ] && echo -e "${RED}β Could not get UDID for $device_name${NC}" && continue
boot_simulator "$device_udid" "$device_name"
set_simulator_location "$device_udid" "$device_name" "$location_index"
install_app "$device_udid" "$device_name" "$APP_PATH" "$BUNDLE_ID"
echo -e "${GREEN}β
$device_name setup complete${NC}"
# Increment location index for next simulator
((location_index++))
done
echo -e "\n${GREEN}π All simulators are ready for testing!${NC}"