diff --git a/app/BrowserStackE2EConfig.json b/app/BrowserStackE2EConfig.json new file mode 100644 index 0000000000..4ab2160dfd --- /dev/null +++ b/app/BrowserStackE2EConfig.json @@ -0,0 +1,8 @@ +{ + "devices": ["Google Pixel-8.0"], + "app": "APPURL", + "deviceLogs": true, + "networkLogs": true, + "testSuite": "TESTURL", + "annotation": ["com.naviapp.E2ETest"] +} \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 2bb577e4d1..6bbc2feb0f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,3 +1,6 @@ +import groovy.json.JsonSlurper +import groovy.json.JsonBuilder + plugins { id('com.android.application') id('kotlin-android') @@ -6,7 +9,6 @@ plugins { id('maven') id('com.google.firebase.crashlytics') id('com.google.gms.google-services') - id("com.browserstack.gradle") } def VERSION_NAME = "1.0.8" @@ -272,12 +274,78 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.71" } -browserStackConfig { - username = "sudarshangs1" - accessKey = "oaYqM81qEjMTwpAaipiQ" - devices = ['Google Pixel 3-9.0'] - annotations = ["${annotation}"] - allowDeviceMockServer = "true" +task executeTestsOnBrowserStack { + doLast { + def jsonParser = new JsonSlurper() + + // Upload App + def appUrl = new ByteArrayOutputStream().withStream { outputStream -> + exec { + executable "/bin/sh" args "-c", "curl -u ${bsAuth} -X POST ${bsAppUpload} -F \"file=@${devDebugApk}\" " + standardOutput = outputStream + } + + jsonParser.parseText(outputStream.toString()).app_url + } + + // Upload Test-Suite + def testUrl = new ByteArrayOutputStream().withStream { outputStream -> + exec { + executable "/bin/sh" args "-c", "curl -u ${bsAuth} -X POST ${bsTestUpload} -F \"file=@${devTestApk}\" " + standardOutput = outputStream + } + + jsonParser.parseText(outputStream.toString()).test_url + } + + println("AppUrl: " + appUrl) + println("TestUrl: " + testUrl) + + def config = jsonParser.parseText(file(bsE2EConfig).text) + config.app = appUrl + config.testSuite = testUrl + + def buildId = new ByteArrayOutputStream().withStream { outputStream -> + def configString = new JsonBuilder(config).toPrettyString().replace("\"", "\\\"") + def executionUrl = "curl -X POST ${bsExecute} -d \\ \"${configString}\" -H \"Content-Type: application/json\" -u ${bsAuth}" + + exec { + executable "/bin/sh" args "-c", "${executionUrl} " + standardOutput = outputStream + } + + jsonParser.parseText(outputStream.toString()).build_id + } + + println("Build-ID: " + buildId) + + // Poll For Test Results + def streamOutput = new ByteArrayOutputStream() + def status = "" + def pollingTimeout = System.currentTimeMillis() + (180 * 1000) + + while (status != "done" && System.currentTimeMillis() <= pollingTimeout) { + exec { + executable "/bin/sh" args "-c", "curl -u ${bsAuth} -X GET \"${bsBuilds}${buildId}\"" + standardOutput = streamOutput + } + + status = jsonParser.parseText(streamOutput.toString()).status + println("Status: " + status) + if (status != "done") { + sleep(60000) + streamOutput = new ByteArrayOutputStream() + } + } + + def buildStatus = jsonParser.parseText(streamOutput.toString()) + def device = config.devices[0] + println(buildStatus.devices["${device}"].test_status) + + if(buildStatus.devices["${device}"].test_status.FAILED > 0) { + throw new GradleException(buildStatus.devices.test_status) + } + } } diff --git a/build.gradle b/build.gradle index da14b5d4fa..0dc4f3e6e5 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,6 @@ buildscript { // Add the Crashlytics Gradle plugin. classpath 'com.google.firebase:firebase-crashlytics-gradle:2.0.0-beta04' - classpath "gradle.plugin.com.browserstack.gradle:browserstack-gradle-plugin:2.3.3" } } diff --git a/gradle.properties b/gradle.properties index 624750fe32..6bf9ee2c3e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,4 +21,12 @@ android.enableJetifier=true kotlin.code.style=official org.gradle.daemon=true org.gradle.parallel=true -annotation="" \ No newline at end of file +annotation="" +bsAuth="sudarshangs1:oaYqM81qEjMTwpAaipiQ" +bsBuilds="https://api-cloud.browserstack.com/app-automate/espresso/builds/" +bsAppUpload="https://api-cloud.browserstack.com/app-automate/upload" +bsTestUpload="https://api-cloud.browserstack.com/app-automate/espresso/test-suite" +bsExecute="https://api-cloud.browserstack.com/app-automate/espresso/build" +devDebugApk=build/outputs/apk/dev/debug/app-dev-debug.apk +devTestApk=build/outputs/apk/androidTest/dev/debug/app-dev-debug-androidTest.apk +bsE2EConfig=BrowserStackE2EConfig.json \ No newline at end of file