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

Added support for virtual device testing/upload apks #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id 'com.diffplug.spotless' version '6.25.0'
}

version = '1.0.6'
version = '1.0.7'
group = 'io.github.lambdatest'

repositories {
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/io/github/lambdatest/gradle/AppUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* Handles the asynchronous upload of application files to the LambdaTest platform. This class
* manages the upload process and returns the application ID for test execution.
*
* <p>Uses {@link UploaderUtil#uploadAndGetId(String, String, String)} for the actual file upload
* process.
* <p>Uses {@link UploaderUtil#uploadAndGetId(String, String, String,Boolean)} for the actual file
* upload process.
*/
public class AppUploader {

Expand All @@ -19,22 +19,26 @@ public class AppUploader {
private String username;
private String accessKey;
private String appFilePath;
private Boolean isVirtualDevice;

/**
* Creates a new AppUploader instance with the specified credentials and file path.
*
* @param username The LambdaTest account username
* @param accessKey The LambdaTest account access key
* @param appFilePath The path to the application file to be uploaded
* @param isVirtualDevice Boolean indicating if upload is to a virtual device
*/
public AppUploader(String username, String accessKey, String appFilePath) {
public AppUploader(
String username, String accessKey, String appFilePath, Boolean isVirtualDevice) {
if (username == null) throw new IllegalArgumentException("Username cannot be null");
if (accessKey == null) throw new IllegalArgumentException("Access Key cannot be null");
if (appFilePath == null) throw new IllegalArgumentException("App File Path cannot be null");

this.username = username;
this.accessKey = accessKey;
this.appFilePath = appFilePath;
this.isVirtualDevice = isVirtualDevice;
}

/**
Expand All @@ -49,7 +53,8 @@ public CompletableFuture<String> uploadAppAsync() {
() -> {
try {
String appId =
UploaderUtil.uploadAndGetId(username, accessKey, appFilePath);
UploaderUtil.uploadAndGetId(
username, accessKey, appFilePath, isVirtualDevice);
logger.info("Uploaded app ID: {}", appId);
return appId;
} catch (IOException e) {
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/io/github/lambdatest/gradle/LambdaTestTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class LambdaTestTask extends DefaultTask {
private String appId;
private String testSuiteId;
private Integer queueTimeout;
private Boolean isVirtualDevice = false;

/**
* Executes the LambdaTest task, which includes uploading the application and test suite,
Expand All @@ -66,14 +67,15 @@ public void runLambdaTest() {

if (appId == null && appFilePath != null) {
logger.info("Uploading app...");
AppUploader appUploader = new AppUploader(username, accessKey, appFilePath);
AppUploader appUploader =
new AppUploader(username, accessKey, appFilePath, isVirtualDevice);
appIdFuture = appUploader.uploadAppAsync();
}

if (testSuiteId == null && testSuiteFilePath != null) {
logger.info("Uploading test suite...");
TestSuiteUploader testSuiteUploader =
new TestSuiteUploader(username, accessKey, testSuiteFilePath);
new TestSuiteUploader(username, accessKey, testSuiteFilePath, isVirtualDevice);
testSuiteIdFuture = testSuiteUploader.uploadTestSuiteAsync();
}

Expand All @@ -96,7 +98,14 @@ public void runLambdaTest() {
// Execute tests
logger.info("Executing tests...");
TestExecutor testExecutor =
new TestExecutor(username, accessKey, appId, testSuiteId, device, isFlutter);
new TestExecutor(
username,
accessKey,
appId,
testSuiteId,
device,
isFlutter,
isVirtualDevice);
Map<String, String> params = new HashMap<>();

if (build != null) params.put("build", build);
Expand Down Expand Up @@ -217,4 +226,13 @@ public void setTestSuiteId(String testSuiteId) {
this.testSuiteId = testSuiteId;
}
}

/**
* Sets whether the device used for the test execution is a virtual device.
*
* @param isVirtualDevice true if the device is a virtual device, false otherwise
*/
public void setIsVirtualDevice(Boolean isVirtualDevice) {
this.isVirtualDevice = (isVirtualDevice != null && isVirtualDevice);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class LambdaUploaderTask extends DefaultTask {
private String accessKey;
private String appFilePath;
private String testSuiteFilePath;
private Boolean isVirtualDevice = false;

@TaskAction
public void uploadApkToLambdaTest() {
Expand All @@ -33,14 +34,15 @@ public void uploadApkToLambdaTest() {

if (appFilePath != null) {
logger.lifecycle("Uploading app ...");
AppUploader appUploader = new AppUploader(username, accessKey, appFilePath);
AppUploader appUploader =
new AppUploader(username, accessKey, appFilePath, isVirtualDevice);
appIdFuture = appUploader.uploadAppAsync();
}

if (testSuiteFilePath != null) {
logger.lifecycle("Uploading test suite ...");
TestSuiteUploader testSuiteUploader =
new TestSuiteUploader(username, accessKey, testSuiteFilePath);
new TestSuiteUploader(username, accessKey, testSuiteFilePath, isVirtualDevice);
testSuiteIdFuture = testSuiteUploader.uploadTestSuiteAsync();
}

Expand Down Expand Up @@ -80,4 +82,8 @@ public void setAppFilePath(String appFilePath) {
public void setTestSuiteFilePath(String testSuiteFilePath) {
this.testSuiteFilePath = testSuiteFilePath;
}

public void setIsVirtualDevice(Boolean isVirtualDevice) {
this.isVirtualDevice = (isVirtualDevice != null && isVirtualDevice);
}
}
8 changes: 7 additions & 1 deletion src/main/java/io/github/lambdatest/gradle/TestExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class TestExecutor {
private String testSuiteId;
private List<String> device;
private Boolean isFlutter;
private Boolean isVirtualDevice;

/**
* Creates a new TestExecutor with the specified configuration.
Expand All @@ -41,13 +42,15 @@ public TestExecutor(
String appId,
String testSuiteId,
List<String> device,
Boolean isFlutter) {
Boolean isFlutter,
Boolean isVirtualDevice) {
this.username = username;
this.accessKey = accessKey;
this.appId = appId;
this.testSuiteId = testSuiteId;
this.device = device;
this.isFlutter = isFlutter;
this.isVirtualDevice = isVirtualDevice;
}

/**
Expand All @@ -70,6 +73,9 @@ public void executeTests(Map<String, String> params) throws IOException {
capabilities.put("app", appId);
capabilities.put("testSuite", testSuiteId);
capabilities.put("device", device);
if (isVirtualDevice) {
capabilities.put("isVirtualDevice", isVirtualDevice);
}
capabilities.putAll(params);

logger.info("Capabilities: {}", capabilities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ public class TestSuiteUploader {
private String username;
private String accessKey;
private String testSuiteFilePath;
private Boolean isVirtualDevice;

/**
* Creates a new TestSuiteUploader instance with the specified credentials and file path.
*
* @param username The LambdaTest account username
* @param accessKey The LambdaTest account access key
* @param testSuiteFilePath The path to the test suite file to be uploaded
* @param isVirtualDevice Boolean indicating if upload is to a virtual device
*/
public TestSuiteUploader(String username, String accessKey, String testSuiteFilePath) {
public TestSuiteUploader(
String username, String accessKey, String testSuiteFilePath, Boolean isVirtualDevice) {
this.username = username;
this.accessKey = accessKey;
this.testSuiteFilePath = testSuiteFilePath;
this.isVirtualDevice = isVirtualDevice;
}

/**
Expand All @@ -42,7 +46,8 @@ public CompletableFuture<String> uploadTestSuiteAsync() {
() -> {
try {
String testSuiteId =
UploaderUtil.uploadAndGetId(username, accessKey, testSuiteFilePath);
UploaderUtil.uploadAndGetId(
username, accessKey, testSuiteFilePath, isVirtualDevice);
logger.info("Uploaded test suite ID: {}", testSuiteId);
return testSuiteId;
} catch (IOException e) {
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/io/github/lambdatest/gradle/UploaderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ private UploaderUtil() {
* @param username The LambdaTest account username
* @param accessKey The LambdaTest account access key
* @param filePath The path to the file to be uploaded
* @param isVirtualDevice Boolean indicating if the device is virtual
* @return The ID of the uploaded file
* @throws IOException if there's an error during file upload or response parsing
*/
public static String uploadAndGetId(String username, String accessKey, String filePath)
public static String uploadAndGetId(
String username, String accessKey, String filePath, Boolean isVirtualDevice)
throws IOException {
OkHttpClient client =
new OkHttpClient.Builder()
Expand All @@ -48,15 +50,20 @@ public static String uploadAndGetId(String username, String accessKey, String fi
.build();

MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body =
MultipartBody.Builder builder =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"appFile",
filePath,
RequestBody.create(new File(filePath), mediaType))
.addFormDataPart("type", "espresso-android")
.build();
.addFormDataPart("type", "espresso-android");

if (isVirtualDevice) {
builder.addFormDataPart("isVirtualDevice", String.valueOf(isVirtualDevice));
}

RequestBody body = builder.build();
Request request =
new Request.Builder()
.url(Constants.API_URL)
Expand Down
Loading