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