Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced to generate and support text based Xpaths #256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/main/java/com/epam/healenium/SelectorComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/epam/healenium/service/HealingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -87,6 +88,15 @@ protected HealedElement toLocator(Scored<Node> node, Context context) {
for (Set<SelectorComponent> detailLevel : selectorDetailLevels) {
By locator = construct(node.getValue(), detailLevel);
List<WebElement> elements = driver.findElements(locator);
if (elements.size() == 1 && !context.getElementIds().contains(((RemoteWebElement) elements.get(0)).getId())) {
Scored<By> 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<By> byScored = new Scored<>(node.getScore(), locator);
context.getElementIds().add(((RemoteWebElement) elements.get(0)).getId());
Expand Down Expand Up @@ -126,4 +136,18 @@ protected By construct(Node node, Set<SelectorComponent> 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<SelectorComponent> detailLevel,String attribute) {
return By.xpath("//"+node.getTag()+"["+attribute+"()='"+detailLevel.stream()
.map(component -> component.createComponent(node))
.collect(Collectors.joining())+"']");
}
}