Skip to content

Commit

Permalink
Merge pull request #240 from healenium/feature/EPMHLM-340
Browse files Browse the repository at this point in the history
EPMHLM-340
  • Loading branch information
Alex-Reif authored Aug 9, 2023
2 parents 5b1e049 + 9928b80 commit 79308c4
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 23 deletions.
10 changes: 5 additions & 5 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.4.3</version>
<version>3.4.4</version>
<packaging>jar</packaging>
<name>healenium-web</name>
<description>healenium web client</description>
Expand Down Expand Up @@ -57,17 +57,17 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.apache>3.8.4</maven.apache>
<typesafe.version>1.4.2</typesafe.version>
<treecomparing.version>0.4.11</treecomparing.version>
<selenium.version>4.5.0</selenium.version>
<jacksondatabind.version>2.13.4.1</jacksondatabind.version>
<treecomparing.version>0.4.12</treecomparing.version>
<selenium.version>4.10.0</selenium.version>
<jacksondatabind.version>2.15.2</jacksondatabind.version>
<commonslang3.version>3.12.0</commonslang3.version>
<commonscodec.version>1.15</commonscodec.version>
<okhttp3.version>4.9.3</okhttp3.version>
<annotations.version>23.0.0</annotations.version>
<mapstruct.version>1.4.2.Final</mapstruct.version>
<lombok.version>1.18.22</lombok.version>
<streamex.version>0.8.1</streamex.version>
<jettyserver.version>10.0.10 </jettyserver.version>
<jettyserver.version>11.0.15 </jettyserver.version>
<hamcrestcore.version>2.2</hamcrestcore.version>
<selenide.version>6.1.1</selenide.version>
<testcontainersjunit.version>1.16.3</testcontainersjunit.version>
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/epam/healenium/SelfHealingDriverWait.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.epam.healenium;

import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Sleeper;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Clock;
import java.time.Duration;
import java.util.function.Function;

public class SelfHealingDriverWait extends WebDriverWait {

private final WebDriver webDriver;

public SelfHealingDriverWait(WebDriver driver, Duration timeout) {
super(driver, timeout);
this.webDriver = driver;
}

public SelfHealingDriverWait(WebDriver driver, Duration timeout, Duration sleep) {
super(driver, timeout, sleep);
this.webDriver = driver;
}

public SelfHealingDriverWait(WebDriver driver, Duration timeout, Duration sleep, Clock clock, Sleeper sleeper) {
super(driver, timeout, sleep, clock, sleeper);
this.webDriver = driver;
}

@Override
public <V> V until(Function<? super WebDriver, V> isTrue) {
if (webDriver instanceof SelfHealingDriver) {
SelfHealingDriver hlmDriver = (SelfHealingDriver) webDriver;
try {
hlmDriver.getCurrentEngine().getSessionContext().setWaitCommand(true);
V until = super.until(isTrue);
hlmDriver.getCurrentEngine().getSessionContext().setWaitCommand(false);
return until;
} catch (TimeoutException timeoutException) {
if (hlmDriver.getCurrentEngine().getSessionContext().isFindElementWaitCommand()) {
hlmDriver.getCurrentEngine().getSessionContext().setWaitCommand(false);
hlmDriver.getCurrentEngine().getSessionContext().setFindElementWaitCommand(false);
V value = isTrue.apply(webDriver);
if (value != null && (Boolean.class != value.getClass() || Boolean.TRUE.equals(value))) {
return value;
}
}
throw timeoutException;
} catch (Exception ex) {
hlmDriver.getCurrentEngine().getSessionContext().setWaitCommand(false);
throw ex;
}
} else {
return super.until(isTrue);
}
}
}



16 changes: 8 additions & 8 deletions src/main/java/com/epam/healenium/SelfHealingEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ public void saveElements(Context context, List<WebElement> webElements) {
List<String> storedIds = sessionSelectors.get(by);
if (storedIds == null || (!storedIds.containsAll(ids) && storedIds.size() != ids.size())) {
sessionSelectors.put(by, ids);
RequestDto requestDto = client.getMapper().buildDto(context.getBy(), context.getAction(), null);
requestDto.setElementIds(ids);
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(getNodePath(webElements));
String currentUrl = getCurrentUrl();
context.setCurrentUrl(currentUrl);
requestDto.setUrl(currentUrl);
requestDto.setNodePath(nodePath);
client.saveElements(requestDto);
}
} catch (Exception e) {
Expand All @@ -116,9 +116,9 @@ public void saveElements(Context context, List<WebElement> webElements) {
}
}

