diff --git a/.gitignore b/.gitignore index da4867f..7c45d64 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +.idea +*.iml \ No newline at end of file diff --git a/README.md b/README.md index 7b44144..f825e59 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/main/java/ApplitoolsTestResultHandler/ApplitoolsTestResultsHandler.java b/src/main/java/ApplitoolsTestResultHandler/ApplitoolsTestResultsHandler.java index 580c7cd..cbb1204 100644 --- a/src/main/java/ApplitoolsTestResultHandler/ApplitoolsTestResultsHandler.java +++ b/src/main/java/ApplitoolsTestResultHandler/ApplitoolsTestResultsHandler.java @@ -92,6 +92,7 @@ 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)); @@ -99,9 +100,6 @@ public ApplitoolsTestResultsHandler(TestResults testResults, String viewKey, Str credsProvider.setCredentials(authScope, credentials); } - else { - proxy = new HttpHost(proxyServer, Integer.parseInt(proxyPort)); - } } this.applitoolsViewKey = viewKey; this.testResults = testResults; @@ -263,17 +261,6 @@ 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()); @@ -281,7 +268,7 @@ private String readJsonStringFromUrl(String url) throws Exception { 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"))); @@ -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 { @@ -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); @@ -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); @@ -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 { @@ -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 { @@ -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); } @@ -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: @@ -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) {