Skip to content

Commit

Permalink
Merge pull request #2487 from dimagi/browserstack
Browse files Browse the repository at this point in the history
[SAAS-12293] Fix No Test Run error in BS
  • Loading branch information
ShivamPokhriyal committed Jun 3, 2021
2 parents 7381b31 + 44b4a82 commit 24ff7b8
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions scripts/browserstack.py
Expand Up @@ -32,7 +32,13 @@ def buildTestCommand(appToken, testToken, classes=None):
if classes:
test["class"] = classes
classSize = len(classes)
test["shards"] = { "numberOfShards": 5 if (classSize > 5) else classSize }
if classSize > 5:
test["shards"] = { "numberOfShards": 5 }
else:
mapping = []
for index, name in enumerate(classes, start=1):
mapping.append({"name" : "Shard " + str(index), "strategy": "class", "values": [name]})
test["shards"] = { "numberOfShards": classSize, "mapping": mapping }
else:
test["shards"] = { "numberOfShards": 5 }

Expand All @@ -52,14 +58,15 @@ def isSuccessfulBuild(buildId):
return status


def testResult(buildId):
def testResult(appToken, testToken, buildId, retryCount):
status = isSuccessfulBuild(buildId)

# if test succeeded then we can simply return from here.
if (status == "passed"):
return

# Otherwise run the failing test one more time.
retryCount = retryCount + 1

# Get the sessionID from test result
resultCommand = 'curl -u "{}:{}" -X GET "https://api-cloud.browserstack.com/app-automate/espresso/v2/builds/{}"'.format(userName, password, buildId)
Expand Down Expand Up @@ -100,40 +107,32 @@ def testResult(buildId):

status = isSuccessfulBuild(buildId)

if (status != "passed"):
if (status != "passed" and retryCount >= 3):
print("Instrumentation Tests Failed. Visit browserstack dashboard for more details.")
print("https://app-automate.browserstack.com/dashboard/v2/builds/{}".format(buildId))
sys.exit(-1)
elif status != "passed":
testResult(appToken, testToken, buildId, retryCount)
else:
print("Instrumentation Tests Passed.")

def shouldSkipAndroidTest():
gitPRId = os.environ["ghprbPullId"]
gitPRCmd = 'curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/dimagi/commcare-android/pulls/{}'.format(gitPRId)
gitPRCmdOutput = subprocess.Popen(shlex.split(gitPRCmd), stdout=PIPE, stderr=None, shell=False).communicate()
gitPRLabels = json.loads(gitPRCmdOutput[0])["labels"]
shouldSkip = False
for label in gitPRLabels:
for label in gitPRLabels:
if label["name"] == "skip-integration-tests":
shouldSkip = True
break
return shouldSkip

def runAndroidTest():
def runAndroidTest():

# Exit if the PR is labelled with `skip-integration-tests`
if shouldSkipAndroidTest():
return

if "BROWSERSTACK_USERNAME" in os.environ:
userName = os.environ["BROWSERSTACK_USERNAME"]

if "BROWSERSTACK_PASSWORD" in os.environ:
password = os.environ["BROWSERSTACK_PASSWORD"]

releaseApp = os.environ["RELEASE_APP_LOCATION"]
testApk = os.environ["TEST_APP_LOCATION"]

releaseUrl = "https://api-cloud.browserstack.com/app-automate/upload"
testUrl = "https://api-cloud.browserstack.com/app-automate/espresso/test-suite"
return

command = 'curl -u "{}:{}" -X POST "{}" -F'

Expand All @@ -147,7 +146,6 @@ def runAndroidTest():

# Running the tests

espressoUrl = "https://api-cloud.browserstack.com/app-automate/espresso/build"
runConfig = buildTestCommand(appToken, testToken)
runCmd = 'curl -X POST "{}" -d \ {} -H "Content-Type: application/json" -u "{}:{}"'.format(espressoUrl, runConfig, userName, password)

Expand All @@ -157,8 +155,17 @@ def runAndroidTest():
buildId = json.loads(output[0])["build_id"]

# Get the result of the test build
testResult(buildId)
testResult(appToken, testToken, buildId, 1)


if __name__ == "__main__":
userName = os.environ["BROWSERSTACK_USERNAME"]
password = os.environ["BROWSERSTACK_PASSWORD"]
releaseApp = os.environ["RELEASE_APP_LOCATION"]
testApk = os.environ["TEST_APP_LOCATION"]

releaseUrl = "https://api-cloud.browserstack.com/app-automate/upload"
testUrl = "https://api-cloud.browserstack.com/app-automate/espresso/test-suite"
espressoUrl = "https://api-cloud.browserstack.com/app-automate/espresso/build"

runAndroidTest()

0 comments on commit 24ff7b8

Please sign in to comment.