public List<List<Node>> getNodePath(List<WebElement> webElements) {
public List<List<Node>> getNodePath(List<WebElement> webElements, Context context) {
return webElements.stream()
.map(e -> nodeService.getNodePath(webDriver, e))
.map(e -> nodeService.getNodePath(webDriver, e, context))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public BaseHandler(SelfHealingEngine engine) {
@Override
public WebElement findElement(By by) {
try {
if (engine.getSessionContext().isWaitCommand()) {
engine.getSessionContext().setFindElementWaitCommand(true);
}
if (engine.isHealingEnabled()) {
Context context = new Context()
.setBy(by)
Expand Down Expand Up @@ -82,6 +85,9 @@ public WebElement findElement(By by) {
@Override
public List<WebElement> findElements(By by) {
try {
if (engine.getSessionContext().isWaitCommand()) {
engine.getSessionContext().setFindElementWaitCommand(true);
}
if (engine.isHealingEnabled()) {
Context context = new Context()
.setBy(by)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
@Override
public WebElement findElement(By by) {
try {
if (engine.getSessionContext().isWaitCommand()) {
engine.getSessionContext().setFindElementWaitCommand(true);
}
PageAwareBy pageBy = awareBy(by);
if (engine.isHealingEnabled()) {
Context context = new Context()
Expand All @@ -96,6 +99,9 @@ public WebElement findElement(By by) {
@Override
public List<WebElement> findElements(By by) {
try {
if (engine.getSessionContext().isWaitCommand()) {
engine.getSessionContext().setFindElementWaitCommand(true);
}
PageAwareBy pageBy = awareBy(by);
By inner = pageBy.getBy();
if (engine.isHealingEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

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

private String pageContent;
@ToString.Exclude
private List<List<Node>> paths;
private List<Locator> unsuccessfulLocators;
}
2 changes: 2 additions & 0 deletions src/main/java/com/epam/healenium/model/SessionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public class SessionContext {
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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public void execute() {
engine.saveElements(context, Collections.singletonList(element));
context.getElements().add(element);
} catch (NoSuchElementException e) {
if (engine.getSessionContext().isWaitCommand()) {
throw e;
}
context.setNoSuchElementException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public void execute() {
engine.saveElements(context, Collections.singletonList(element));
context.getElements().add(element);
} catch (NoSuchElementException e) {
if (engine.getSessionContext().isWaitCommand()) {
throw e;
}
context.setNoSuchElementException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public GetReferenceElementsProcessor(BaseProcessor nextProcessor) {

@Override
public boolean validate() {
if (engine.getSessionContext().isWaitCommand()) {
return false;
}
Locator locator = engine.getClient().getMapper().byToLocator(context.getBy());
Map<String, String> enableHealingSelectors = engine.getSessionContext().getEnableHealingElements();
Map<String, String> disableHealingSelector = engine.getSessionContext().getDisableHealingElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void execute() {

private void splitDbNodes(List<List<Node>> nodesFromDb) {
for (WebElement webElement : context.getElements()) {
List<Node> nodePath = engine.getNodeService().getNodePath(driver, webElement);
List<Node> nodePath = engine.getNodeService().getNodePath(driver, webElement, context);
if (!nodesFromDb.contains(nodePath)) {
context.getNewElementsToNodes().put(webElement, nodePath);
} else {
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/epam/healenium/service/NodeService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.epam.healenium.service;

import com.epam.healenium.FieldName;
import com.epam.healenium.model.Context;
import com.epam.healenium.treecomparing.Node;
import com.epam.healenium.treecomparing.NodeBuilder;
import com.epam.healenium.utils.ResourceReader;
Expand Down Expand Up @@ -33,20 +34,23 @@ public class NodeService {
/**
* build list nodes by source webElement
*
* @param driver - web driver
* @param webElement - source element
* @param driver - web driver
* @param context - find element context dto
* @return - list path nodes
*/
public List<Node> getNodePath(WebDriver driver, WebElement webElement) {
public List<Node> getNodePath(WebDriver driver, WebElement webElement, Context context) {
JavascriptExecutor executor = (JavascriptExecutor) driver;
String data = (String) executor.executeScript(SCRIPT, webElement);
List<Node> path = new LinkedList<>();

try {
ObjectMapper mapper = new ObjectMapper();
JsonNode treeNode = mapper.readTree(data);
if (treeNode.isArray()) {
for (final JsonNode jsonNode : treeNode) {
context.setCurrentUrl(treeNode.get("url").textValue());
JsonNode items = treeNode.get("items");
if (items.isArray()) {
for (final JsonNode jsonNode : items) {
Node node = toNode(mapper.treeAsTokens(jsonNode));
path.add(node);
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/epam/healenium/utils/ProxyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
package com.epam.healenium.utils;

import com.epam.healenium.SelfHealingDriver;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.stream.Stream;
import lombok.experimental.UtilityClass;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
Expand All @@ -25,6 +21,11 @@
import org.openqa.selenium.interactions.Interactive;
import org.openqa.selenium.interactions.Locatable;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.stream.Stream;

@UtilityClass
@SuppressWarnings("unchecked")
public class ProxyFactory {
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/itemsWithAttributes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

var items = [];
var result = {items: null, url: null};
var a = arguments[0];
while (a != document) {
var child = a;
Expand All @@ -25,4 +26,6 @@ while (a != document) {
items.unshift(node);
a = a.parentNode;
}
return JSON.stringify(items);
result.items = items;
result.url = window.location.href;
return JSON.stringify(result);

0 comments on commit 79308c4

Please sign in to comment.