-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenv-setup-run.sh
More file actions
548 lines (432 loc) · 18.4 KB
/
env-setup-run.sh
File metadata and controls
548 lines (432 loc) · 18.4 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#!/usr/bin/env bash
# shellcheck shell=bash
setup_environment() {
local setup_type=$1
local tech_stack=$2
local max_parallels
log_section "📦 Project Setup"
# Set variables based on setup type
if [ "$setup_type" = "web" ]; then
log_msg_to "Team max parallels for web: $TEAM_PARALLELS_MAX_ALLOWED_WEB" "$NOW_RUN_LOG_FILE"
max_parallels=$TEAM_PARALLELS_MAX_ALLOWED_WEB
else
log_msg_to "Team max parallels for mobile: $TEAM_PARALLELS_MAX_ALLOWED_MOBILE" "$NOW_RUN_LOG_FILE"
max_parallels=$TEAM_PARALLELS_MAX_ALLOWED_MOBILE
fi
log_msg_to "Starting ${setup_type} setup for " "$tech_stack" "$NOW_RUN_LOG_FILE"
local local_flag=false
# Calculate parallels
local total_parallels
total_parallels=$(awk -v n="$max_parallels" 'BEGIN { printf "%d", n }')
[ -z "$total_parallels" ] && total_parallels=1
local parallels_per_platform=$total_parallels
log_msg_to "[${setup_type} Setup]" "$NOW_RUN_LOG_FILE"
log_msg_to "Total parallels allocated: $total_parallels" "$NOW_RUN_LOG_FILE"
case "$tech_stack" in
java)
"setup_${setup_type}_java" "$local_flag" "$parallels_per_platform" "$NOW_RUN_LOG_FILE"
log_section "✅ Results"
identify_run_status_java "$NOW_RUN_LOG_FILE"
check_return_value $? "$NOW_RUN_LOG_FILE" "${setup_type} setup succeeded." "❌ ${setup_type} setup failed. Check $log_file for details"
;;
python)
"setup_${setup_type}_python" "$local_flag" "$parallels_per_platform" "$NOW_RUN_LOG_FILE"
log_section "✅ Results"
identify_run_status_python "$NOW_RUN_LOG_FILE"
check_return_value $? "$NOW_RUN_LOG_FILE" "${setup_type} setup succeeded." "❌ ${setup_type} setup failed. Check $log_file for details"
;;
nodejs)
"setup_${setup_type}_nodejs" "$local_flag" "$parallels_per_platform" "$NOW_RUN_LOG_FILE"
log_section "✅ Results"
identify_run_status_nodejs "$NOW_RUN_LOG_FILE"
check_return_value $? "$NOW_RUN_LOG_FILE" "${setup_type} setup succeeded." "❌ ${setup_type} setup failed. Check $log_file for details"
;;
*)
log_warn "Unknown TECH_STACK: $tech_stack" "$NOW_RUN_LOG_FILE"
return 1
;;
esac
}
setup_web_java() {
local local_flag=$1
local parallels=$2
REPO="now-testng-browserstack"
TARGET_DIR="$WORKSPACE_DIR/$PROJECT_FOLDER/$REPO"
mkdir -p "$WORKSPACE_DIR/$PROJECT_FOLDER"
clone_repository "$REPO" "$TARGET_DIR"
cd "$TARGET_DIR"|| return 1
log_info "Target website: $CX_TEST_URL"
if is_domain_private; then
local_flag=true
fi
report_bstack_local_status "$local_flag"
# === 5️⃣ YAML Setup ===
log_msg_to "🧩 Generating YAML config (bstack.yml)"
# YAML config path
export BROWSERSTACK_CONFIG_FILE="./browserstack.yml"
platform_yaml=$(generate_web_platforms "$TEAM_PARALLELS_MAX_ALLOWED_WEB", "yaml")
cat >> "$BROWSERSTACK_CONFIG_FILE" <<EOF
platforms:
$platform_yaml
EOF
export BSTACK_PARALLELS=$parallels
export BSTACK_PLATFORMS=$platform_yaml
export BROWSERSTACK_LOCAL=$local_flag
export BROWSERSTACK_BUILD_NAME="now-$NOW_OS-web-java-testng"
export BROWSERSTACK_PROJECT_NAME="now-$NOW_OS-web"
# === 6️⃣ Build and Run ===
log_msg_to "⚙️ Running 'mvn install -DskipTests'"
log_info "Installing dependencies"
mvn install -DskipTests >> "$NOW_RUN_LOG_FILE" 2>&1 || return 1
log_success "Dependencies installed"
log_section "Validate Environment Variables"
log_info "BrowserStack Username: $BROWSERSTACK_USERNAME"
log_info "BrowserStack Build: $BROWSERSTACK_BUILD_NAME"
log_info "Web Application Endpoint: $CX_TEST_URL"
log_info "BrowserStack Local Flag: $BROWSERSTACK_LOCAL"
log_info "Parallels per platform: $BSTACK_PARALLELS"
log_info "Platforms: \n$BSTACK_PLATFORMS"
print_tests_running_log_section "mvn test -P sample-test"
log_msg_to "🚀 Running 'mvn test -P sample-test'. This could take a few minutes. Follow the Automaton build here: https://automation.browserstack.com/"
mvn test -P sample-test >> "$NOW_RUN_LOG_FILE" 2>&1 &
cmd_pid=$!|| return 1
show_spinner "$cmd_pid"
wait "$cmd_pid"
cd "$WORKSPACE_DIR/$PROJECT_FOLDER" || return 1
return 0
}
setup_app_java() {
local local_flag=$1
local parallels=$2
local log_file=$3
REPO="now-testng-appium-app-browserstack"
TARGET_DIR="$WORKSPACE_DIR/$PROJECT_FOLDER/$REPO"
local app_url=$BROWSERSTACK_APP
log_msg_to "APP_PLATFORM: $APP_PLATFORM" >> "$NOW_RUN_LOG_FILE" 2>&1
clone_repository "$REPO" "$TARGET_DIR"
if [[ "$APP_PLATFORM" == "all" || "$APP_PLATFORM" == "android" ]]; then
cd "android/testng-examples" || return 1
else
cd ios/testng-examples || return 1
fi
# YAML config path
export BROWSERSTACK_CONFIG_FILE="./browserstack.yml"
platform_yaml=$(generate_mobile_platforms "$TEAM_PARALLELS_MAX_ALLOWED_MOBILE" "yaml")
cat >> "$BROWSERSTACK_CONFIG_FILE" <<EOF
app: ${app_url}
platforms:
$platform_yaml
EOF
export BSTACK_PARALLELS=$parallels
export BROWSERSTACK_LOCAL=true
export BSTACK_PLATFORMS=$platform_yaml
export BROWSERSTACK_BUILD_NAME="now-$NOW_OS-app-java-testng"
export BROWSERSTACK_PROJECT_NAME="now-$NOW_OS-app"
# Run Maven install first
log_msg_to "⚙️ Running 'mvn clean'"
log_info "Installing dependencies"
if ! mvn clean >> "$NOW_RUN_LOG_FILE" 2>&1; then
log_msg_to "❌ 'mvn clean' FAILED. See $log_file for details."
return 1 # Fail the function if clean fails
fi
log_success "Dependencies installed"
log_section "Validate Environment Variables"
log_info "BrowserStack Username: $BROWSERSTACK_USERNAME"
log_info "BrowserStack Build: $BROWSERSTACK_BUILD_NAME"
log_info "Native App Endpoint: $BROWSERSTACK_APP"
log_info "BrowserStack Local Flag: $BROWSERSTACK_LOCAL"
log_info "Parallels per platform: $BSTACK_PARALLELS"
log_info "Platforms: \n$BSTACK_PLATFORMS"
log_msg_to "🚀 Running 'mvn test -P sample-test'. This could take a few minutes. Follow the Automaton build here: https://automation.browserstack.com/"
print_tests_running_log_section "mvn test -P sample-test"
mvn test -P sample-test >> "$NOW_RUN_LOG_FILE" 2>&1 &
cmd_pid=$!|| return 1
show_spinner "$cmd_pid"
wait "$cmd_pid"
cd "$WORKSPACE_DIR/$PROJECT_FOLDER" || return 1
return 0
}
setup_web_python() {
local local_flag=$1
local parallels=$2
local log_file=$3
REPO="now-pytest-browserstack"
TARGET_DIR="$WORKSPACE_DIR/$PROJECT_FOLDER/$REPO"
clone_repository "$REPO" "$TARGET_DIR" ""
# detect_setup_python_env
pip3 install --only-binary grpcio -r requirements.txt >> "$NOW_RUN_LOG_FILE" 2>&1
pip3 uninstall -y pytest-html pytest-rerunfailures >> "$NOW_RUN_LOG_FILE" 2>&1
log_success "Dependencies installed"
# Update YAML at root level (browserstack.yml)
export BROWSERSTACK_CONFIG_FILE="./browserstack.yml"
platform_yaml=$(generate_web_platforms "$TEAM_PARALLELS_MAX_ALLOWED_WEB" "yaml")
export BSTACK_PLATFORMS=$platform_yaml
cat >> "$BROWSERSTACK_CONFIG_FILE" <<EOF
platforms:
$platform_yaml
EOF
if is_domain_private; then
local_flag=true
else
local_flag=false
fi
export BSTACK_PARALLELS=1
export BROWSERSTACK_LOCAL=$local_flag
export BSTACK_PLATFORMS=$platform_yaml
export BROWSERSTACK_BUILD_NAME="now-$NOW_OS-web-python-pytest"
export BROWSERSTACK_PROJECT_NAME="now-$NOW_OS-web"
report_bstack_local_status "$local_flag"
log_section "Validate Environment Variables"
log_info "BrowserStack Username: $BROWSERSTACK_USERNAME"
log_info "BrowserStack Build: $BROWSERSTACK_BUILD_NAME"
log_info "Web Application Endpoint: $CX_TEST_URL"
log_info "BrowserStack Local Flag: $BROWSERSTACK_LOCAL"
log_info "Parallels per platform: $BSTACK_PARALLELS"
log_info "Platforms: \n$BSTACK_PLATFORMS"
print_tests_running_log_section "browserstack-sdk pytest -s tests/*.py"
log_msg_to "🚀 Running 'browserstack-sdk pytest -s tests/*.py'. This could take a few minutes. Follow the Automaton build here: https://automation.browserstack.com/"
# Run tests
browserstack-sdk pytest -s tests/*.py >> "$NOW_RUN_LOG_FILE" 2>&1 & cmd_pid=$!|| return 1
show_spinner "$cmd_pid"
wait "$cmd_pid"
cd "$WORKSPACE_DIR/$PROJECT_FOLDER" || return 1
return 0
}
setup_app_python() {
local local_flag=$1
local parallels=$2
local log_file=$3
REPO="now-pytest-appium-app-browserstack"
TARGET_DIR="$WORKSPACE_DIR/$PROJECT_FOLDER/$REPO"
clone_repository "$REPO" "$TARGET_DIR"
# detect_setup_python_env
# Install dependencies
pip install --only-binary grpcio -r requirements.txt >> "$NOW_RUN_LOG_FILE" 2>&1
log_success "Dependencies installed"
local app_url=$BROWSERSTACK_APP
local platform_yaml
export BSTACK_PARALLELS=1
# Decide which directory to run based on APP_PLATFORM (default to android)
local run_dir="android"
if [ "$APP_PLATFORM" = "ios" ]; then
run_dir="ios"
fi
export BROWSERSTACK_CONFIG_FILE="./$run_dir/browserstack.yml"
platform_yaml=$(generate_mobile_platforms "$TEAM_PARALLELS_MAX_ALLOWED_MOBILE" "yaml")
cat >> "$BROWSERSTACK_CONFIG_FILE" <<EOF
platforms:
$platform_yaml
EOF
export BSTACK_PLATFORMS=$platform_yaml
export BROWSERSTACK_LOCAL=true
export BROWSERSTACK_BUILD_NAME="now-$NOW_OS-app-python-pytest"
export BROWSERSTACK_PROJECT_NAME="now-$NOW_OS-app"
log_section "Validate Environment Variables"
log_info "BrowserStack Username: $BROWSERSTACK_USERNAME"
log_info "BrowserStack Build: $BROWSERSTACK_BUILD_NAME"
log_info "Native App Endpoint: $BROWSERSTACK_APP"
log_info "BrowserStack Local Flag: $BROWSERSTACK_LOCAL"
log_info "Parallels per platform: $BSTACK_PARALLELS"
log_info "Platforms: \n$BSTACK_PLATFORMS"
print_tests_running_log_section "cd $run_dir && browserstack-sdk pytest -s bstack-sample.py"
# Run pytest with BrowserStack SDK from the chosen platform directory
log_msg_to "🚀 Running 'cd $run_dir && browserstack-sdk pytest -s bstack_sample.py'"
(
cd "$run_dir" && browserstack-sdk pytest -s bstack_sample.py >> "$NOW_RUN_LOG_FILE" 2>&1 || return 1 & cmd_pid=$!|| return 1
show_spinner "$cmd_pid"
wait "$cmd_pid"
)
deactivate
cd "$WORKSPACE_DIR/$PROJECT_FOLDER" || return 1
return 0
}
setup_web_nodejs() {
local local_flag=$1
local parallels=$2
REPO="now-webdriverio-browserstack"
TARGET_DIR="$WORKSPACE_DIR/$PROJECT_FOLDER/$REPO"
mkdir -p "$WORKSPACE_DIR/$PROJECT_FOLDER"
clone_repository "$REPO" "$TARGET_DIR"
# === 2️⃣ Install Dependencies ===
log_msg_to "⚙️ Running 'npm install'"
log_info "Installing dependencies"
npm install >> "$NOW_RUN_LOG_FILE" 2>&1 || return 1
log_success "Dependencies installed"
local caps_json=""
# === 4️⃣ Generate Capabilities JSON ===
caps_json=$(generate_web_platforms "$parallels" "json")
export BSTACK_CAPS_JSON=$caps_json
export BSTACK_PARALLELS=$parallels
if is_domain_private; then
local_flag=true
fi
export BROWSERSTACK_LOCAL=$local_flag
export BROWSERSTACK_BUILD_NAME="now-$NOW_OS-web-nodejs-wdio"
export BROWSERSTACK_PROJECT_NAME="now-$NOW_OS-web"
report_bstack_local_status "$local_flag"
log_section "Validate Environment Variables"
log_info "BrowserStack Username: $BROWSERSTACK_USERNAME"
log_info "BrowserStack Build: $BROWSERSTACK_BUILD_NAME"
log_info "Web Application Endpoint: $CX_TEST_URL"
log_info "BrowserStack Local Flag: $BROWSERSTACK_LOCAL"
log_info "Parallels per platform: $BSTACK_PARALLELS"
log_info "Platforms: \n$BSTACK_CAPS_JSON"
# === 8️⃣ Run Tests ===
log_msg_to "🚀 Running 'npm run test'. This could take a few minutes. Follow the Automaton build here: https://automation.browserstack.com/"
print_tests_running_log_section "npm run test"
npm run test >> "$NOW_RUN_LOG_FILE" 2>&1 || return 1 &
cmd_pid=$!|| return 1
show_spinner "$cmd_pid"
wait "$cmd_pid"
cd "$WORKSPACE_DIR/$PROJECT_FOLDER" || return 1
return 0
}
setup_app_nodejs() {
local local_flag=$1
local parallels=$2
local log_file=$3
local caps_json=""
log_msg_to "Starting Mobile NodeJS setup with parallels: $parallels" >> "$NOW_RUN_LOG_FILE" 2>&1
mkdir -p "$WORKSPACE_DIR/$PROJECT_FOLDER"
REPO="now-webdriverio-appium-app-browserstack"
TARGET_DIR="$WORKSPACE_DIR/$PROJECT_FOLDER/$REPO"
TEST_FOLDER="/test"
clone_repository $REPO "$TARGET_DIR" "$TEST_FOLDER"
# === 2️⃣ Install Dependencies ===
log_info "Installing dependencies"
log_msg_to "⚙️ Running 'npm install'"
npm install >> "$NOW_RUN_LOG_FILE" 2>&1 || return 1
log_success "Dependencies installed"
caps_json=$(generate_mobile_platforms "$TEAM_PARALLELS_MAX_ALLOWED_MOBILE" "json")
export BSTACK_CAPS_JSON=$caps_json
local app_url=$BROWSERSTACK_APP
export BSTACK_PARALLELS=$parallels
export BROWSERSTACK_LOCAL=true
export BROWSERSTACK_APP=$app_url
export BROWSERSTACK_BUILD_NAME="now-$NOW_OS-app-nodejs-wdio"
export BROWSERSTACK_PROJECT_NAME="now-$NOW_OS-app"
log_section "Validate Environment Variables"
log_info "BrowserStack Username: $BROWSERSTACK_USERNAME"
log_info "BrowserStack Build: $BROWSERSTACK_BUILD_NAME"
log_info "Native App Endpoint: $BROWSERSTACK_APP"
log_info "BrowserStack Local Flag: $BROWSERSTACK_LOCAL"
log_info "Parallels per platform: $BSTACK_PARALLELS"
log_info "Platforms: \n$BSTACK_CAPS_JSON"
# === 8️⃣ Run Tests ===
log_msg_to "🚀 Running 'npm run test'. This could take a few minutes. Follow the Automaton build here: https://automation.browserstack.com/"
print_tests_running_log_section "npm run test"
npm run test >> "$NOW_RUN_LOG_FILE" 2>&1 || return 1 &
cmd_pid=$!|| return 1
show_spinner "$cmd_pid"
wait "$cmd_pid"
# === 9️⃣ Wrap Up ===
log_msg_to "✅ Mobile JS setup and test execution completed successfully."
cd "$WORKSPACE_DIR/$PROJECT_FOLDER" || return 1
return 0
}
clone_repository() {
local repo_git=$1
local install_folder=$2
local test_folder=$3
local git_branch=$4
rm -rf "$install_folder"
log_msg_to "📦 Cloning repo $repo_git into $install_folder"
log_info "Cloning repository: $repo_git"
# git clone https://github.com/BrowserStackCE/"$repo_git".git "$install_folder" >> "$NOW_RUN_LOG_FILE" 2>&1 || return 1
if [ -z "$git_branch" ]; then
# git_branch is null or empty
git clone "https://github.com/BrowserStackCE/$repo_git.git" \
"$install_folder" >> "$NOW_RUN_LOG_FILE" 2>&1 || return 1
else
# git_branch has a value
git clone -b "$git_branch" "https://github.com/BrowserStackCE/$repo_git.git" \
"$install_folder" >> "$NOW_RUN_LOG_FILE" 2>&1 || return 1
fi
log_msg_to "✅ Cloned repository: $repo_git into $install_folder"
cd "$install_folder/$test_folder" || return 1
}
# ===== Orchestration: decide what to run based on TEST_TYPE and plan fetch =====
run_setup_wrapper() {
local test_type=$1
local tech_stack=$2
log_msg_to "Orchestration: TEST_TYPE=$test_type, WEB_PLAN_FETCHED=$WEB_PLAN_FETCHED, MOBILE_PLAN_FETCHED=$MOBILE_PLAN_FETCHED"
case "$test_type" in
Web)
if [ "$WEB_PLAN_FETCHED" == true ]; then
run_setup "$test_type" "$tech_stack"
else
log_msg_to "⚠️ Skipping Web setup — Web plan not fetched"
fi
;;
App)
if [ "$MOBILE_PLAN_FETCHED" == true ]; then
run_setup "$test_type" "$tech_stack"
else
log_msg_to "⚠️ Skipping Mobile setup — Mobile plan not fetched"
fi
;;
*)
log_msg_to "❌ Invalid TEST_TYPE: $test_type"
exit 1
;;
esac
}
check_return_value() {
local return_value=$1
local log_file=$2
local success_message=$3
local failure_message=$4
if [ "$return_value" -eq 0 ]; then
log_success "$success_message" "$NOW_RUN_LOG_FILE"
exit 0
else
log_error "$failure_message" "$NOW_RUN_LOG_FILE"
exit 1
fi
}
report_bstack_local_status() {
if [ "$local_flag" = "true" ]; then
log_msg_to "✅ BrowserStack Local is ENABLED for this run."
log_success "Target website is behind firewall. BrowserStack Local enabled for this run."
else
log_msg_to "✅ BrowserStack Local is DISABLED for this run."
log_success "Target website is publicly resolvable. BrowserStack Local disabled for this run."
fi
}
print_tests_running_log_section() {
log_section "🚀 Running Tests: $1"
log_info "Executing: Test run command. This could take a few minutes..."
log_info "You can monitor test progress here: 🔗 https://automation.browserstack.com/"
}
detect_setup_python_env() {
log_info "Detecting latest Python environment"
latest_python=$(
{ ls -1 /usr/local/bin/python3.[0-9]* /usr/bin/python3.[0-9]* 2>/dev/null || true; } \
| grep -E 'python3\.[0-9]+$' \
| sort -V \
| tail -n 1
)
if [[ -z "$latest_python" ]]; then
log_warn "No specific Python3.x version found. Falling back to system python3."
latest_python=$(command -v python3)
fi
if [[ -z "$latest_python" ]]; then
log_error "Python3 not found on this system."
exit 1
fi
echo "🐍 Switching to: $latest_python"
log_info "Using Python interpreter: $latest_python"
"$latest_python" -m venv .venv || {
log_error "Failed to create virtual environment."
exit 1
}
# Activate virtual environment (handle Windows/Unix paths)
if [ -f ".venv/Scripts/activate" ]; then
# shellcheck source=/dev/null
source .venv/Scripts/activate
else
# shellcheck source=/dev/null
source .venv/bin/activate
fi
log_success "Virtual environment created and activated."
}