Skip to content

Commit c2f6e80

Browse files
authored
Merge pull request #55 from AutomateThePlanet/shadow-dom-logic-update
Shadow DOM Logic Update
2 parents 3a3a8aa + fb73b5d commit c2f6e80

File tree

12 files changed

+699
-389
lines changed

12 files changed

+699
-389
lines changed

bellatrix.core/src/main/java/solutions/bellatrix/core/utilities/HtmlService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public <T> T getAttribute(Element element, String attributeName, Class<T> clazz)
9393
}
9494
}
9595

96+
@Deprecated
9697
public static String convertAbsoluteXpathToCss(String xpath) {
9798
String cssSelector = xpath.replace(NODE, CHILD_COMBINATOR);
9899

@@ -115,6 +116,7 @@ public static String convertAbsoluteXpathToCss(String xpath) {
115116
return semiFinalLocator;
116117
}
117118

119+
@Deprecated
118120
public static String removeDanglingChildCombinatorsFromCss(String css) {
119121
// convert to array by splitting the css by the child combinator
120122
// and remove from that array empty steps

bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/components/WebComponent.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import solutions.bellatrix.playwright.components.options.actions.HoverOptions;
4343
import solutions.bellatrix.playwright.components.options.actions.UncheckOptions;
4444
import solutions.bellatrix.playwright.components.options.states.BoundingBoxOptions;
45+
import solutions.bellatrix.playwright.components.shadowdom.ShadowDomService;
4546
import solutions.bellatrix.playwright.components.shadowdom.ShadowRoot;
4647
import solutions.bellatrix.playwright.configuration.WebSettings;
4748
import solutions.bellatrix.playwright.findstrategies.*;
@@ -325,7 +326,16 @@ protected String defaultGetWidthAttribute() {
325326
}
326327

327328
protected String defaultGetInnerHtmlAttribute() {
328-
return Optional.ofNullable(findLocator().innerHTML()).orElse("");
329+
if (!(this.inShadowContext())) {
330+
return Optional.ofNullable(findLocator().innerHTML()).orElse("");
331+
} else {
332+
if (this instanceof ShadowRoot) {
333+
return ShadowDomService.getShadowHtml(this, true);
334+
} else {
335+
return ShadowDomService.getShadowHtml(this, false);
336+
}
337+
}
338+
329339
}
330340

331341
protected String defaultGetForAttribute() {
@@ -1388,4 +1398,15 @@ public <TComponent extends WebComponent> List<TComponent> shadowRootCreateAllByI
13881398
public <TComponent extends WebComponent> List<TComponent> shadowRootCreateAllByInnerTextContaining(Class<TComponent> componentClass, String innerText) {
13891399
return create().allBy(componentClass, new InnerTextContainingFindStrategy(innerText));
13901400
}
1401+
1402+
private boolean inShadowContext() {
1403+
var component = this;
1404+
1405+
while (component != null) {
1406+
if (component instanceof ShadowRoot) return true;
1407+
component = component.getParentComponent();
1408+
}
1409+
1410+
return false;
1411+
}
13911412
}

bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/components/advanced/grid/Grid.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import solutions.bellatrix.playwright.findstrategies.XpathFindStrategy;
2525

2626
import java.lang.reflect.Field;
27+
import java.lang.reflect.InvocationTargetException;
2728
import java.lang.reflect.Method;
2829
import java.util.ArrayList;
2930
import java.util.Arrays;
@@ -137,12 +138,8 @@ public void forEachRow(Consumer<GridRow> action) {
137138

138139
public GridCell getCell(int row, int column) {
139140
String xpath = HtmlService.getAbsoluteXpath(getTableService().getCell(row, column));
140-
// if (innerXpath.startsWith(".")) innerXpath = innerXpath.substring(1);
141-
// String outerXpath = getCurrentElementXPath();
142-
//
143-
// String fullXpath = Objects.requireNonNullElse(outerXpath, ".") + innerXpath;
144141

145-
GridCell cell = this.create().byXpath(GridCell.class, xpath);
142+
GridCell cell = this.create().byXpath(GridCell.class, "." + xpath);
146143
setCellMetaData(cell, row, column);
147144

148145
return cell;
@@ -310,8 +307,13 @@ public <TRowObject> void assertTable(Class<TRowObject> clazz, List<TRowObject> e
310307
if (!clazz.equals(Object.class)) {
311308
entity = castRow(clazz, i, propsNotToCompare);
312309
} else {
313-
Method method = this.getClass().getMethod("castRow", int.class, List.class);
314-
entity = (TRowObject)method.invoke(this, i, Arrays.stream(propsNotToCompare).toList());
310+
Method method = null;
311+
try {
312+
method = this.getClass().getMethod("castRow", int.class, List.class);
313+
entity = (TRowObject)method.invoke(this, i, Arrays.stream(propsNotToCompare).toList());
314+
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
315+
throw new RuntimeException(e);
316+
}
315317
}
316318
EntitiesAsserter.areEqual(expectedEntities.get(i), entity, propsNotToCompare);
317319
}
@@ -355,9 +357,10 @@ public <TRowObject> TRowObject castRow(Class<TRowObject> clazz, int rowIndex, St
355357
}
356358

357359
var dto = InstanceFactory.create(clazz);
358-
var fields = clazz.getFields();
360+
var fields = clazz.getDeclaredFields();
359361
for (var field : fields) {
360362
var fieldType = field.getType();
363+
field.setAccessible(true);
361364

362365
var headerInfo = getHeaderNamesService().getHeaderInfoByField(field);
363366

@@ -492,7 +495,9 @@ public <TComponent extends WebComponent, TFindStrategy extends FindStrategy> Gri
492495

493496
public <TGridModel> Grid setModelColumns(Class<TGridModel> clazz) {
494497
controlColumnDataCollection = new ArrayList<>();
495-
for (var field : clazz.getFields()) {
498+
List<Field> declaredFields = List.of(clazz.getDeclaredFields());
499+
for (var field : declaredFields) {
500+
field.setAccessible(true);
496501
var headerName = field.isAnnotationPresent(TableHeader.class) ? field.getAnnotation(TableHeader.class).name() : field.getName();
497502
controlColumnDataCollection.add(new ControlColumnData(headerName));
498503
}

bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/components/common/create/RelativeCreateService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public <TComponent extends WebComponent, TFindStrategy extends FindStrategy> TCo
5050
TComponent newComponent;
5151

5252
if (inShadowContext()) {
53-
newComponent = ShadowDomService.createInShadowContext((WebComponent)baseComponent, componentClass, findStrategy);
53+
newComponent = ShadowDomService.createInShadowContext(componentClass, (WebComponent)baseComponent, findStrategy);
5454
} else {
5555
newComponent = createFromParentComponent(componentClass, findStrategy);
5656
}
@@ -69,7 +69,7 @@ public <TComponent extends WebComponent, TFindStrategy extends FindStrategy> Lis
6969
List<TComponent> componentList = new ArrayList<>();
7070

7171
if (inShadowContext()) {
72-
componentList = ShadowDomService.createAllInShadowContext((WebComponent)baseComponent, componentClass, findStrategy);
72+
componentList = ShadowDomService.createAllInShadowContext(componentClass, (WebComponent)baseComponent, findStrategy);
7373
} else {
7474
var elements = findStrategy.convert(baseComponent.getWrappedElement()).all();
7575

0 commit comments

Comments
 (0)