-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSmartUIAppSnapshot.java
220 lines (200 loc) · 10.2 KB
/
SmartUIAppSnapshot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package io.github.lambdatest;
import com.google.gson.Gson;
import io.github.lambdatest.constants.Constants;
import io.github.lambdatest.models.*;
import io.github.lambdatest.utils.FullPageScreenshotUtil;
import io.github.lambdatest.utils.GitUtils;
import io.github.lambdatest.utils.SmartUIUtil;
import org.openqa.selenium.*;
import java.io.File;
import java.util.*;
import java.util.logging.Logger;
import io.github.lambdatest.utils.LoggerUtil;
public class SmartUIAppSnapshot {
private final Logger log = LoggerUtil.createLogger("lambdatest-java-app-sdk");
private final SmartUIUtil util;
private final Gson gson = new Gson();
private String projectToken;
private BuildData buildData;
public SmartUIAppSnapshot() {
this.util = new SmartUIUtil();
}
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) {
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());
options.put("buildName", this.buildData.getName());
} 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 {
this.start(new HashMap<>());
}
private String getProjectToken(Map<String, String> options) {
if (options != null && options.containsKey(Constants.PROJECT_TOKEN)) {
String token = options.get(Constants.PROJECT_TOKEN).trim();
if (!token.isEmpty()) {
return token;
}
}
String envToken = System.getenv("PROJECT_TOKEN");
if (envToken != null && !envToken.trim().isEmpty()) {
return envToken.trim();
}
throw new IllegalArgumentException(Constants.Errors.PROJECT_TOKEN_UNSET);
}
private void validateMandatoryParams(WebDriver driver, String screenshotName, String deviceName) {
if (driver == null) {
log.severe(Constants.Errors.SELENIUM_DRIVER_NULL + " during take snapshot");
throw new IllegalArgumentException(Constants.Errors.SELENIUM_DRIVER_NULL);
}
if (screenshotName == null || screenshotName.isEmpty()) {
log.info(Constants.Errors.SNAPSHOT_NAME_NULL);
throw new IllegalArgumentException(Constants.Errors.SNAPSHOT_NAME_NULL);
}
if (deviceName == null || deviceName.isEmpty()) {
throw new IllegalArgumentException(Constants.Errors.DEVICE_NAME_NULL);
}
}
private String getOptionValue(Map<String, String> options, String key) {
if (options != null && options.containsKey(key)) {
String value = options.get(key);
return value != null ? value.trim() : "";
}
return "";
}
private UploadSnapshotRequest initializeUploadRequest(String screenshotName, String viewport) {
UploadSnapshotRequest request = new UploadSnapshotRequest();
request.setScreenshotName(screenshotName);
request.setProjectToken(projectToken);
request.setViewport(viewport);
log.info("Viewport set to :" + viewport);
if (Objects.nonNull(buildData)) {
request.setBuildId(buildData.getBuildId());
request.setBuildName(buildData.getName());
}
return request;
}
private UploadSnapshotRequest configureDeviceNameAndPlatform(UploadSnapshotRequest request, String deviceName, String platform) {
String browserName = deviceName.toLowerCase().startsWith("i") ? "iOS" : "Android";
String platformName = (platform == null || platform.isEmpty()) ? browserName : platform;
request.setOs(platformName);
request.setDeviceName(deviceName + " " + platformName);
assert platform != null;
request.setBrowserName(platform.toLowerCase().contains("ios") ? "safari" : "chrome");
return request;
}
public void smartuiAppSnapshot(WebDriver driver, String screenshotName, Map<String, String> options)
throws Exception {
try {
String deviceName = getOptionValue(options, "deviceName");
String platform = getOptionValue(options, "platform");
validateMandatoryParams(driver, screenshotName, deviceName);
Dimension d = driver.manage().window().getSize();
int width = d.getWidth(), height = d.getHeight();
UploadSnapshotRequest initReq = initializeUploadRequest(screenshotName, width + "x" + height);
UploadSnapshotRequest uploadSnapshotRequest = configureDeviceNameAndPlatform(initReq, deviceName, platform);
String screenshotHash = UUID.randomUUID().toString();
uploadSnapshotRequest.setScreenshotHash(screenshotHash);
String uploadChunk = getOptionValue(options, "uploadChunk");
String pageCount = getOptionValue(options, "pageCount"); int userInputtedPageCount=0;
if(!pageCount.isEmpty()) {
userInputtedPageCount = Integer.parseInt(pageCount);
}
if(!uploadChunk.isEmpty() && uploadChunk.toLowerCase().contains("true")) {
uploadSnapshotRequest.setUploadChunk("true");
} else {
uploadSnapshotRequest.setUploadChunk("false");
}
String navBarHeight = getOptionValue(options, "navigationBarHeight");
String statusBarHeight = getOptionValue(options, "statusBarHeight");
if(!navBarHeight.isEmpty()) {
uploadSnapshotRequest.setNavigationBarHeight(navBarHeight);
}
if(!statusBarHeight.isEmpty()) {
uploadSnapshotRequest.setStatusBarHeight(statusBarHeight);
}
String cropFooter = getOptionValue(options, "cropFooter");
if (!cropFooter.isEmpty()) {
uploadSnapshotRequest.setCropFooter(cropFooter.toLowerCase());
}
String cropStatusBar = getOptionValue(options, "cropStatusBar");
if (!cropStatusBar.isEmpty()) {
uploadSnapshotRequest.setCropStatusBar(cropStatusBar.toLowerCase());
}
String fullPage = getOptionValue(options, "fullPage").toLowerCase();
if(!Boolean.parseBoolean(fullPage)){
if(!pageCount.isEmpty()){
throw new IllegalArgumentException(Constants.Errors.PAGE_COUNT_ERROR);
}
TakesScreenshot takesScreenshot = (TakesScreenshot) driver;
File screenshot = takesScreenshot.getScreenshotAs(OutputType.FILE);
log.info("Screenshot captured: " + screenshotName);
uploadSnapshotRequest.setFullPage("false");
util.uploadScreenshot(screenshot, uploadSnapshotRequest, this.buildData);
} else {
uploadSnapshotRequest.setFullPage("true");
FullPageScreenshotUtil fullPageCapture = new FullPageScreenshotUtil(driver, screenshotName);
List<File> ssDir = fullPageCapture.captureFullPage(userInputtedPageCount);
if(ssDir.isEmpty()){
throw new RuntimeException(Constants.Errors.SMARTUI_SNAPSHOT_FAILED);
}
int pageCountInSsDir = ssDir.size(); int i;
if(pageCountInSsDir == 1) { //when page count is set to 1 as user for fullPage
uploadSnapshotRequest.setFullPage("false");
util.uploadScreenshot(ssDir.get(0), uploadSnapshotRequest, this.buildData);
return;
}
for( i = 0; i < pageCountInSsDir -1; ++i){
uploadSnapshotRequest.setIsLastChunk("false");
uploadSnapshotRequest.setChunkCount(i);
util.uploadScreenshot(ssDir.get(i), uploadSnapshotRequest, this.buildData);
}
uploadSnapshotRequest.setIsLastChunk("true");
uploadSnapshotRequest.setChunkCount(i);
util.uploadScreenshot(ssDir.get(pageCountInSsDir-1), uploadSnapshotRequest, this.buildData);
}
} 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());
}
}
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);
}
}
} 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());
}
}
}