Skip to content

Commit

Permalink
fix and bring parity with bs_android
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Nov 28, 2023
1 parent 1a57e48 commit e7a6a99
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions bin/bs_ios
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ set -euo pipefail
# This version of the script works only with the V2 endpoint (XC Test Plans).
# This script assumes that Xcode Test Plan name is TestPlan.xctestplan.
#
# It forwards all arguments to "patrol build android", so you can pass --target,
# It forwards all arguments to "patrol build ios", so you can pass --target,
# --flavor, --exclude etc. just as you would pass them to "patrol_cli".
#
# It can also be configured with the following environment variables:
BROWSERSTACK_CREDS="${BROWSERSTACK_CREDS:-}"
BROWSERSTACK_PROJECT="${BROWSERSTACK_PROJECT:-}"
BROWSERSTACK_ANDROID_DEVICES="${BROWSERSTACK_ANDROID_DEVICES:-}"
BROWSERSTACK_SKIP_BUILD="${BROWSERSTACK_SKIP_BUILD:-}"
BROWSERSTACK_IDLE_TIMEOUT="${BROWSERSTACK_IDLE_TIMEOUT:-}"
BROWSERSTACK_IOS_DEVICES="${BROWSERSTACK_IOS_DEVICES:-}"
BROWSERSTACK_SKIP_BUILD="${BROWSERSTACK_SKIP_BUILD:-0}"
BROWSERSTACK_IDLE_TIMEOUT="${BROWSERSTACK_IDLE_TIMEOUT:-900}"

# Capture all arguments because they'll be consumed by getopt
# shellcheck disable=SC2206
Expand Down Expand Up @@ -43,17 +43,29 @@ fi
# Arg parsing end

if [ -z "${BROWSERSTACK_CREDS:-}" ]; then
echo "Error: BROWSERSTACK_CREDS not set" >&2
echo "Error: BROWSERSTACK_CREDS not set"
exit 1
fi

if [ -z "$BROWSERSTACK_PROJECT" ]; then
default_project="Unnamed iOS project"
echo "BROWSERSTACK_PROJECT not set, using default: $default_project"
BROWSERSTACK_PROJECT="$default_project"
fi

if [ -z "$BROWSERSTACK_IOS_DEVICES" ]; then
default_devices="[\"iPhone 14-16\"]"
echo "BROWSERSTACK_IOS_DEVICES not set, using default: $default_devices"
BROWSERSTACK_IOS_DEVICES="$default_devices"
fi

if [ "$BROWSERSTACK_SKIP_BUILD" = 1 ]; then
echo "BROWSERSTACK_SKIP_BUILD set to 1, build was skipped"
else
patrol build ios --release "${original_args[@]}"
fi

printf "Will create zip archive of test files\n"
echo "Will create zip archive of test files"

cd build/ios_integ/Build/Products

Expand All @@ -67,36 +79,48 @@ cd "build/ios_integ/Build/Products/Release${FLAVOR_SUFFIXED}iphoneos"
rm -rf ios_tests.zip

# BrowserStack fails if DiagnosticCollectionPolicy is present
plutil -remove 'TestConfigurations.TestTargets.DiagnosticCollectionPolicy' ../"${FLAVOR_PREFIXED}"_Patrol_iphoneos*.xctestrun
plutil -remove 'TestConfigurations.TestTargets.DiagnosticCollectionPolicy' ../"${FLAVOR_PREFIXED}"_TestPlan_iphoneos*.xctestrun

cp ../"${FLAVOR_PREFIXED}"_Patrol_iphoneos*.xctestrun .
zip -r ios_tests.zip "${FLAVOR_PREFIXED}"_Patrol_iphoneos*.xctestrun RunnerUITests-Runner.app >/dev/null
cp ../"${FLAVOR_PREFIXED}"_TestPlan_iphoneos*.xctestrun .
zip -r ios_tests.zip "${FLAVOR_PREFIXED}"_TestPlan_iphoneos*.xctestrun RunnerUITests-Runner.app >/dev/null
cd - >/dev/null
printf "Completed zipping\n"

printf "\nUploading app...\n"
echo "Created zip archive"

app_path="$PWD/build/ios_integ/Build/Products/Runner.ipa"
if [ ! -f "$app_path" ]; then
echo "Error: app under test not found at $app_path"
exit 1
fi

# https://www.browserstack.com/docs/app-automate/api-reference/xcuitest/apps#upload-an-app
app_url="$(
printf "Will upload app under test from %s\n\n" "$app_path"
app_upload_response="$(
curl -u "$BROWSERSTACK_CREDS" \
-X POST "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/app" \
-F "file=@$PWD/build/ios_integ/Build/Products/Runner.ipa" |
jq --raw-output .app_url
-F "file=@$app_path"
)"

echo "Uploaded app, url: $app_url"
app_url="$(echo "$app_upload_response" | jq --raw-output .app_url)"
printf "\nUploaded app under test, url: %s\n" "$app_url"

test_path="$PWD/build/ios_integ/Build/Products/Release${FLAVOR_SUFFIXED}iphoneos/ios_tests.zip"
if [ ! -f "$test_path" ]; then
echo "Error: zip archive of test suite not found at $test_path"
exit 1
fi

printf "\nUploading test...\n"

# https://www.browserstack.com/docs/app-automate/api-reference/xcuitest/tests#upload-a-test-suite
test_url="$(
printf "Will upload zip archive of test suite from %s\n\n" "$test_path"
test_upload_response="$(
curl -u "$BROWSERSTACK_CREDS" \
-X POST "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/test-suite" \
-F "file=@$PWD/build/ios_integ/Build/Products/Release${FLAVOR_SUFFIXED}iphoneos/ios_tests.zip" |
jq --raw-output .test_suite_url
-F "file=@$test_path"
)"

echo "Uploaded test instrumentation app, url: $test_url"
test_url="$(echo "$test_upload_response" | jq --raw-output .test_suite_url)"
echo "\nUploaded zip archive of test suite, url: %s\n" "$test_url"

# https://www.browserstack.com/docs/app-automate/api-reference/xcuitest/builds#execute-a-build
printf "Will schedule test execution\n\n"
Expand All @@ -115,4 +139,4 @@ curl -u "$BROWSERSTACK_CREDS" \
}
EOF

printf "\n\nScheduled test execution"
printf "\n\nScheduled test execution\n"

0 comments on commit e7a6a99

Please sign in to comment.