Skip to content

Commit 6453052

Browse files
committed
Refactored retry logic to appear only once in ShadowDomService
1 parent 6ca723a commit 6453052

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

bellatrix.web/src/main/java/solutions/bellatrix/web/components/shadowdom/ShadowDomService.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,7 @@ public static String getShadowHtml(WebComponent shadowComponent, boolean isShado
4444
}
4545

4646
public static <TComponent extends WebComponent, TFindStrategy extends FindStrategy> TComponent createFromShadowRoot(Class<TComponent> componentClass, ShadowRoot parentComponent, TFindStrategy findStrategy) {
47-
if (Wait.retry(() -> {
48-
List<TComponent> foundElements = createAllFromShadowRoot(componentClass, parentComponent, findStrategy);
49-
50-
if (foundElements.size() == 0) throw new IllegalArgumentException();
51-
52-
}, Duration.ofSeconds(ConfigurationService.get(WebSettings.class).getTimeoutSettings().getElementWaitTimeout()), Duration.ofSeconds(1), false)) {
53-
return createAllFromShadowRoot(componentClass, parentComponent, findStrategy).get(0);
54-
} else {
55-
throw new IllegalArgumentException("No elements inside the shadow DOM were found with the findStrategy: " + findStrategy.toString());
56-
}
47+
return retryFindingSingleComponent(() -> createAllFromShadowRoot(componentClass, parentComponent, findStrategy), findStrategy);
5748
}
5849

5950
public static <TComponent extends WebComponent, TFindStrategy extends FindStrategy> List<TComponent> createAllFromShadowRoot(Class<TComponent> componentClass, ShadowRoot parentComponent, TFindStrategy findStrategy) {
@@ -79,16 +70,7 @@ public static <TComponent extends WebComponent, TFindStrategy extends FindStrate
7970
}
8071

8172
public static <TComponent extends WebComponent, TFindStrategy extends FindStrategy> TComponent createInShadowContext(Class<TComponent> componentClass, WebComponent parentComponent, TFindStrategy findStrategy) {
82-
if (Wait.retry(() -> {
83-
List<TComponent> foundElements = createAllInShadowContext(componentClass, parentComponent, findStrategy);
84-
85-
if (foundElements.size() == 0) throw new IllegalArgumentException();
86-
87-
}, Duration.ofSeconds(ConfigurationService.get(WebSettings.class).getTimeoutSettings().getElementWaitTimeout()), Duration.ofSeconds(1), false)) {
88-
return createAllInShadowContext(componentClass, parentComponent, findStrategy).get(0);
89-
} else {
90-
throw new IllegalArgumentException("No elements inside the shadow DOM were found with the findStrategy: " + findStrategy.toString());
91-
}
73+
return retryFindingSingleComponent(() -> createAllInShadowContext(componentClass, parentComponent, findStrategy), findStrategy);
9274
}
9375

9476
public static <TComponent extends WebComponent, TFindStrategy extends FindStrategy> List<TComponent> createAllInShadowContext(Class<TComponent> componentClass, WebComponent parentComponent, TFindStrategy findStrategy) {
@@ -261,6 +243,28 @@ private static String convertToCssOrXpath(FindStrategy findStrategy) {
261243
return null;
262244
}
263245

246+
private static <TComponent extends WebComponent> TComponent retryFindingSingleComponent(Callable<List<TComponent>> callable, FindStrategy findStrategy) {
247+
if (Wait.retry(() -> {
248+
List<TComponent> foundElements;
249+
try {
250+
foundElements = callable.call();
251+
} catch (Exception e) {
252+
throw new RuntimeException(e);
253+
}
254+
255+
if (foundElements.size() == 0) throw new IllegalArgumentException();
256+
257+
}, Duration.ofSeconds(ConfigurationService.get(WebSettings.class).getTimeoutSettings().getElementWaitTimeout()), Duration.ofSeconds(1), false)) {
258+
try {
259+
return callable.call().get(0);
260+
} catch (Exception e) {
261+
throw new RuntimeException(e);
262+
}
263+
} else {
264+
throw new IllegalArgumentException("No element inside the shadow DOM was found with the findStrategy: " + findStrategy.toString());
265+
}
266+
}
267+
264268
private static final String javaScript = /* lang=js */ """
265269
function (element, locator, relativeElementCss) {
266270
const child_combinator = " > ";

0 commit comments

Comments
 (0)