1
+ package io .github .lambdatest ;
2
+
3
+
4
+ import com .google .gson .Gson ;
5
+ import io .appium .java_client .AppiumDriver ;
6
+ import io .github .lambdatest .constants .Constants ;
7
+ import io .github .lambdatest .models .*;
8
+ import io .github .lambdatest .utils .GitUtils ;
9
+ import io .github .lambdatest .utils .SmartUIUtil ;
10
+ import org .openqa .selenium .Dimension ;
11
+ import org .openqa .selenium .OutputType ;
12
+ import org .openqa .selenium .TakesScreenshot ;
13
+
14
+ import java .io .File ;
15
+ import java .util .Map ;
16
+ import java .util .Objects ;
17
+ import java .util .logging .Logger ;
18
+
19
+ public class SmartUIAppSnapshot {
20
+ private final Logger log ;
21
+ private final SmartUIUtil util ;
22
+ private final Gson gson ;
23
+ private String projectToken ;
24
+
25
+ private BuildData buildData ;
26
+
27
+ public SmartUIAppSnapshot () {
28
+ this .log = Logger .getLogger ("lt-smart-ui-java-sdk" );
29
+ this .util = new SmartUIUtil ();
30
+ this .gson = new Gson ();
31
+ }
32
+
33
+
34
+ public void start (Map <String , String > options ) throws Exception {
35
+ try {
36
+ this .projectToken = getProjectToken (options );
37
+ log .info ("Project token set as: " + this .projectToken );
38
+ } catch (Exception e ){
39
+ log .severe (Constants .Errors .PROJECT_TOKEN_UNSET );
40
+ throw new Exception ("Project token is a mandatory field" );
41
+ }
42
+ Map <String , String > envVars = System .getenv ();
43
+ GitInfo git = GitUtils .getGitInfo (envVars );
44
+ // Authenticate user and create a build
45
+ try {
46
+ BuildResponse buildRes = util .build (git , this .projectToken , options );
47
+ this .buildData = buildRes .getData ();
48
+ log .info ("Build ID set : " + this .buildData .getBuildId () + "for Build name : " + this .buildData .getName ());
49
+ options .put ("buildName" , this .buildData .getName ());
50
+ } catch (Exception e ) {
51
+ log .severe ("Couldn't create build: " + e .getMessage ());
52
+ throw new IllegalStateException ("Couldn't create build: " + e .getMessage ());
53
+ }
54
+ }
55
+ public void start () throws Exception {
56
+ String envToken = System .getenv ("PROJECT_TOKEN" );
57
+ this .projectToken = envToken ;
58
+ log .info ("Project token set as: " + this .projectToken );
59
+ if (Objects .isNull (this .projectToken )){
60
+ log .severe (Constants .Errors .PROJECT_TOKEN_UNSET );
61
+ throw new Exception ("Project token is a mandatory field" );
62
+ }
63
+ Map <String , String > envVars = System .getenv ();
64
+ GitInfo git = GitUtils .getGitInfo (envVars );
65
+ // Authenticate user and create a build
66
+ try {
67
+ BuildResponse buildRes = util .build (git , this .projectToken , null );
68
+ this .buildData = buildRes .getData ();
69
+ log .info ("Build ID set : " + this .buildData .getBuildId () + "for Build name : " + this .buildData .getName ());
70
+ } catch (Exception e ) {
71
+ log .severe ("Couldn't create build: " + e .getMessage ());
72
+ throw new IllegalStateException ("Couldn't create build: " + e .getMessage ());
73
+ }
74
+ }
75
+
76
+ private String getProjectToken (Map <String , String > options ) {
77
+ if (options != null && options .containsKey (Constants .PROJECT_TOKEN )) {
78
+ String token = options .get (Constants .PROJECT_TOKEN ).trim ();
79
+ if (!token .isEmpty ()) {
80
+ return token ;
81
+ }
82
+ }
83
+ String envToken = System .getenv ("PROJECT_TOKEN" );
84
+ if (envToken != null && !envToken .trim ().isEmpty ()) {
85
+ return envToken .trim ();
86
+ }
87
+ throw new IllegalArgumentException (Constants .Errors .PROJECT_TOKEN_UNSET );
88
+ }
89
+
90
+
91
+ public void smartuiAppSnapshot (AppiumDriver appiumDriver , String screenshotName , Map <String , String > options ) throws Exception {
92
+ try {
93
+ if (Objects .isNull (appiumDriver )) {
94
+ log .severe (Constants .Errors .SELENIUM_DRIVER_NULL +" during take snapshot" );
95
+ throw new IllegalArgumentException (Constants .Errors .SELENIUM_DRIVER_NULL );
96
+ }
97
+ if (screenshotName == null || screenshotName .isEmpty ()) {
98
+ log .info (Constants .Errors .SNAPSHOT_NAME_NULL );
99
+ throw new IllegalArgumentException (Constants .Errors .SNAPSHOT_NAME_NULL );
100
+ }
101
+
102
+ TakesScreenshot takesScreenshot = (TakesScreenshot ) appiumDriver ;
103
+ File screenshot = takesScreenshot .getScreenshotAs (OutputType .FILE );
104
+ log .info ("Screenshot captured: " + screenshotName );
105
+
106
+ UploadSnapshotRequest uploadSnapshotRequest = new UploadSnapshotRequest ();
107
+ uploadSnapshotRequest .setScreenshotName (screenshotName );
108
+ uploadSnapshotRequest .setProjectToken (projectToken );
109
+ Dimension d = appiumDriver .manage ().window ().getSize ();
110
+ int w = d .getWidth (), h = d .getWidth ();
111
+ uploadSnapshotRequest .setViewport (w +"x" +h );
112
+ log .info ("Device viewport set to: " + uploadSnapshotRequest .getViewport ());
113
+ String platform = "" , deviceName ="" , browserName ="" ;
114
+ if (options != null && options .containsKey ("platform" )){
115
+ platform = options .get ("platform" ).trim ();
116
+ }
117
+ if (options != null && options .containsKey ("deviceName" )){
118
+ deviceName = options .get ("deviceName" ).trim ();
119
+ }
120
+ if (Objects .isNull (deviceName ) || deviceName .isEmpty ()){
121
+ throw new IllegalArgumentException (Constants .Errors .DEVICE_NAME_NULL );
122
+ }
123
+ if (Objects .isNull (platform ) || platform .isEmpty ()){
124
+ if (deviceName .toLowerCase ().startsWith ("i" )){
125
+ browserName = "iOS" ;
126
+ }
127
+ else {
128
+ browserName = "Android" ;
129
+ }
130
+ }
131
+ uploadSnapshotRequest .setOs (platform != null && !platform .isEmpty () ? platform : browserName );
132
+ if (platform != null && !platform .isEmpty ()){
133
+ uploadSnapshotRequest .setDeviceName (deviceName +" " +platform );
134
+ }
135
+ else {
136
+ uploadSnapshotRequest .setDeviceName (deviceName + " " +browserName );
137
+ }
138
+
139
+ if (platform .toLowerCase ().contains ("ios" )) {
140
+ uploadSnapshotRequest .setBrowserName ("safari" );
141
+ } else {
142
+ uploadSnapshotRequest .setBrowserName ("chrome" );
143
+ }
144
+ if (Objects .nonNull (buildData )) {
145
+ uploadSnapshotRequest .setBuildId (buildData .getBuildId ());
146
+ uploadSnapshotRequest .setBuildName (buildData .getName ());
147
+ }
148
+ UploadSnapshotResponse uploadSnapshotResponse = util .uploadScreenshot (screenshot ,uploadSnapshotRequest , this .buildData );
149
+ log .info ("For uploading: " + uploadSnapshotRequest .toString () + " received response: " + uploadSnapshotResponse .getData ());
150
+ } catch (Exception e ) {
151
+ log .severe (Constants .Errors .UPLOAD_SNAPSHOT_FAILED + " due to: " +e .getMessage ());
152
+ throw new Exception ("Couldnt upload image to Smart UI due to: " + e .getMessage ());
153
+ }
154
+ }
155
+
156
+ public void stop () throws Exception {
157
+ try {
158
+ if (this .buildData != null ) {
159
+ log .info ("Stopping session for buildId: " + this .buildData .getBuildId ());
160
+ if (Objects .nonNull (this .buildData .getBuildId ())){
161
+ util .stopBuild (this .buildData .getBuildId (), projectToken );
162
+ log .info ("Session ended for token: " + projectToken );}
163
+ else {
164
+ log .info ("Build ID not found to stop build for " + projectToken );
165
+ }
166
+ }
167
+ } catch (Exception e ) {
168
+ log .severe ("Couldn't stop the build due to an exception: " + e .getMessage ());
169
+ throw new Exception (Constants .Errors .STOP_BUILD_FAILED +" due to : " + e .getMessage ());
170
+ }
171
+ }
172
+ }
0 commit comments