Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,3 +22,5 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.idea
*.iml
2 changes: 1 addition & 1 deletion README.md
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
# ApplitoolsTestResultsHandler - Java
### v2.0.3
### v2.0.4

The Applitools Test Results Handler extends the capabilities of TestResults with additional API calls.
With these additional API calls you will be able to retrive additional details at the end of the test.
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -92,16 +92,14 @@ private String preparePath(String Path) {
public ApplitoolsTestResultsHandler(TestResults testResults, String viewKey, String proxyServer, String proxyPort, String proxyUser, String proxyPassword) throws Exception {

if ((proxyServer != null) && (proxyPort != null)) {
proxy = new HttpHost(proxyServer, Integer.parseInt(proxyPort));
if ((proxyPassword != null) && (proxyUser != null)) {
Credentials credentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);
AuthScope authScope = new AuthScope(proxyServer, Integer.parseInt(proxyPort));
credsProvider = new BasicCredentialsProvider();

credsProvider.setCredentials(authScope, credentials);
}
else {
proxy = new HttpHost(proxyServer, Integer.parseInt(proxyPort));
}
}
this.applitoolsViewKey = viewKey;
this.testResults = testResults;
Expand DownExpand Up@@ -263,25 +261,14 @@ private String[] calculateStepsNames() throws Exception {
return StepsNames;
}

private CloseableHttpClient getCloseableHttpClient() {
CloseableHttpClient client = null;
if (proxy != null)
client = HttpClientBuilder.create().setProxy(proxy).build();
else if (credsProvider != null)
client = HttpClientBuilder.create().setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
else
client = HttpClientBuilder.create().build();
return client;
}

private String readJsonStringFromUrl(String url) throws Exception {

HttpsURLConnection.setDefaultSSLSocketFactory(new sun.security.ssl.SSLSocketFactoryImpl());
CloseableHttpResponse response = null;
HttpGet get = new HttpGet(url);

CloseableHttpClient client = getCloseableHttpClient();
response = runLongRequest(get);
response = runLongRequest(client, get);
InputStream is = response.getEntity().getContent();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
Expand DownExpand Up@@ -310,7 +297,7 @@ private String postJsonToURL(String url, String payload) throws Exception {

CloseableHttpClient client = getCloseableHttpClient();

response = runLongRequest(post);
response = runLongRequest(client, post);

InputStream is = response.getEntity().getContent();
try {
Expand DownExpand Up@@ -355,7 +342,7 @@ else if (type == "Diff")
CloseableHttpResponse response = null;
HttpGet get = new HttpGet(urls[i].toString());
CloseableHttpClient client = getCloseableHttpClient();
response = runLongRequest(get);
response = runLongRequest(client, get);
InputStream is = response.getEntity().getContent();
try {
BufferedImage image = ImageIO.read(is);
Expand DownExpand Up@@ -454,7 +441,7 @@ private void saveImagesInFolder(String path, String imageType, URL[] imageURLS)
HttpGet get = new HttpGet(imageURLS[i].toString());
CloseableHttpClient client = getCloseableHttpClient();

response = runLongRequest(get);
response = runLongRequest(client, get);
InputStream is = response.getEntity().getContent();
try {
BufferedImage bi = ImageIO.read(is);
Expand DownExpand Up@@ -586,7 +573,7 @@ private String getSessionInfo(String sessionId, String batchId) throws IOExcepti
CloseableHttpClient client = getCloseableHttpClient();

CloseableHttpResponse response = null;
response = runLongRequest(get);
response = runLongRequest(client, get);
InputStream stream = response.getEntity().getContent();

try {
Expand DownExpand Up@@ -830,17 +817,16 @@ public String getHostingApp() throws JSONException {
return this.testData.getJSONObject("startInfo").getJSONObject("environment").optString("hostingApp");
}

public CloseableHttpResponse runLongRequest(HttpRequestBase apiCall) throws InterruptedException {
public CloseableHttpResponse runLongRequest(CloseableHttpClient client, HttpRequestBase apiCall) throws InterruptedException {
HttpRequestBase requestBase = createHttpRequest(apiCall);
CloseableHttpResponse response = sendRequest(requestBase, 1, false);
CloseableHttpResponse response = sendRequest(client, requestBase, 1, false);
return longRequestCheckStatus(response);
}

public CloseableHttpResponse sendRequest(HttpRequestBase apiCall, int retry, boolean delayBeforeRetry) throws InterruptedException {
public CloseableHttpResponse sendRequest(CloseableHttpClient client, HttpRequestBase apiCall, int retry, boolean delayBeforeRetry) throws InterruptedException {
counter += 1;
String requestId = counter + "--" + UUID.randomUUID();
apiCall.addHeader("x-applitools-eyes-client-request-id", requestId);
CloseableHttpClient client = HttpClientBuilder.create().build();
CloseableHttpResponse response;

try {
Expand All@@ -853,9 +839,9 @@ public CloseableHttpResponse sendRequest(HttpRequestBase apiCall, int retry, boo
if (retry > 0) {
if (delayBeforeRetry) {
Thread.sleep(RETRY_REQUEST_INTERVAL);
return sendRequest(apiCall, retry - 1, delayBeforeRetry);
return sendRequest(client, apiCall, retry - 1, delayBeforeRetry);
}
return sendRequest(apiCall, retry - 1, delayBeforeRetry);
return sendRequest(client, apiCall, retry - 1, delayBeforeRetry);
}
throw new Error(errorMessage);
}
Expand DownExpand Up@@ -883,7 +869,7 @@ public CloseableHttpResponse longRequestCheckStatus(CloseableHttpResponse respon
URI = responseReceived.getFirstHeader("Location").getValue() + "?apiKey=" + this.applitoolsViewKey;
request = new HttpDelete(URI);
request = createHttpRequest(request);
return sendRequest(request, 1, false);
return sendRequest(getCloseableHttpClient(), request, 1, false);
case HttpStatus.SC_GONE:
throw new Error("The server task is gone");
default:
Expand All@@ -898,14 +884,24 @@ public CloseableHttpResponse longRequestLoop(HttpRequestBase options, int delay)
System.out.println("Still running... Retrying in " + delay);

Thread.sleep(delay);
CloseableHttpResponse response = sendRequest(options, 1, false);
CloseableHttpResponse response = sendRequest(getCloseableHttpClient(), options, 1, false);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return longRequestLoop(options, delay);
}
return response;
}


private CloseableHttpClient getCloseableHttpClient() {
CloseableHttpClient client;
if (proxy == null) {
client = HttpClientBuilder.create().build();
} else if (credsProvider == null) {
client = HttpClientBuilder.create().setProxy(proxy).build();
} else {
client = HttpClientBuilder.create().setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
}
return client;
}

public HttpRequestBase createHttpRequest(HttpRequestBase apiCall) {

Expand Down