Skip to content

Commit 6f21e0d

Browse files
committed
Implemented shadowRoot functionality wrapper
1 parent 8f3e389 commit 6f21e0d

File tree

12 files changed

+492
-12
lines changed

12 files changed

+492
-12
lines changed

src/main/java/aquality/selenium/browser/JavaScript.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public enum JavaScript {
4040
GET_VIEWPORT_COORDINATES("getViewPortCoordinates.js"),
4141
GET_SCREEN_OFFSET("getScreenOffset.js"),
4242
OPEN_IN_NEW_TAB("openInNewTab.js"),
43-
OPEN_NEW_TAB("openNewTab.js");
43+
OPEN_NEW_TAB("openNewTab.js"),
44+
EXPAND_SHADOW_ROOT("expandShadowRoot.js");
4445

4546
private final String filename;
4647

@@ -55,11 +56,12 @@ public enum JavaScript {
5556
*/
5657
public String getScript() {
5758
URL scriptFile = getClass().getResource("/js/" + filename);
58-
try {
59-
InputStream stream = scriptFile.openStream();
60-
return IOUtils.toString(stream, StandardCharsets.UTF_8.name());
61-
} catch (IOException e) {
62-
Logger.getInstance().fatal(format("Couldn't find the script \"%s\"", filename), e);
59+
if (scriptFile != null) {
60+
try (InputStream stream = scriptFile.openStream()) {
61+
return IOUtils.toString(stream, StandardCharsets.UTF_8.name());
62+
} catch (IOException e) {
63+
Logger.getInstance().fatal(format("Couldn't find the script \"%s\"", filename), e);
64+
}
6365
}
6466
return "";
6567
}

src/main/java/aquality/selenium/elements/Element.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.openqa.selenium.By;
2121
import org.openqa.selenium.Keys;
2222
import org.openqa.selenium.NoSuchElementException;
23+
import org.openqa.selenium.SearchContext;
2324
import org.openqa.selenium.remote.RemoteWebElement;
2425

2526
import java.time.Duration;
@@ -28,6 +29,8 @@
2829
* Abstract class, describing wrapper of WebElement.
2930
*/
3031
public abstract class Element extends aquality.selenium.core.elements.Element implements IElement {
32+
private IElementFinder elementFinder;
33+
3134
/**
3235
* The main constructor
3336
*
@@ -51,7 +54,14 @@ protected IElementFactory getElementFactory() {
5154

5255
@Override
5356
protected IElementFinder getElementFinder() {
54-
return AqualityServices.get(IElementFinder.class);
57+
if (elementFinder == null) {
58+
elementFinder = AqualityServices.get(IElementFinder.class);
59+
}
60+
return elementFinder;
61+
}
62+
63+
void setElementFinder(IElementFinder elementFinder) {
64+
this.elementFinder = elementFinder;
5565
}
5666

5767
@Override
@@ -176,4 +186,10 @@ public void sendKeys(Keys key) {
176186
logElementAction("loc.text.sending.key", Keys.class.getSimpleName().concat(".").concat(key.name()));
177187
doWithRetry(() -> getElement().sendKeys(key));
178188
}
189+
190+
@Override
191+
public SearchContext expandShadowRoot() {
192+
logElementAction("loc.shadowroot.expand");
193+
return getElement().getShadowRoot();
194+
}
179195
}

src/main/java/aquality/selenium/elements/ElementFactory.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import aquality.selenium.browser.AqualityServices;
44
import aquality.selenium.browser.JavaScript;
55
import aquality.selenium.core.elements.interfaces.IElementFinder;
6+
import aquality.selenium.core.elements.interfaces.IElementSupplier;
67
import aquality.selenium.core.localization.ILocalizationManager;
78
import aquality.selenium.core.waitings.IConditionalWait;
89
import aquality.selenium.elements.interfaces.*;
@@ -19,9 +20,12 @@
1920

2021
public class ElementFactory extends aquality.selenium.core.elements.ElementFactory implements IElementFactory {
2122

23+
private final IElementFinder elementFinder;
24+
2225
@Inject
2326
public ElementFactory(IConditionalWait conditionalWait, IElementFinder elementFinder, ILocalizationManager localizationManager) {
2427
super(conditionalWait, elementFinder, localizationManager);
28+
this.elementFinder = elementFinder;
2529
}
2630

2731
private static Map<Class<? extends By>, String> getLocatorToXPathTemplateMap() {
@@ -92,4 +96,16 @@ protected String extractXPathFromLocator(By locator) {
9296
? String.format(getLocatorToXPathTemplateMap().get(locatorClass), locValuableString)
9397
: super.extractXPathFromLocator(locator);
9498
}
99+
100+
@Override
101+
protected <T extends aquality.selenium.core.elements.interfaces.IElement> IElementSupplier<T> getDefaultElementSupplier(Class<T> clazz) {
102+
IElementSupplier<T> baseSupplier = super.getDefaultElementSupplier(clazz);
103+
return (locator, name, state) -> {
104+
T element = baseSupplier.get(locator, name, state);
105+
if (element instanceof Element) {
106+
((Element)element).setElementFinder(elementFinder);
107+
}
108+
return element;
109+
};
110+
}
95111
}

src/main/java/aquality/selenium/elements/actions/JsActions.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import aquality.selenium.core.utilities.IElementActionRetrier;
77
import aquality.selenium.elements.HighlightState;
88
import aquality.selenium.elements.interfaces.IElement;
9+
import aquality.selenium.elements.interfaces.IShadowRootExpander;
910
import org.openqa.selenium.Point;
11+
import org.openqa.selenium.SearchContext;
1012

1113
import java.util.ArrayList;
1214
import java.util.List;
1315

14-
public class JsActions {
16+
public class JsActions implements IShadowRootExpander {
1517

1618
protected IElement element;
1719
protected String type;
@@ -23,6 +25,12 @@ public JsActions(IElement element, String type) {
2325
this.name = element.getName();
2426
}
2527

28+
@Override
29+
public SearchContext expandShadowRoot() {
30+
logElementAction("loc.shadowroot.expand.js");
31+
return (SearchContext) executeScript(JavaScript.EXPAND_SHADOW_ROOT, element);
32+
}
33+
2634
/**
2735
* Click via JS.
2836
*/

src/main/java/aquality/selenium/elements/interfaces/IElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import java.util.List;
1313

14-
public interface IElement extends aquality.selenium.core.elements.interfaces.IElement {
14+
public interface IElement extends aquality.selenium.core.elements.interfaces.IElement, IShadowRootExpander {
1515

1616
/**
1717
* Send keys.

0 commit comments

Comments
 (0)