1
1
package io .github .lambdatest ;
2
2
3
-
4
3
import com .google .gson .Gson ;
5
4
import io .github .lambdatest .constants .Constants ;
6
5
import io .github .lambdatest .models .*;
19
18
import io .github .lambdatest .utils .LoggerUtil ;
20
19
21
20
public class SmartUIAppSnapshot {
22
- private final Logger log ;
21
+ private final Logger log = LoggerUtil . createLogger ( "lambdatest-java-app-sdk" ) ;
23
22
private final SmartUIUtil util ;
24
- private final Gson gson ;
23
+ private final Gson gson = new Gson () ;
25
24
private String projectToken ;
26
25
27
26
private BuildData buildData ;
28
27
29
28
public SmartUIAppSnapshot () {
30
- this .log = LoggerUtil .createLogger ("lambdatest-java-app-sdk" );
31
29
this .util = new SmartUIUtil ();
32
- this .gson = new Gson ();
33
30
}
34
31
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 {
37
47
this .projectToken = getProjectToken (options );
38
48
log .info ("Project token set as: " + this .projectToken );
39
- } catch (Exception e ){
49
+ } catch (Exception e ) {
40
50
log .severe (Constants .Errors .PROJECT_TOKEN_UNSET );
41
51
throw new Exception ("Project token is a mandatory field" , e );
42
52
}
43
-
53
+
44
54
try {
45
55
Map <String , String > envVars = new HashMap <>(System .getenv ());
46
56
GitInfo git = GitUtils .getGitInfo (envVars );
47
57
BuildResponse buildRes = util .build (git , this .projectToken , options );
48
58
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 ());
50
60
options .put ("buildName" , this .buildData .getName ());
51
- } catch (Exception e ) {
61
+ } catch (Exception e ) {
52
62
log .severe ("Couldn't create smartui build: " + e .getMessage ());
53
63
throw new Exception ("Couldn't create smartui build: " + e .getMessage ());
54
64
}
55
65
}
56
66
57
- public void start () throws Exception {
67
+ public void start () throws Exception {
58
68
this .start (new HashMap <>());
59
69
}
60
70
@@ -72,11 +82,11 @@ private String getProjectToken(Map<String, String> options) {
72
82
throw new IllegalArgumentException (Constants .Errors .PROJECT_TOKEN_UNSET );
73
83
}
74
84
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 {
77
87
try {
78
88
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" );
80
90
throw new IllegalArgumentException (Constants .Errors .SELENIUM_DRIVER_NULL );
81
91
}
82
92
if (screenshotName == null || screenshotName .isEmpty ()) {
@@ -93,32 +103,30 @@ public void smartuiAppSnapshot(WebDriver appiumDriver, String screenshotName, Ma
93
103
uploadSnapshotRequest .setProjectToken (projectToken );
94
104
Dimension d = appiumDriver .manage ().window ().getSize ();
95
105
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" )) {
100
110
platform = options .get ("platform" ).trim ();
101
111
}
102
- if (options != null && options .containsKey ("deviceName" )){
112
+ if (options != null && options .containsKey ("deviceName" )) {
103
113
deviceName = options .get ("deviceName" ).trim ();
104
114
}
105
- if (deviceName == null || deviceName .isEmpty ()){
115
+ if (deviceName == null || deviceName .isEmpty ()) {
106
116
throw new IllegalArgumentException (Constants .Errors .DEVICE_NAME_NULL );
107
117
}
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 {
113
122
browserName = "Android" ;
114
123
}
115
124
}
116
125
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 );
122
130
}
123
131
124
132
if (platform .toLowerCase ().contains ("ios" )) {
@@ -130,28 +138,30 @@ public void smartuiAppSnapshot(WebDriver appiumDriver, String screenshotName, Ma
130
138
uploadSnapshotRequest .setBuildId (buildData .getBuildId ());
131
139
uploadSnapshotRequest .setBuildName (buildData .getName ());
132
140
}
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 ());
135
145
} 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 ());
138
148
}
139
149
}
140
150
141
- public void stop () throws Exception {
151
+ public void stop () throws Exception {
142
152
try {
143
153
if (this .buildData != null ) {
144
154
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 );
150
160
}
151
161
}
152
162
} catch (Exception e ) {
153
163
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 ());
155
165
}
156
166
}
157
167
}
0 commit comments