Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Android Java E2E tests from App Center to Browserstack #22117

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ jobs:

- template: use-android-ndk.yml

- template: install-appcenter.yml

- template: use-android-emulator.yml
parameters:
create: true
Expand All @@ -70,17 +68,16 @@ jobs:

- script: |
set -e -x
cd android_test/android
appcenter test run espresso \
--app "AI-Frameworks/ORT-Mobile-Android" \
--devices $(app_center_android_test_devices) \
--app-path ./app/build/outputs/apk/debug/app-debug.apk \
--test-series "master" \
--locale "en_US" \
--build-dir ./app/build/outputs/apk/androidTest/debug \
--token $(app_center_api_token)
displayName: Run E2E tests using App Center
workingDirectory: $(Build.BinariesDirectory)
cd tools/python
pip install requests
python upload_and_run_browserstack_tests.py \
--id $(browserstack_username) \
--token $(browserstack_access_key) \
Comment on lines +75 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there WIP documentation somewhere that provides the high level details of how this all works and where these values are defined so that we know how to update them in the future?

--app_apk_path "$(Build.BinariesDirectory)/android_test/android/app/build/outputs/apk/debug/app-debug.apk" \
--test_apk_path "$(Build.BinariesDirectory)/android_test/android/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk" \
--devices "Samsung Galaxy S23-13.0" "Google Pixel 3-9.0"
displayName: Run E2E tests using Browserstack
workingDirectory: $(Build.SourcesDirectory)

- template: component-governance-component-detection-steps.yml
parameters :
Expand Down
88 changes: 88 additions & 0 deletions tools/python/upload_and_run_browserstack_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import argparse
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
import json
import time

import requests

parser = argparse.ArgumentParser("Upload and run BrowserStack tests")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we usually want to put the script logic to execute inside the check if __name__ == '__main__'. then, it only gets executed when the script is called directly and not in other contexts, like when the script is imported by something else.

more details here: https://docs.python.org/3/library/__main__.html


parser.add_argument("--id", type=str, help="BrowserStack user ID")
parser.add_argument("--token", type=str, help="BrowserStack token")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this value secret?

passing it on the command line might be fine in the context of a CI pipeline where it is redacted from logs, but command line arguments are generally not hidden. e.g., when running locally, the command line arguments will be visible to other processes and also in the command history.

some alternatives are to pass the secret value via stdin or in an environment variable.

if we don't expect to run this script much locally, this is less of a concern.

parser.add_argument("--test_platform", type=str, help="Testing platform, i.e., XCUITest or Espresso")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there are two valid options, can we just constrain them here to catch typos earlier and with an obvious error message?
e.g., choices=['espresso', ...]

parser.add_argument("--app_apk_path", type=str, help="Path to the app APK")
parser.add_argument("--test_apk_path", type=str, help="Path to the test suite APK")
parser.add_argument("--devices", type=str, nargs="+", help="List of devices to run the tests on. For more info, " +
Fixed Show fixed Hide fixed
"see https://www.browserstack.com/docs/app-automate/espresso/specify-devices")

args = parser.parse_args()

def post_response_to_json(response):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the name is post_reponse_to_json but this function is also called with a GET response. maybe response_to_json would be a more general name.

if len(response) == 0:
raise Exception("No response from BrowserStack")
try:
json_response = json.loads(response)
print("JSON response: ", json.dumps(json_response, indent=2))
if "error" in json_response:
raise Exception(json_response["error"])
Comment on lines +31 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there specific json properties we can check for 'error' instead of matching across the entire response?

return json_response
except Exception as e:
print("response received: ", response)
raise Exception("Invalid JSON response from BrowserStack") from e


def upload_apk_parse_json(post_url, apk_path):
with open(apk_path, "rb") as apk_file:
response = requests.post(post_url, files={"file": apk_file}, auth=(args.id, args.token))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we configure specific timeouts based on the operation? I can imagine apk upload may take longer than what the default timeout (assuming there is one) allows for.

return post_response_to_json(response.text)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also check response.status_code? e.g., this method might be useful:
https://docs.python-requests.org/en/latest/api/#requests.Response.raise_for_status

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's also a response method for parsing json which may simplify things:
https://docs.python-requests.org/en/latest/api/#requests.Response.json



upload_app_json = upload_apk_parse_json(
"https://api-cloud.browserstack.com/app-automate/{test_platform}/v2/app".format(test_platform = args.test_platform), args.app_apk_path
Fixed Show fixed Hide fixed
)
upload_test_json = upload_apk_parse_json(
"https://api-cloud.browserstack.com/app-automate/{test_platform}/v2/test-suite".format(test_platform = args.test_platform), args.test_apk_path
Fixed Show fixed Hide fixed
)

headers = {}

json_data = {
"devices": args.devices,
"app": upload_app_json["app_url"],
"testSuite": upload_test_json["test_suite_url"],
}

build_response = requests.post(
"https://api-cloud.browserstack.com/app-automate/" + args.test_platform + "/v2/build",
headers=headers,
json=json_data,
auth=(args.id, args.token),
)

build_response_json = post_response_to_json(build_response.text)

# GET TEST RESULTS

tests_status = "running"

while tests_status == "running":
time.sleep(30)
Comment on lines +73 to +74
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have an overall timeout for this loop?

test_response = requests.get(
"https://api-cloud.browserstack.com/app-automate/{test_platform}/v2/builds/{build_id}".format(
test_platform = args.test_platform,
build_id=build_response_json["build_id"]
),
auth=(args.id, args.token),
)

test_response_json = post_response_to_json(test_response.text)

tests_status = test_response_json["status"]

print("=" * 30)
print(
"Test suite details: ",
"https://app-automate.browserstack.com/dashboard/v2/builds/" + build_response_json["build_id"],
)
print("=" * 30)
if tests_status != "passed":
raise Exception("Tests failed. Go to the link above for more details.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: include the link directly as there could (in theory) be other output when the CI runs that makes 'above' unclear.

Loading