diff --git a/src/main/java/com/epam/healenium/client/RestClient.java b/src/main/java/com/epam/healenium/client/RestClient.java index be41d4e..4af065c 100644 --- a/src/main/java/com/epam/healenium/client/RestClient.java +++ b/src/main/java/com/epam/healenium/client/RestClient.java @@ -3,7 +3,7 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,6 +19,7 @@ import com.epam.healenium.model.RequestDto; import com.epam.healenium.treecomparing.Node; import com.epam.healenium.treecomparing.Scored; +import com.epam.healenium.utils.SystemUtils; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; @@ -59,10 +60,10 @@ public RestClient(Config config) { private OkHttpClient okHttpClient() { return new OkHttpClient.Builder() - .connectTimeout(30, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .writeTimeout(30, TimeUnit.SECONDS) - .build(); + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .build(); } private ObjectMapper initMapper() { @@ -79,16 +80,16 @@ private ObjectMapper initMapper() { * Store info in backend * @param by */ - public void selectorRequest(By by, StackTraceElement element, List nodePath){ + public void selectorRequest(By by, StackTraceElement element, List nodePath) { RequestDto requestDto = mapper.buildDto(by, element, nodePath); try { RequestBody body = RequestBody.create(JSON, objectMapper.writeValueAsString(requestDto)); Request request = new Request.Builder() - .url(baseUrl) - .post(body) - .build(); + .url(baseUrl) + .post(body) + .build(); okHttpClient().newCall(request).execute(); - } catch (Exception e){ + } catch (Exception e) { log.warn("Failed to make response"); } } @@ -100,7 +101,7 @@ public void selectorRequest(By by, StackTraceElement element, List nodePat * @param page * @return */ - public void healRequest(By locator, StackTraceElement element, String page, List> choices, Scored healed, byte[] screenshot){ + public void healRequest(By locator, StackTraceElement element, String page, List> choices, Scored healed, byte[] screenshot) { RequestDto requestDto = mapper.buildDto(locator, element, page, choices, healed, screenshot); try { RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM) @@ -109,13 +110,15 @@ public void healRequest(By locator, StackTraceElement element, String page, List .build(); Request request = new Request.Builder() - .addHeader("sessionKey", sessionKey) - .url(baseUrl + "/healing") - .post(requestBody) - .build(); + .addHeader("sessionKey", sessionKey) + .addHeader("instance", SystemUtils.getHostIpAddress()) + .addHeader("hostProject", SystemUtils.getHostProjectName()) + .url(baseUrl + "/healing") + .post(requestBody) + .build(); okHttpClient().newCall(request).execute(); - } catch (Exception e){ - log.warn("Failed to make response",e); + } catch (Exception e) { + log.warn("Failed to make response", e); } } @@ -125,7 +128,7 @@ public void healRequest(By locator, StackTraceElement element, String page, List * @param element * @return */ - public Optional> getLastValidPath(By locator, StackTraceElement element){ + public Optional> getLastValidPath(By locator, StackTraceElement element) { List nodes = null; RequestDto requestDto = mapper.buildDto(locator, element); try { @@ -145,7 +148,7 @@ public Optional> getLastValidPath(By locator, StackTraceElement eleme nodes = objectMapper.readValue(result, new TypeReference>() { }); } - } catch (Exception ex){ + } catch (Exception ex) { log.warn("Failed to make response", ex); } return Optional.ofNullable(nodes); @@ -155,7 +158,7 @@ public Optional> getLastValidPath(By locator, StackTraceElement eleme * * @return */ - private String buildScreenshotName(){ + private String buildScreenshotName() { return "screenshot_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd-MMM-yyyy-hh-mm-ss").withLocale(Locale.US)) + ".png"; } } diff --git a/src/main/java/com/epam/healenium/elementcreators/AttributesElementCreator.java b/src/main/java/com/epam/healenium/elementcreators/AttributesElementCreator.java index 7e8b093..ca1bbaf 100644 --- a/src/main/java/com/epam/healenium/elementcreators/AttributesElementCreator.java +++ b/src/main/java/com/epam/healenium/elementcreators/AttributesElementCreator.java @@ -30,7 +30,7 @@ public String create(Node node) { .stream() .filter(entry -> StringUtils.isNoneBlank(entry.getKey(), entry.getValue())) .filter(entry -> !SKIPPED_ATTRIBUTES.contains(entry.getKey())) - .map(entry -> String.format("[%s=\"%s\"]", entry.getKey().trim(), entry.getValue().trim())) + .map(entry -> String.format("[%s='%s']", entry.getKey().trim(), entry.getValue().trim())) .collect(Collectors.joining()); } } diff --git a/src/main/java/com/epam/healenium/utils/SystemUtils.java b/src/main/java/com/epam/healenium/utils/SystemUtils.java new file mode 100644 index 0000000..89b9284 --- /dev/null +++ b/src/main/java/com/epam/healenium/utils/SystemUtils.java @@ -0,0 +1,39 @@ +/** + * Healenium-web Copyright (C) 2019 EPAM + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.epam.healenium.utils; + +import lombok.experimental.UtilityClass; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; + +import java.net.Inet4Address; +import java.net.UnknownHostException; + +@Slf4j +@UtilityClass +public class SystemUtils { + + public String getHostProjectName() { + String projectPath = System.getProperty("user.dir").replace("\\", "/"); + return projectPath.substring(projectPath.lastIndexOf("/") + 1); + } + + public String getHostIpAddress() { + try { + return Inet4Address.getLocalHost().getHostAddress(); + } catch (UnknownHostException ex) { + log.warn("Failed to get host address", ex); + } + return StringUtils.EMPTY; + } +}