Skip to content

Commit

Permalink
Add bs_android script (#44)
Browse files Browse the repository at this point in the history
* copy over the `bs` script (renamed to `bs_android`)

* add comments with links to API docs

* handle how `flavor` impact path, and forward all args to `patrol build`

* forward arguments

* forward BROWSERSTACK_PROJECT and BROWSERSTACK_DEVICES
  • Loading branch information
bartekpacia committed Jul 13, 2023
1 parent bee30b0 commit 08d3984
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions bin/bs_android
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env bash
set -euo pipefail

# bs_android uploads app binaries for UI testing on BrowserStack.
#
# It forwards all arguments to `patrol build android`, so you can pass --target,
# --flavor, --exclude etc.
#
# It can also be configured with the following environment variables:

BROWSERSTACK_CREDS="${BROWSERSTACK_CREDS:-}"
BROWSERSTACK_PROJECT="${BROWSERSTACK_PROJECT:-}"
BROWSERSTACK_DEVICES="${BROWSERSTACK_DEVICES:-}"

# Capture all arguments because they'll be consumed by getopt
# shellcheck disable=SC2206
original_args=($@)

# Arg parsing start

FLAVOR=""
FLAVOR_SUFFIXED="-"
FLAVOR_DIR="/"

TEMP=$(getopt -n "$0" -a -l "flavor:" -- -- "$@")
eval set -- "$TEMP"
while [ $# -gt 0 ]; do
case "$1" in
--flavor) FLAVOR="$2"; shift;;
--) shift;;
esac
shift
done

if [ -n "$FLAVOR" ]; then
echo "Passed flavor: $FLAVOR"
FLAVOR_SUFFIXED="-$FLAVOR-"
FLAVOR_DIR="/$FLAVOR/"
fi

# Arg parsing end

if [[ "$(patrol --version)" != *"v2"* ]]; then
echo "Error: patrol_cli v2 is required"
exit 1
fi

if [ -z "${BROWSERSTACK_CREDS:-}" ]; then
echo "Error: missing BROWSERSTACK_CREDS env var"
exit 1
fi

if [ -z "${BROWSERSTACK_PROJECT:-}" ]; then
default_project="Unnamed project"
echo "Warning: missing BROWSERSTACK_PROJECT env var, falling back to default: $default_project"
BROWSERSTACK_PROJECT="Unnamed project"
fi

if [ -z "${BROWSERSTACK_DEVICES:-}" ]; then
default="[\"Google Pixel 4-10.0\"]"
echo "Warning: missing BROWSERSTACK_DEVICES env var, falling back to default: $default"
BROWSERSTACK_DEVICES="$default"
fi

patrol build android "${original_args[@]}"

# https://www.browserstack.com/docs/app-automate/api-reference/espresso/apps#upload-an-app
app_upload_response="$(
curl -u "$BROWSERSTACK_CREDS" \
-X POST "https://api-cloud.browserstack.com/app-automate/espresso/v2/app" \
-F "file=@$PWD/build/app/outputs/apk${FLAVOR_DIR}debug/app${FLAVOR_SUFFIXED}debug.apk"
)"

app_url="$(echo "$app_upload_response" | jq --raw-output .app_url)"
echo "Uploaded app, url: $app_url"

# https://www.browserstack.com/docs/app-automate/api-reference/espresso/tests#upload-a-test-suite
test_upload_response="$(
curl --silent -u "$BROWSERSTACK_CREDS" \
-X POST "https://api-cloud.browserstack.com/app-automate/espresso/v2/test-suite" \
-F "file=@$PWD/build/app/outputs/apk/androidTest${FLAVOR_DIR}debug/app${FLAVOR_SUFFIXED}debug-androidTest.apk"
)"

test_url="$(echo "$test_upload_response" | jq --raw-output .test_suite_url)"
echo "Uploaded test, url: $test_url"

# https://www.browserstack.com/docs/app-automate/api-reference/espresso/builds#execute-a-build
curl -u "$BROWSERSTACK_CREDS" \
-X POST "https://api-cloud.browserstack.com/app-automate/espresso/v2/build" \
-H "Content-Type: application/json" \
--data-binary @- <<EOF
{
"app": "$app_url",
"testSuite": "$test_url",
"project": $BROWSERSTACK_PROJECT,
"devices": $BROWSERSTACK_DEVICES,
"singleRunnerInvocation": "true",
"useOrchestrator": "true",
"clearPackageData": "true",
"deviceLogs": "true",
"local": "false"
}
EOF

printf "\nScheduled test execution"

0 comments on commit 08d3984

Please sign in to comment.