-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathVisualRegressionTracker.java
More file actions
executable file
·189 lines (162 loc) · 7.86 KB
/
VisualRegressionTracker.java
File metadata and controls
executable file
·189 lines (162 loc) · 7.86 KB
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
package io.visual_regression_tracker.sdk_java;
import com.google.gson.Gson;
import io.visual_regression_tracker.sdk_java.request.BuildRequest;
import io.visual_regression_tracker.sdk_java.request.TestRunRequest;
import io.visual_regression_tracker.sdk_java.response.BuildResponse;
import io.visual_regression_tracker.sdk_java.response.TestRunResponse;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
enum METHOD {
GET,
POST,
PATCH
}
@Slf4j
public class VisualRegressionTracker {
private static final String TRACKER_NOT_STARTED = "Visual Regression Tracker has not been started";
private static final String CONFIG_FILE_NAME = "vrt.json";
protected static final String API_KEY_HEADER = "apiKey";
protected static final String PROJECT_HEADER = "project";
protected Gson gson;
protected VisualRegressionTrackerConfig configuration;
protected PathProvider paths;
protected String buildId;
protected String projectId;
public VisualRegressionTracker() {
VisualRegressionTrackerConfig.VisualRegressionTrackerConfigBuilder configBuilder = VisualRegressionTrackerConfig.builder();
File configFile = new File(CONFIG_FILE_NAME);
if (configFile.exists()) {
configBuilder.configFile(configFile);
}
configuration = configBuilder.build();
paths = new PathProvider(configuration.getApiUrl());
gson = new Gson();
}
public VisualRegressionTracker(VisualRegressionTrackerConfig trackerConfig) {
configuration = trackerConfig;
paths = new PathProvider(trackerConfig.getApiUrl());
gson = new Gson();
}
public BuildResponse start() throws IOException, InterruptedException {
String projectName = configuration.getProject();
String branch = configuration.getBranchName();
String ciBuildId = configuration.getCiBuildId();
BuildRequest newBuild = BuildRequest.builder()
.branchName(branch)
.project(projectName)
.ciBuildId(ciBuildId)
.build();
log.info("Starting Visual Regression Tracker for project <{}> and branch <{}>", projectName, branch);
HttpRequest.BodyPublisher body = HttpRequest.BodyPublishers.ofString(gson.toJson(newBuild));
HttpResponse<String> response = getResponse(METHOD.POST, paths.getBuildPath(), body);
BuildResponse buildResponse = handleResponse(response, BuildResponse.class);
buildId = buildResponse.getId();
projectId = buildResponse.getProjectId();
log.info("Visual Regression Tracker is started for project <{}>: projectId: <{}>, buildId: <{}>, ciBuildId: <{}>",
projectName, projectId, buildId, buildResponse.getCiBuildId());
return buildResponse;
}
public BuildResponse stop() throws IOException, InterruptedException {
if (!isStarted()) {
throw new TestRunException(TRACKER_NOT_STARTED);
}
log.info("Stopping Visual Regression Tracker for buildId <{}>", buildId);
HttpRequest.BodyPublisher body = HttpRequest.BodyPublishers.ofString("{\"isRunning\":false}");
HttpResponse<String> response = getResponse(METHOD.PATCH, paths.getBuildPathForBuild(buildId), body);
BuildResponse vrtStopResponse = handleResponse(response, BuildResponse.class);
log.info("Visual Regression Tracker is stopped for buildId <{}>", buildId);
return vrtStopResponse;
}
public TestRunResult track(String name, String imageBase64, TestRunOptions testRunOptions)
throws IOException, InterruptedException {
log.info("Tracking test run <{}> with options <{}> for buildId <{}>", name, testRunOptions, buildId);
TestRunResponse testResultDTO = submitTestRun(name, imageBase64, testRunOptions);
String errorMessage;
switch (testResultDTO.getStatus()) {
case NEW:
errorMessage = "No baseline: ".concat(testResultDTO.getUrl());
break;
case UNRESOLVED:
errorMessage = "Difference found: ".concat(testResultDTO.getUrl());
break;
default:
errorMessage = "";
break;
}
if (!errorMessage.isEmpty()) {
if (configuration.getEnableSoftAssert()) {
log.error(errorMessage);
} else {
throw new TestRunException(errorMessage);
}
}
return new TestRunResult(testResultDTO, this.paths);
}
public TestRunResult track(String name, String imageBase64) throws IOException, InterruptedException {
return track(name, imageBase64, TestRunOptions.builder().build());
}
protected boolean isStarted() {
return buildId != null && projectId != null;
}
protected TestRunResponse submitTestRun(String name, String imageBase64,
TestRunOptions testRunOptions) throws IOException, InterruptedException {
if (!isStarted()) {
throw new TestRunException(TRACKER_NOT_STARTED);
}
TestRunRequest newTestRun = TestRunRequest.builder()
.projectId(projectId)
.buildId(buildId)
.branchName(configuration.getBranchName())
.name(name)
.imageBase64(imageBase64)
.os(testRunOptions.getOs())
.browser(testRunOptions.getBrowser())
.viewport(testRunOptions.getViewport())
.device(testRunOptions.getDevice())
.customTags(testRunOptions.getCustomTags())
.diffTollerancePercent(testRunOptions.getDiffTollerancePercent())
.ignoreAreas(testRunOptions.getIgnoreAreas())
.build();
HttpRequest.BodyPublisher body = HttpRequest.BodyPublishers.ofString(gson.toJson(newTestRun));
HttpResponse<String> response = getResponse(METHOD.POST, paths.getTestRunPath(), body);
return handleResponse(response, TestRunResponse.class);
}
private HttpResponse<String> getResponse(METHOD method, String url, HttpRequest.BodyPublisher body) throws IOException, InterruptedException {
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.timeout(Duration.ofSeconds(configuration.getHttpTimeoutInSeconds()))
.header(API_KEY_HEADER, configuration.getApiKey())
.header(PROJECT_HEADER, configuration.getProject())
.header("Content-Type", "application/json;charset=UTF-8")
.uri(URI.create(url));
HttpRequest request = getRequest(method, body, requestBuilder);
HttpResponse<String> response = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(Duration.ofSeconds(configuration.getHttpTimeoutInSeconds()))
.build()
.send(request, HttpResponse.BodyHandlers.ofString());
return response;
}
protected HttpRequest getRequest(METHOD method, HttpRequest.BodyPublisher body, HttpRequest.Builder requestBuilder) {
switch (method) {
case PATCH:
return requestBuilder.method("PATCH", body).build();
case POST:
return requestBuilder.POST(body).build();
default:
throw new UnsupportedOperationException("This method is not yet supported.");
}
}
protected <T> T handleResponse(HttpResponse<String> response, Class<T> classOfT) {
String responseBody = response.body();
if (!String.valueOf(response.statusCode()).startsWith("2")) {
throw new TestRunException(responseBody);
}
return gson.fromJson(responseBody, classOfT);
}
}