|
13 | 13 |
|
14 | 14 | package solutions.bellatrix.web.components.shadowdom; |
15 | 15 |
|
| 16 | +import lombok.SneakyThrows; |
16 | 17 | import lombok.experimental.UtilityClass; |
17 | 18 | import org.jsoup.Jsoup; |
18 | 19 | import org.jsoup.nodes.Element; |
19 | 20 | import org.jsoup.select.Elements; |
20 | 21 | import org.openqa.selenium.By; |
| 22 | +import solutions.bellatrix.core.configuration.ConfigurationService; |
21 | 23 | import solutions.bellatrix.core.utilities.HtmlService; |
22 | 24 | import solutions.bellatrix.core.utilities.InstanceFactory; |
23 | 25 | import solutions.bellatrix.core.utilities.Ref; |
| 26 | +import solutions.bellatrix.core.utilities.Wait; |
24 | 27 | import solutions.bellatrix.web.components.WebComponent; |
| 28 | +import solutions.bellatrix.web.configuration.WebSettings; |
25 | 29 | import solutions.bellatrix.web.findstrategies.CssFindStrategy; |
26 | 30 | import solutions.bellatrix.web.findstrategies.FindStrategy; |
27 | 31 | import solutions.bellatrix.web.findstrategies.ShadowXPathFindStrategy; |
28 | 32 |
|
| 33 | +import java.time.Duration; |
29 | 34 | import java.util.ArrayList; |
30 | 35 | import java.util.Arrays; |
31 | 36 | import java.util.List; |
32 | 37 | import java.util.Stack; |
| 38 | +import java.util.concurrent.Callable; |
33 | 39 | import java.util.stream.Collectors; |
34 | 40 |
|
35 | 41 | @UtilityClass |
@@ -97,16 +103,56 @@ public static <TComponent extends WebComponent, TFindStrategy extends FindStrate |
97 | 103 | return componentList; |
98 | 104 | } |
99 | 105 |
|
| 106 | + @SneakyThrows |
100 | 107 | private static String[] getAbsoluteCss(ShadowRoot shadowRoot, String locator) { |
101 | | - return shadowRoot.getJavaScriptService() |
102 | | - .<ArrayList<String>>genericExecute(String.format("return (%s)(arguments[0], arguments[1], arguments[2]);", javaScript), |
103 | | - shadowRoot.findElement(), locator, null).toArray(String[]::new); |
| 108 | + Callable<String[]> js = () -> { |
| 109 | + return shadowRoot.getJavaScriptService() |
| 110 | + .<ArrayList<String>>genericExecute(String.format("return (%s)(arguments[0], arguments[1], arguments[2]);", javaScript), |
| 111 | + shadowRoot.findElement(), locator, null).toArray(String[]::new); |
| 112 | + }; |
| 113 | + |
| 114 | + if (Wait.retry(() -> { |
| 115 | + String[] foundElements; |
| 116 | + try { |
| 117 | + foundElements = js.call(); |
| 118 | + } catch (Exception e) { |
| 119 | + throw new RuntimeException(e); |
| 120 | + } |
| 121 | + |
| 122 | + if (foundElements == null || foundElements.length == 0) { |
| 123 | + throw new IllegalArgumentException(); |
| 124 | + } |
| 125 | + }, Duration.ofSeconds(ConfigurationService.get(WebSettings.class).getTimeoutSettings().getElementWaitTimeout()), Duration.ofSeconds(1), false)) { |
| 126 | + return js.call(); |
| 127 | + } else { |
| 128 | + throw new IllegalArgumentException("No elements inside the shadow DOM were found with the locator: " + locator); |
| 129 | + } |
104 | 130 | } |
105 | 131 |
|
| 132 | + @SneakyThrows |
106 | 133 | private static String[] getRelativeCss(ShadowRoot shadowRoot, String locator, String parentLocator) { |
107 | | - return shadowRoot.getJavaScriptService() |
108 | | - .<ArrayList<String>>genericExecute(String.format("return (%s)(arguments[0], arguments[1], arguments[2]);", javaScript), |
109 | | - shadowRoot.findElement(), locator, parentLocator).toArray(String[]::new); |
| 134 | + Callable<String[]> js = () -> { |
| 135 | + return shadowRoot.getJavaScriptService() |
| 136 | + .<ArrayList<String>>genericExecute(String.format("return (%s)(arguments[0], arguments[1], arguments[2]);", javaScript), |
| 137 | + shadowRoot.findElement(), locator, parentLocator).toArray(String[]::new); |
| 138 | + }; |
| 139 | + |
| 140 | + if (Wait.retry(() -> { |
| 141 | + String[] foundElements; |
| 142 | + try { |
| 143 | + foundElements = js.call(); |
| 144 | + } catch (Exception e) { |
| 145 | + throw new RuntimeException(e); |
| 146 | + } |
| 147 | + |
| 148 | + if (foundElements == null || foundElements.length == 0) { |
| 149 | + throw new IllegalArgumentException(); |
| 150 | + } |
| 151 | + }, Duration.ofSeconds(ConfigurationService.get(WebSettings.class).getTimeoutSettings().getElementWaitTimeout()), Duration.ofSeconds(1), false)) { |
| 152 | + return js.call(); |
| 153 | + } else { |
| 154 | + throw new IllegalArgumentException("No elements inside the shadow DOM were found with the locator: " + locator); |
| 155 | + } |
110 | 156 | } |
111 | 157 |
|
112 | 158 | private static <TComponent extends WebComponent> TComponent buildMissingShadowRootsAndCreate(Class<TComponent> clazz, ShadowRoot parentComponent, Ref<String> fullCss) { |
|
0 commit comments