11package io .github .lambdatest ;
22
3-
43import com .google .gson .Gson ;
54import io .github .lambdatest .constants .Constants ;
65import io .github .lambdatest .models .*;
1918import io .github .lambdatest .utils .LoggerUtil ;
2019
2120public class SmartUIAppSnapshot {
22- private final Logger log ;
21+ private final Logger log = LoggerUtil . createLogger ( "lambdatest-java-app-sdk" ) ;
2322 private final SmartUIUtil util ;
24- private final Gson gson ;
23+ private final Gson gson = new Gson () ;
2524 private String projectToken ;
2625
2726 private BuildData buildData ;
2827
2928 public SmartUIAppSnapshot () {
30- this .log = LoggerUtil .createLogger ("lambdatest-java-app-sdk" );
3129 this .util = new SmartUIUtil ();
32- this .gson = new Gson ();
3330 }
3431
35- public void start (Map <String , String > options ) throws Exception {
36- try {
32+ public SmartUIAppSnapshot (String proxyHost , int proxyPort ) throws Exception {
33+ this .util = new SmartUIUtil (proxyHost , proxyPort );
34+ }
35+
36+ public SmartUIAppSnapshot (String proxyHost , int proxyPort , boolean allowInsecure ) throws Exception {
37+ this .util = new SmartUIUtil (proxyHost , proxyPort , allowInsecure );
38+ }
39+
40+ public SmartUIAppSnapshot (String proxyProtocol , String proxyHost , int proxyPort , boolean allowInsecure )
41+ throws Exception {
42+ this .util = new SmartUIUtil (proxyProtocol , proxyHost , proxyPort , allowInsecure );
43+ }
44+
45+ public void start (Map <String , String > options ) throws Exception {
46+ try {
3747 this .projectToken = getProjectToken (options );
3848 log .info ("Project token set as: " + this .projectToken );
39- } catch (Exception e ){
49+ } catch (Exception e ) {
4050 log .severe (Constants .Errors .PROJECT_TOKEN_UNSET );
4151 throw new Exception ("Project token is a mandatory field" , e );
4252 }
43-
53+
4454 try {
4555 Map <String , String > envVars = new HashMap <>(System .getenv ());
4656 GitInfo git = GitUtils .getGitInfo (envVars );
4757 BuildResponse buildRes = util .build (git , this .projectToken , options );
4858 this .buildData = buildRes .getData ();
49- log .info ("Build ID set : " + this .buildData .getBuildId () + "for Build name : " + this .buildData .getName ());
59+ log .info ("Build ID set : " + this .buildData .getBuildId () + "for Build name : " + this .buildData .getName ());
5060 options .put ("buildName" , this .buildData .getName ());
51- } catch (Exception e ) {
61+ } catch (Exception e ) {
5262 log .severe ("Couldn't create smartui build: " + e .getMessage ());
5363 throw new Exception ("Couldn't create smartui build: " + e .getMessage ());
5464 }
5565 }
5666
57- public void start () throws Exception {
67+ public void start () throws Exception {
5868 this .start (new HashMap <>());
5969 }
6070
@@ -72,11 +82,11 @@ private String getProjectToken(Map<String, String> options) {
7282 throw new IllegalArgumentException (Constants .Errors .PROJECT_TOKEN_UNSET );
7383 }
7484
75-
76- public void smartuiAppSnapshot ( WebDriver appiumDriver , String screenshotName , Map < String , String > options ) throws Exception {
85+ public void smartuiAppSnapshot ( WebDriver appiumDriver , String screenshotName , Map < String , String > options )
86+ throws Exception {
7787 try {
7888 if (appiumDriver == null ) {
79- log .severe (Constants .Errors .SELENIUM_DRIVER_NULL +" during take snapshot" );
89+ log .severe (Constants .Errors .SELENIUM_DRIVER_NULL + " during take snapshot" );
8090 throw new IllegalArgumentException (Constants .Errors .SELENIUM_DRIVER_NULL );
8191 }
8292 if (screenshotName == null || screenshotName .isEmpty ()) {
@@ -93,32 +103,30 @@ public void smartuiAppSnapshot(WebDriver appiumDriver, String screenshotName, Ma
93103 uploadSnapshotRequest .setProjectToken (projectToken );
94104 Dimension d = appiumDriver .manage ().window ().getSize ();
95105 int w = d .getWidth (), h = d .getHeight ();
96- uploadSnapshotRequest .setViewport (w + "x" + h );
97- log .info ("Device viewport set to: " + uploadSnapshotRequest .getViewport ());
98- String platform = "" , deviceName = "" , browserName ="" ;
99- if (options != null && options .containsKey ("platform" )){
106+ uploadSnapshotRequest .setViewport (w + "x" + h );
107+ log .info ("Device viewport set to: " + uploadSnapshotRequest .getViewport ());
108+ String platform = "" , deviceName = "" , browserName = "" ;
109+ if (options != null && options .containsKey ("platform" )) {
100110 platform = options .get ("platform" ).trim ();
101111 }
102- if (options != null && options .containsKey ("deviceName" )){
112+ if (options != null && options .containsKey ("deviceName" )) {
103113 deviceName = options .get ("deviceName" ).trim ();
104114 }
105- if (deviceName == null || deviceName .isEmpty ()){
115+ if (deviceName == null || deviceName .isEmpty ()) {
106116 throw new IllegalArgumentException (Constants .Errors .DEVICE_NAME_NULL );
107117 }
108- if (platform == null || platform .isEmpty ()){
109- if (deviceName .toLowerCase ().startsWith ("i" )){
110- browserName = "iOS" ;
111- }
112- else {
118+ if (platform == null || platform .isEmpty ()) {
119+ if (deviceName .toLowerCase ().startsWith ("i" )) {
120+ browserName = "iOS" ;
121+ } else {
113122 browserName = "Android" ;
114123 }
115124 }
116125 uploadSnapshotRequest .setOs (platform != null && !platform .isEmpty () ? platform : browserName );
117- if (platform != null && !platform .isEmpty ()){
118- uploadSnapshotRequest .setDeviceName (deviceName +" " +platform );
119- }
120- else {
121- uploadSnapshotRequest .setDeviceName (deviceName + " " +browserName );
126+ if (platform != null && !platform .isEmpty ()) {
127+ uploadSnapshotRequest .setDeviceName (deviceName + " " + platform );
128+ } else {
129+ uploadSnapshotRequest .setDeviceName (deviceName + " " + browserName );
122130 }
123131
124132 if (platform .toLowerCase ().contains ("ios" )) {
@@ -130,28 +138,30 @@ public void smartuiAppSnapshot(WebDriver appiumDriver, String screenshotName, Ma
130138 uploadSnapshotRequest .setBuildId (buildData .getBuildId ());
131139 uploadSnapshotRequest .setBuildName (buildData .getName ());
132140 }
133- UploadSnapshotResponse uploadSnapshotResponse = util .uploadScreenshot (screenshot ,uploadSnapshotRequest , this .buildData );
134- log .info ("For uploading: " + uploadSnapshotRequest .toString () + " received response: " + uploadSnapshotResponse .getData ());
141+ UploadSnapshotResponse uploadSnapshotResponse = util .uploadScreenshot (screenshot , uploadSnapshotRequest ,
142+ this .buildData );
143+ log .info ("For uploading: " + uploadSnapshotRequest .toString () + " received response: "
144+ + uploadSnapshotResponse .getData ());
135145 } catch (Exception e ) {
136- log .severe (Constants .Errors .UPLOAD_SNAPSHOT_FAILED + " due to: " +e .getMessage ());
137- throw new Exception ("Couldnt upload image to Smart UI due to: " + e .getMessage ());
146+ log .severe (Constants .Errors .UPLOAD_SNAPSHOT_FAILED + " due to: " + e .getMessage ());
147+ throw new Exception ("Couldnt upload image to Smart UI due to: " + e .getMessage ());
138148 }
139149 }
140150
141- public void stop () throws Exception {
151+ public void stop () throws Exception {
142152 try {
143153 if (this .buildData != null ) {
144154 log .info ("Stopping session for buildId: " + this .buildData .getBuildId ());
145- if (Objects .nonNull (this .buildData .getBuildId ())){
146- util .stopBuild (this .buildData .getBuildId (), projectToken );
147- log .info ("Session ended for token: " + projectToken );}
148- else {
149- log .info ("Build ID not found to stop build for " + projectToken );
155+ if (Objects .nonNull (this .buildData .getBuildId ())) {
156+ util .stopBuild (this .buildData .getBuildId (), projectToken );
157+ log .info ("Session ended for token: " + projectToken );
158+ } else {
159+ log .info ("Build ID not found to stop build for " + projectToken );
150160 }
151161 }
152162 } catch (Exception e ) {
153163 log .severe ("Couldn't stop the build due to an exception: " + e .getMessage ());
154- throw new Exception (Constants .Errors .STOP_BUILD_FAILED +" due to : " + e .getMessage ());
164+ throw new Exception (Constants .Errors .STOP_BUILD_FAILED + " due to : " + e .getMessage ());
155165 }
156166 }
157167}
0 commit comments