From 847eda58846e52c2300d6d26295b55ba9dfdf2bd Mon Sep 17 00:00:00 2001 From: kherath17 Date: Wed, 22 Nov 2023 15:02:50 +0530 Subject: [PATCH] Enhanced to generate and support text based Xpaths --- .../com/epam/healenium/SelectorComponent.java | 12 +++------- .../InnerTextElementCreator.java | 10 ++++++++ .../healenium/service/HealingService.java | 24 +++++++++++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/epam/healenium/elementcreators/InnerTextElementCreator.java diff --git a/src/main/java/com/epam/healenium/SelectorComponent.java b/src/main/java/com/epam/healenium/SelectorComponent.java index 5919db1..faf1bb1 100644 --- a/src/main/java/com/epam/healenium/SelectorComponent.java +++ b/src/main/java/com/epam/healenium/SelectorComponent.java @@ -12,14 +12,7 @@ */ package com.epam.healenium; -import com.epam.healenium.elementcreators.ClassElementCreator; -import com.epam.healenium.elementcreators.PathElementCreator; -import com.epam.healenium.elementcreators.PositionElementCreator; -import com.epam.healenium.elementcreators.AttributesElementCreator; -import com.epam.healenium.elementcreators.ElementCreator; -import com.epam.healenium.elementcreators.IdElementCreator; -import com.epam.healenium.elementcreators.ParentElementCreator; -import com.epam.healenium.elementcreators.TagElementCreator; +import com.epam.healenium.elementcreators.*; import com.epam.healenium.treecomparing.Node; import lombok.AllArgsConstructor; @@ -31,7 +24,8 @@ public enum SelectorComponent { ID(new IdElementCreator()), CLASS(new ClassElementCreator()), POSITION(new PositionElementCreator()), - ATTRIBUTES(new AttributesElementCreator()); + ATTRIBUTES(new AttributesElementCreator()), + INNERTEXT(new InnerTextElementCreator()); private final ElementCreator elementCreator; diff --git a/src/main/java/com/epam/healenium/elementcreators/InnerTextElementCreator.java b/src/main/java/com/epam/healenium/elementcreators/InnerTextElementCreator.java new file mode 100644 index 0000000..393a34a --- /dev/null +++ b/src/main/java/com/epam/healenium/elementcreators/InnerTextElementCreator.java @@ -0,0 +1,10 @@ +package com.epam.healenium.elementcreators; + +import com.epam.healenium.treecomparing.Node; + +public class InnerTextElementCreator implements ElementCreator{ + @Override + public String create(Node node) { + return node.getInnerText(); + } +} diff --git a/src/main/java/com/epam/healenium/service/HealingService.java b/src/main/java/com/epam/healenium/service/HealingService.java index 1a697f1..bdf121a 100644 --- a/src/main/java/com/epam/healenium/service/HealingService.java +++ b/src/main/java/com/epam/healenium/service/HealingService.java @@ -44,6 +44,7 @@ public class HealingService { add(EnumSet.of(SelectorComponent.PARENT, SelectorComponent.TAG, SelectorComponent.CLASS, SelectorComponent.POSITION)); add(EnumSet.of(SelectorComponent.PARENT, SelectorComponent.TAG, SelectorComponent.ID, SelectorComponent.CLASS, SelectorComponent.ATTRIBUTES)); add(EnumSet.of(SelectorComponent.PATH)); + add(EnumSet.of(SelectorComponent.INNERTEXT)); }}; public HealingService(Config finalizedConfig, WebDriver driver) { @@ -87,6 +88,15 @@ protected HealedElement toLocator(Scored node, Context context) { for (Set detailLevel : selectorDetailLevels) { By locator = construct(node.getValue(), detailLevel); List elements = driver.findElements(locator); + if (elements.size() == 1 && !context.getElementIds().contains(((RemoteWebElement) elements.get(0)).getId())) { + Scored byScored = new Scored<>(node.getScore(), locator); + context.getElementIds().add(((RemoteWebElement) elements.get(0)).getId()); + HealedElement healedElement = new HealedElement(); + healedElement.setElement(elements.get(0)).setScored(byScored); + return healedElement; + }else + locator=constructXPath(node.getValue(), selectorDetailLevels.get(6),"text"); + elements = driver.findElements(locator); if (elements.size() == 1 && !context.getElementIds().contains(((RemoteWebElement) elements.get(0)).getId())) { Scored byScored = new Scored<>(node.getScore(), locator); context.getElementIds().add(((RemoteWebElement) elements.get(0)).getId()); @@ -126,4 +136,18 @@ protected By construct(Node node, Set detailLevel) { .map(component -> component.createComponent(node)) .collect(Collectors.joining())); } + + /** + * Construct xPath by Node + * @param node + * @param detailLevel + * @param attribute + * @return + */ + protected By constructXPath(Node node, Set detailLevel,String attribute) { + return By.xpath("//"+node.getTag()+"["+attribute+"()='"+detailLevel.stream() + .map(component -> component.createComponent(node)) + .collect(Collectors.joining())+"']"); + } } +