Skip to content

add proxy support with SmartUIAppSnapshot #25

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

Merged
merged 2 commits into from
Mar 28, 2025
Merged
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
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'io.github.lambdatest'
version = '1.0.12-beta.2'
version = '1.0.12'
description = 'lambdatest-java-sdk'

repositories {
Expand Down Expand Up @@ -83,7 +83,7 @@ afterEvaluate {
mavenJava(MavenPublication) {
groupId = 'io.github.lambdatest'
artifactId = 'lambdatest-java-sdk'
version = '1.0.12-beta.2'
version = '1.0.12'

pom {
name.set('LambdaTest Java SDK')
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.lambdatest</groupId>
<artifactId>lambdatest-java-sdk</artifactId>
<version>1.0.12-beta.2</version>
<version>1.0.12</version>
<name>lambdatest-java-sdk</name>
<description>LambdaTest SDK in Java</description>
<url>https://www.lambdatest.com</url>
Expand Down Expand Up @@ -41,12 +41,12 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
<version>4.5.14</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
<version>4.5.14</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
Expand Down Expand Up @@ -138,7 +138,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand Down
94 changes: 52 additions & 42 deletions src/main/java/io/github/lambdatest/SmartUIAppSnapshot.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.lambdatest;


import com.google.gson.Gson;
import io.github.lambdatest.constants.Constants;
import io.github.lambdatest.models.*;
Expand All @@ -19,42 +18,53 @@
import io.github.lambdatest.utils.LoggerUtil;

public class SmartUIAppSnapshot {
private final Logger log;
private final Logger log = LoggerUtil.createLogger("lambdatest-java-app-sdk");
private final SmartUIUtil util;
private final Gson gson;
private final Gson gson = new Gson();
private String projectToken;

private BuildData buildData;

public SmartUIAppSnapshot() {
this.log = LoggerUtil.createLogger("lambdatest-java-app-sdk");
this.util = new SmartUIUtil();
this.gson = new Gson();
}

public void start(Map<String, String> options) throws Exception{
try{
public SmartUIAppSnapshot(String proxyHost, int proxyPort) throws Exception {
this.util = new SmartUIUtil(proxyHost, proxyPort);
}

public SmartUIAppSnapshot(String proxyHost, int proxyPort, boolean allowInsecure) throws Exception {
this.util = new SmartUIUtil(proxyHost, proxyPort, allowInsecure);
}

public SmartUIAppSnapshot(String proxyProtocol, String proxyHost, int proxyPort, boolean allowInsecure)
throws Exception {
this.util = new SmartUIUtil(proxyProtocol, proxyHost, proxyPort, allowInsecure);
}

public void start(Map<String, String> options) throws Exception {
try {
this.projectToken = getProjectToken(options);
log.info("Project token set as: " + this.projectToken);
} catch (Exception e){
} catch (Exception e) {
log.severe(Constants.Errors.PROJECT_TOKEN_UNSET);
throw new Exception("Project token is a mandatory field", e);
}

try {
Map<String, String> envVars = new HashMap<>(System.getenv());
GitInfo git = GitUtils.getGitInfo(envVars);
BuildResponse buildRes = util.build(git, this.projectToken, options);
this.buildData = buildRes.getData();
log.info("Build ID set : " + this.buildData.getBuildId() + "for Build name : "+ this.buildData.getName());
log.info("Build ID set : " + this.buildData.getBuildId() + "for Build name : " + this.buildData.getName());
options.put("buildName", this.buildData.getName());
} catch(Exception e) {
} catch (Exception e) {
log.severe("Couldn't create smartui build: " + e.getMessage());
throw new Exception("Couldn't create smartui build: " + e.getMessage());
}
}

public void start() throws Exception{
public void start() throws Exception {
this.start(new HashMap<>());
}

Expand All @@ -72,11 +82,11 @@ private String getProjectToken(Map<String, String> options) {
throw new IllegalArgumentException(Constants.Errors.PROJECT_TOKEN_UNSET);
}


public void smartuiAppSnapshot(WebDriver appiumDriver, String screenshotName, Map<String, String> options) throws Exception {
public void smartuiAppSnapshot(WebDriver appiumDriver, String screenshotName, Map<String, String> options)
throws Exception {
try {
if (appiumDriver == null) {
log.severe(Constants.Errors.SELENIUM_DRIVER_NULL +" during take snapshot");
log.severe(Constants.Errors.SELENIUM_DRIVER_NULL + " during take snapshot");
throw new IllegalArgumentException(Constants.Errors.SELENIUM_DRIVER_NULL);
}
if (screenshotName == null || screenshotName.isEmpty()) {
Expand All @@ -93,32 +103,30 @@ public void smartuiAppSnapshot(WebDriver appiumDriver, String screenshotName, Ma
uploadSnapshotRequest.setProjectToken(projectToken);
Dimension d = appiumDriver.manage().window().getSize();
int w = d.getWidth(), h = d.getHeight();
uploadSnapshotRequest.setViewport(w+"x"+h);
log.info("Device viewport set to: "+ uploadSnapshotRequest.getViewport());
String platform = "", deviceName="", browserName ="";
if(options != null && options.containsKey("platform")){
uploadSnapshotRequest.setViewport(w + "x" + h);
log.info("Device viewport set to: " + uploadSnapshotRequest.getViewport());
String platform = "", deviceName = "", browserName = "";
if (options != null && options.containsKey("platform")) {
platform = options.get("platform").trim();
}
if(options != null && options.containsKey("deviceName")){
if (options != null && options.containsKey("deviceName")) {
deviceName = options.get("deviceName").trim();
}
if(deviceName == null || deviceName.isEmpty()){
if (deviceName == null || deviceName.isEmpty()) {
throw new IllegalArgumentException(Constants.Errors.DEVICE_NAME_NULL);
}
if(platform == null || platform.isEmpty()){
if(deviceName.toLowerCase().startsWith("i")){
browserName = "iOS";
}
else {
if (platform == null || platform.isEmpty()) {
if (deviceName.toLowerCase().startsWith("i")) {
browserName = "iOS";
} else {
browserName = "Android";
}
}
uploadSnapshotRequest.setOs(platform != null && !platform.isEmpty() ? platform : browserName);
if(platform != null && !platform.isEmpty()){
uploadSnapshotRequest.setDeviceName(deviceName+" "+platform);
}
else {
uploadSnapshotRequest.setDeviceName(deviceName + " "+browserName);
if (platform != null && !platform.isEmpty()) {
uploadSnapshotRequest.setDeviceName(deviceName + " " + platform);
} else {
uploadSnapshotRequest.setDeviceName(deviceName + " " + browserName);
}

if (platform.toLowerCase().contains("ios")) {
Expand All @@ -130,28 +138,30 @@ public void smartuiAppSnapshot(WebDriver appiumDriver, String screenshotName, Ma
uploadSnapshotRequest.setBuildId(buildData.getBuildId());
uploadSnapshotRequest.setBuildName(buildData.getName());
}
UploadSnapshotResponse uploadSnapshotResponse = util.uploadScreenshot(screenshot,uploadSnapshotRequest, this.buildData);
log.info("For uploading: " + uploadSnapshotRequest.toString() + " received response: "+ uploadSnapshotResponse.getData());
UploadSnapshotResponse uploadSnapshotResponse = util.uploadScreenshot(screenshot, uploadSnapshotRequest,
this.buildData);
log.info("For uploading: " + uploadSnapshotRequest.toString() + " received response: "
+ uploadSnapshotResponse.getData());
} catch (Exception e) {
log.severe(Constants.Errors.UPLOAD_SNAPSHOT_FAILED + " due to: " +e.getMessage());
throw new Exception("Couldnt upload image to Smart UI due to: " + e.getMessage());
log.severe(Constants.Errors.UPLOAD_SNAPSHOT_FAILED + " due to: " + e.getMessage());
throw new Exception("Couldnt upload image to Smart UI due to: " + e.getMessage());
}
}

public void stop() throws Exception{
public void stop() throws Exception {
try {
if (this.buildData != null) {
log.info("Stopping session for buildId: " + this.buildData.getBuildId());
if(Objects.nonNull(this.buildData.getBuildId())){
util.stopBuild(this.buildData.getBuildId(), projectToken);
log.info("Session ended for token: " + projectToken);}
else {
log.info("Build ID not found to stop build for "+ projectToken);
if (Objects.nonNull(this.buildData.getBuildId())) {
util.stopBuild(this.buildData.getBuildId(), projectToken);
log.info("Session ended for token: " + projectToken);
} else {
log.info("Build ID not found to stop build for " + projectToken);
}
}
} catch (Exception e) {
log.severe("Couldn't stop the build due to an exception: " + e.getMessage());
throw new Exception(Constants.Errors.STOP_BUILD_FAILED +" due to : "+ e.getMessage());
throw new Exception(Constants.Errors.STOP_BUILD_FAILED + " due to : " + e.getMessage());
}
}
}
97 changes: 95 additions & 2 deletions src/main/java/io/github/lambdatest/utils/HttpClientUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,108 @@
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import io.github.lambdatest.utils.LoggerUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import javax.net.ssl.SSLContext;

public class HttpClientUtil {
private final CloseableHttpClient httpClient;
private Logger log;
private Logger log = LoggerUtil.createLogger("lambdatest-java-sdk");

public HttpClientUtil() {
this.httpClient = HttpClients.createDefault();
this.log = LoggerUtil.createLogger("lambdatest-java-sdk");
}

public HttpClientUtil(String proxyHost, int proxyPort) throws Exception {
this(proxyHost, proxyPort, false);
}

public HttpClientUtil(String proxyHost, int proxyPort, boolean allowInsecure) throws Exception {
try {
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(proxy)
.build();
// Build the HttpClient with conditional SSL settings
CloseableHttpClient clientBuilder;

// If allowInsecure is true, disable SSL verification
if (allowInsecure) {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, TrustAllStrategy.INSTANCE)
.build();

clientBuilder = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setSSLContext(sslContext)
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
} else {
// Build standard HttpClient with proxy
clientBuilder = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.build();
}

this.httpClient = clientBuilder;
String proxyConfig = String.format("%s:%d (Insecure: %b)", proxyHost, proxyPort, allowInsecure);
log.info(proxyConfig);

} catch (Exception e) {
log.severe("Error configuring HttpClient" + e.getMessage());
throw new Exception("Failed to create HttpClient" + e.getMessage());
}
}

public HttpClientUtil(String proxyProtocol, String proxyHost, int proxyPort, boolean allowInsecure)
throws Exception {
try {
HttpHost proxy = new HttpHost(proxyHost, proxyPort, proxyProtocol);

RequestConfig requestConfig = RequestConfig.custom()
.setProxy(proxy)
.build();

// Build the HttpClient with conditional SSL settings
CloseableHttpClient clientBuilder;

// If allowInsecure is true, disable SSL verification
if (allowInsecure) {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, TrustAllStrategy.INSTANCE)
.build();

clientBuilder = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setSSLContext(sslContext)
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();

} else {
// Build standard HttpClient with proxy
clientBuilder = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.build();
}

// Assign the built HttpClient
this.httpClient = clientBuilder;

String proxyConfig = String.format("%s://%s:%d (Insecure: %b)",
proxyProtocol, proxyHost, proxyPort, allowInsecure);
log.info(proxyConfig);
} catch (Exception e) {
log.severe("Error configuring HttpClient" + e.getMessage());
throw new Exception("Failed to create HttpClient" + e.getMessage());
}
}

public String request(String url, String method, String data) throws IOException {
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/io/github/lambdatest/utils/SmartUIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@

public class SmartUIUtil {
private final HttpClientUtil httpClient;
private final Logger log;
private final Logger log = LoggerUtil.createLogger("lambdatest-java-sdk");
private Gson gson = new Gson();

public SmartUIUtil() {
this.httpClient = new HttpClientUtil();
this.log = LoggerUtil.createLogger("lambdatest-java-sdk");
}

public SmartUIUtil(String proxyHost, int proxyPort) throws Exception {
this.httpClient = new HttpClientUtil(proxyHost, proxyPort);
}

public SmartUIUtil(String proxyHost, int proxyPort, boolean allowInsecure) throws Exception {
this.httpClient = new HttpClientUtil(proxyHost, proxyPort, allowInsecure);
}

public SmartUIUtil(String proxyProtocol, String proxyHost, int proxyPort, boolean allowInsecure) throws Exception {
this.httpClient = new HttpClientUtil(proxyProtocol, proxyHost, proxyPort, allowInsecure);
}

public boolean isSmartUIRunning() {
Expand Down Expand Up @@ -111,7 +122,7 @@ public BuildResponse build(GitInfo git, String projectToken, Map<String, String>
log.info("Build name set from system: " + buildNameStr);
}
} else {
createBuildRequest.setBuildName("smartui-" + UUID.randomUUID().toString().substring(0, 10));
createBuildRequest.setBuildName("smartui-" + UUID.randomUUID().toString().substring(0, 10));
}
if (Objects.nonNull(git)) {
createBuildRequest.setGit(git);
Expand Down