forked from cliqz-oss/browser-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
125 additions
and
372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
buildSrc/src/main/groovy/com/cliqz/gradle/TestDroidTask.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.cliqz.gradle | ||
|
||
import okhttp3.FormBody | ||
import okhttp3.MediaType | ||
import okhttp3.MultipartBody | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import okhttp3.RequestBody | ||
import okhttp3.Response | ||
import okio.BufferedSink | ||
import org.apache.http.entity.mime.content.FileBody | ||
import org.bouncycastle.cert.ocsp.Req | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.tasks.TaskAction | ||
import groovy.json.JsonSlurper | ||
import org.jetbrains.annotations.NotNull | ||
|
||
class TestDroidTask extends DefaultTask { | ||
|
||
static final PROJECT_ID = '207029628' | ||
static final API_KEY = 'WnxRgICLtoIRfDqlPDYcM4xr0xTLFv7H' | ||
static final FRAMEWORK_ID = '252' | ||
static final DEVICE_GROUP_ID = '40437' | ||
|
||
final OkHttpClient client = new OkHttpClient() | ||
final slurper = new JsonSlurper() | ||
|
||
@TaskAction | ||
def connectTestdroid() { | ||
// 1. Does the project identified by the id param exists? Decide yourself how to pass this param | ||
final url = new URL("https://cloud.bitbar.com/api/me/projects/${PROJECT_ID}") | ||
final request = url.openConnection() | ||
request.setRequestProperty('Authorization', "Basic ${getBasicAuth(API_KEY)}") | ||
if (request.responseCode == HttpURLConnection.HTTP_OK) { | ||
logger.info("Project exists") | ||
} else { | ||
logger.error("No project for the given id: ${PROJECT_ID}") | ||
} | ||
// 2. Upload the 2 apks and store the ids | ||
final apkID = uploadFile("/Users/kiizajosephbazaare/browser-android/app/build/outputs/apk/cliqz/debug/app-cliqz-arm64-v8a-debug.apk") | ||
final testsID = uploadFile("/Users/kiizajosephbazaare/browser-android/app/build/outputs/apk/androidTest/cliqz/debug/app-cliqz-debug-androidTest.apk") | ||
// 3. Create the testrun and run it | ||
final runID = performTestRun(apkID, testsID) | ||
// 4. Poll every minute (os so) to check the job finished | ||
String state = pollTestRunStatus(runID) | ||
int timeTaken = 0 | ||
while (state!= "FINISHED"){ | ||
sleep(60000) | ||
timeTaken+=1 | ||
state = pollTestRunStatus(runID) | ||
} | ||
logger.info("Tests finished in ${timeTaken} minutes") | ||
// 5. Download the artifacts and put them in a predefined folder (where Azure/Jenkins can find it) | ||
// 6. log error if any test failed, otherwise just return | ||
} | ||
|
||
def getBasicAuth(String apiKey) { | ||
"${apiKey}:".bytes.encodeBase64().toString() | ||
} | ||
|
||
def uploadFile(String filepath){ | ||
final file = new File(filepath) | ||
final mediaType = MediaType.parse('application/octet-stream') | ||
def requestBody = new MultipartBody.Builder() | ||
.setType(MultipartBody.FORM) | ||
.addFormDataPart('file', file.name, RequestBody.create(file, mediaType)) | ||
.build() | ||
def request = new Request.Builder() | ||
.header('Authorization', "Basic ${getBasicAuth(API_KEY)}") | ||
.url("https://cloud.bitbar.com/api/me/files") | ||
.post(requestBody) | ||
.build() | ||
final response = client.newCall(request).execute() | ||
if (!response.isSuccessful()) throw new IOException("Unexpected file" + response.code) | ||
final json = slurper.parseText(response.body.string()) | ||
json.id | ||
} | ||
|
||
def performTestRun(long appFileID, long testFileID){ | ||
final mediaType = MediaType.parse('application/json') | ||
final content = """ | ||
{ | ||
"osType":"ANDROID", | ||
"projectId":"$PROJECT_ID", | ||
"files":[ | ||
{"id":"$appFileID"}, | ||
{"id":"$testFileID"} | ||
], | ||
"frameworkId":"$FRAMEWORK_ID", | ||
"deviceGroupId":"$DEVICE_GROUP_ID" | ||
}""".stripIndent() | ||
|
||
def request = new Request.Builder() | ||
.header('Authorization', "Basic ${getBasicAuth(API_KEY)}") | ||
.url("https://cloud.bitbar.com/api/me/runs") | ||
.post(RequestBody.create(content, mediaType)) | ||
.build() | ||
final response = client.newCall(request).execute() | ||
if (!response.isSuccessful()) throw new IOException("Test run not possible " + response.code) | ||
final testRun = slurper.parseText(response.body.string()) | ||
response.close() | ||
testRun.id | ||
} | ||
|
||
def pollTestRunStatus(long testID){ | ||
final poll = new URL("https://cloud.bitbar.com/api/me/projects/${PROJECT_ID}/runs/${testID}") | ||
final check = poll.openConnection() | ||
check.setRequestProperty('Authorization', "Basic ${getBasicAuth(API_KEY)}") | ||
if (check.responseCode == HttpURLConnection.HTTP_OK){ | ||
final pollStatus = slurper.parseText (check.content.text) | ||
pollStatus.state | ||
} else { | ||
logger.error("No Test Run for the given id: ${testID}") | ||
} | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
buildSrc/src/main/groovy/com/cliqz/gradle/TestdroidTask.groovy
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.