Skip to content

Commit

Permalink
Merge pull request #269 from healenium/feature/EPM-HLM-X01
Browse files Browse the repository at this point in the history
EPM-HLM Adjust logs
  • Loading branch information
Alex-Reif authored Apr 1, 2024
2 parents b10b357 + da2d2f7 commit 53ae46a
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 96 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Self-healing library for Selenium Web-based tests
for Gradle projects:
```
dependencies {
compile group: 'com.epam.healenium', name: 'healenium-web', version: '3.5.0'
compile group: 'com.epam.healenium', name: 'healenium-web', version: '3.5.1'
}
```

Expand All @@ -22,7 +22,7 @@ for Maven projects:
<dependency>
<groupId>com.epam.healenium</groupId>
<artifactId>healenium-web</artifactId>
<version>3.5.0</version>
<version>3.5.1</version>
</dependency>
```
### 1. Init driver instance of SelfHealingDriver
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.epam.healenium</groupId>
<artifactId>healenium-web</artifactId>
<version>3.5.0</version>
<version>3.5.1</version>
<packaging>jar</packaging>
<name>healenium-web</name>
<description>healenium web client</description>
Expand Down Expand Up @@ -52,7 +52,7 @@
<maven.apache>3.8.4</maven.apache>
<typesafe.version>1.4.2</typesafe.version>
<treecomparing.version>0.4.13</treecomparing.version>
<selenium.version>4.17.0</selenium.version>
<selenium.version>4.18.1</selenium.version>
<jacksondatabind.version>2.15.2</jacksondatabind.version>
<commonslang3.version>3.12.0</commonslang3.version>
<commonscodec.version>1.15</commonscodec.version>
Expand All @@ -66,7 +66,7 @@
<selenide.version>7.0.0</selenide.version>
<testcontainersjunit.version>1.16.3</testcontainersjunit.version>
<junit.version>5.8.2</junit.version>
<webdrivermanager.version>5.5.3</webdrivermanager.version>
<webdrivermanager.version>5.6.4</webdrivermanager.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/epam/healenium/SelfHealingEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -95,24 +94,21 @@ public SelfHealingEngine(@NotNull WebDriver delegate) {

public void saveElements(Context context, List<WebElement> webElements) {
try {
String by = context.getBy().toString();
List<String> ids = context.getElementIds();
Map<String, List<String>> sessionSelectors = sessionContext.getSessionSelectors();
List<String> storedIds = sessionSelectors.get(by);
if (storedIds == null || (!storedIds.containsAll(ids) && storedIds.size() != ids.size())) {
sessionSelectors.put(by, ids);
if (!webElements.isEmpty()) {
List<List<Node>> nodePath = getNodePath(webElements, context);
if (context.getCurrentUrl() == null) {
context.setCurrentUrl(getCurrentUrl());
}

RequestDto requestDto = client.getMapper().buildDto(context.getBy(), context.getAction(), context.getCurrentUrl());
requestDto.setSessionId(((RemoteWebDriver) webDriver).getSessionId().toString());
requestDto.setNodePath(nodePath);

client.saveElements(requestDto);
}
} catch (Exception e) {
log.warn("[Save Elements] Error during save elements: {}. Message: {}. Exception: {}",
context.getElementIds(), e.getMessage(), e);
context.getElementIds(), e.getMessage(), e.toString());
}
}

Expand Down
25 changes: 13 additions & 12 deletions src/main/java/com/epam/healenium/client/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ public void saveElements(RequestDto requestDto) {
request.setHeader("Content-Length", String.valueOf(data.length));
request.setHeader("Content-Type", JSON_UTF_8);
request.setContent(Contents.bytes(data));
log.debug("[Save Elements] Request: {}. Request body: {}", request, requestDto);
log.debug("[Save Elements] By: {}, Locator: {}, Command: {}, URL: {}",
requestDto.getType(), requestDto.getLocator(), requestDto.getCommand(), requestDto.getUrl());
serverHttpClient.execute(request);
} catch (Exception e) {
log.warn("[Save Elements] Error during call. Message: {}, Exception: {}", e.getMessage(), e);
log.warn("[Save Elements] Error during call. Message: {}, Exception: {}", e.getMessage(), e.toString());
}
}

Expand All @@ -140,11 +141,10 @@ public ConfigSelectorDto getElements() {
Supplier<InputStream> result = response.getContent();
configSelectorDto = objectMapper.readValue(result.get(), new TypeReference<ConfigSelectorDto>() {
});
log.debug("[Get Elements] Response: {}", configSelectorDto);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
log.warn("[Get Elements] Error during call. Message: {}, Exception: {}", e.getMessage(), e);
log.warn("[Get Elements] Error during call. Message: {}, Exception: {}", e.getMessage(), e.toString());
}
return configSelectorDto;
}
Expand Down Expand Up @@ -176,10 +176,12 @@ public void healRequest(Context context) {
request.setHeader("sessionKey", sessionKey);
request.setHeader("hostProject", SystemUtils.getHostProjectName());
request.setContent(Contents.bytes(data));
log.debug("[Heal Element] Request: {}. Request body: {}", request, requestDtos);
for (RequestDto requestDto : requestDtos) {
log.debug("[Save Healed Elements] {}", requestDto.getUsedResult().getLocator());
}
serverHttpClient.execute(request);
} catch (Exception e) {
log.warn("[Heal Element] Error during call. Message: {}. Exception: {}", e.getMessage(), e);
log.warn("[Heal Element] Error during call. Message: {}. Exception: {}", e.getMessage(), e.toString());
}
}

Expand All @@ -203,16 +205,16 @@ public Optional<ReferenceElementsDto> getReferenceElements(By locator, String co
.addQueryParameter("methodName", requestDto.getMethodName())
.addQueryParameter("command", requestDto.getCommand())
.addQueryParameter("url", currentUrl);
log.debug("[Get Reference Elements] Request: {}", request);
log.debug("[Get Reference Elements] Request. Locator: {}, Command: {}, Url: {}",
requestDto.getLocator(), requestDto.getCommand(), currentUrl);
HttpResponse response = serverHttpClient.execute(request);
if (response.getStatus() == 200) {
Supplier<InputStream> result = response.getContent();
referenceElementsDto = objectMapper.readValue(result.get(), new TypeReference<ReferenceElementsDto>() {
});
}
log.debug("[Get Reference Elements] Response: {}", referenceElementsDto);
} catch (Exception e) {
log.warn("[Get Reference Elements] Error during call. Message: {}. Exception: {}", e.getMessage(), e);
log.warn("[Get Reference Elements] Error during call. Message: {}. Exception: {}", e.getMessage(), e.toString());
}
return Optional.ofNullable(referenceElementsDto);
}
Expand All @@ -231,7 +233,6 @@ public List<Locator> imitate(SelectorImitatorDto selectorImitatorDto) {
request.setHeader("Content-Length", String.valueOf(data.length));
request.setHeader("Content-Type", JSON_UTF_8);
request.setContent(Contents.bytes(data));
log.debug("[Selector Imitate] Request: {}. Request body: {}", request, content);
HttpResponse response = imitateHttpClient.execute(request);

if (response.getStatus() == 200) {
Expand All @@ -242,7 +243,7 @@ public List<Locator> imitate(SelectorImitatorDto selectorImitatorDto) {
return locators;
}
} catch (Exception e) {
log.warn("[Selector Imitate] Error during call. Message: {}, Exception: {}", e.getMessage(), e);
log.warn("[Selector Imitate] Error during call. Message: {}, Exception: {}", e.getMessage(), e.toString());
}
return Collections.emptyList();
}
Expand All @@ -254,7 +255,7 @@ public void initReport(String sessionId) {
log.debug("[Init Report] Request: {}", request);
serverHttpClient.execute(request);
} catch (Exception e) {
log.warn("[Init Report] Error during call. Message: {}, Exception: {}", e.getMessage(), e);
log.warn("[Init Report] Error during call. Message: {}, Exception: {}", e.getMessage(), e.toString());
}
}
}
60 changes: 0 additions & 60 deletions src/main/java/com/epam/healenium/client/callback/HttpCallback.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public WebElement findElement(By by) {
if (context.getElements().size() > 0) {
return context.getElements().get(0);
}
throw context.getNoSuchElementException();
if (context.getNoSuchElementException() != null) {
throw context.getNoSuchElementException();
} else {
throw new NoSuchElementException("Failed to find element using " + by.toString());
}
}
return driver.findElement(by);
} catch (NoSuchElementException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.epam.healenium.treecomparing.Node;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.List;
Expand All @@ -12,7 +11,6 @@
public class ReferenceElementsDto {

private String pageContent;
@ToString.Exclude
private List<List<Node>> paths;
private List<Locator> unsuccessfulLocators;
}
2 changes: 0 additions & 2 deletions src/main/java/com/epam/healenium/model/SessionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;

Expand All @@ -17,7 +16,6 @@ public class SessionContext {
private String sessionId;
private Map<String, String> enableHealingElements = new HashMap<>();
private Map<String, String> disableHealingElement = new HashMap<>();
private Map<String, List<String>> sessionSelectors = new HashMap<>();
private BiFunction<SelfHealingEngine, String, String> functionUrl;
private boolean waitCommand = false;
private boolean findElementWaitCommand = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ public boolean validate() {

@Override
public void execute() {
log.warn("Failed to find an element using locator {}", context.getBy().toString());
if (context.getNoSuchElementException() != null) {
log.warn("Failed to find an element using locator {}", context.getBy().toString());
log.warn("Reason: {}", context.getNoSuchElementException().getMessage());
}
log.warn("Trying to heal...");
populateUrlKey();
ReferenceElementsDto referenceElementsDto = restClient.getReferenceElements(
context.getBy(), context.getAction(), context.getCurrentUrl())
Expand All @@ -75,7 +74,6 @@ private void populateUrlKey() {
context.setCurrentUrl(engine.getCurrentUrl());
}
String urlKey = engine.getSessionContext().getFunctionUrl().apply(engine, context.getCurrentUrl());
log.debug("[Find Element] Get reference element. UrlKey: {}", urlKey);
context.setUrlKey(urlKey);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.openqa.selenium.WebElement;

import java.util.List;
import java.util.stream.Collectors;

/**
* Healing Elements Processor
Expand Down Expand Up @@ -42,9 +43,15 @@ public void execute() {
Node destination = engine.parseTree(targetPage);
context.setPageContent(targetPage);

nodesFromDb.stream()
.filter(nodes -> !context.getNewElementsToNodes().containsValue(nodes) && !context.getExistElementsToNodes().containsValue(nodes))
.forEach(nodes -> healingService.findNewLocations(nodes, destination, context));
List<List<Node>> nodesToHeal = nodesFromDb.stream()
.filter(nodes -> !context.getNewElementsToNodes().containsValue(nodes)
&& !context.getExistElementsToNodes().containsValue(nodes))
.collect(Collectors.toList());
if (!nodesToHeal.isEmpty()) {
log.warn("Failed to find an elements using locator {}", context.getBy().toString());
log.warn("Trying to heal...");
}
nodesToHeal.forEach(nodes -> healingService.findNewLocations(nodes, destination, context));
}

private void splitDbNodes(List<List<Node>> nodesFromDb) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void execute() {
Node destination = engine.parseTree(targetPage);
context.setPageContent(targetPage);

log.warn("Trying to heal...");
for (List<Node> nodes : context.getReferenceElementsDto().getPaths()) {
healingService.findNewLocations(nodes, destination, context);
}
Expand Down

0 comments on commit 53ae46a

Please sign in to comment.