|
8 | 8 | import aquality.selenium.core.logging.Logger;
|
9 | 9 | import aquality.selenium.core.waitings.IConditionalWait;
|
10 | 10 | import com.google.inject.Inject;
|
11 |
| -import org.openqa.selenium.By; |
| 11 | +import org.openqa.selenium.*; |
12 | 12 | import org.openqa.selenium.By.ByTagName;
|
13 | 13 | import org.openqa.selenium.By.ByXPath;
|
14 |
| -import org.openqa.selenium.InvalidArgumentException; |
15 |
| -import org.openqa.selenium.WebElement; |
16 | 14 | import org.openqa.selenium.support.pagefactory.ByChained;
|
17 | 15 |
|
18 | 16 | import java.lang.reflect.Constructor;
|
19 | 17 | import java.lang.reflect.InvocationTargetException;
|
20 | 18 | import java.time.Duration;
|
21 |
| -import java.util.ArrayList; |
22 |
| -import java.util.HashMap; |
23 |
| -import java.util.List; |
24 |
| -import java.util.Map; |
| 19 | +import java.util.*; |
25 | 20 | import java.util.concurrent.TimeoutException;
|
26 | 21 |
|
27 | 22 | public class ElementFactory implements IElementFactory {
|
@@ -87,16 +82,26 @@ public <T extends IElement> List<T> findElements(By locator, String name, IEleme
|
87 | 82 | } catch (TimeoutException e) {
|
88 | 83 | throw new org.openqa.selenium.TimeoutException(e.getMessage());
|
89 | 84 | }
|
90 |
| - List<WebElement> webElements = elementFinder.findElements(locator, state, ZERO_TIMEOUT); |
91 | 85 | String namePrefix = name == null ? "element" : name;
|
92 |
| - List<T> list = new ArrayList<>(); |
93 |
| - for (int index = 1; index <= webElements.size(); index++) { |
94 |
| - WebElement webElement = webElements.get(index - 1); |
95 |
| - String currentName = String.format("%1$s %2$s", namePrefix, index); |
96 |
| - T element = supplier.get(generateLocator(locator, webElement, index), currentName, state); |
97 |
| - list.add(element); |
98 |
| - } |
99 |
| - return list; |
| 86 | + List<T> elements = new ArrayList<>(); |
| 87 | + Collection<Class<? extends Throwable>> ignoredExceptions = Arrays.asList( |
| 88 | + StaleElementReferenceException.class, JavascriptException.class, org.openqa.selenium.TimeoutException.class |
| 89 | + ); |
| 90 | + conditionalWait.waitFor(() -> { |
| 91 | + List<WebElement> webElements = elementFinder.findElements(locator, state, ZERO_TIMEOUT); |
| 92 | + for (int index = 1; index <= webElements.size(); index++) { |
| 93 | + WebElement webElement = webElements.get(index - 1); |
| 94 | + String currentName = String.format("%1$s %2$s", namePrefix, index); |
| 95 | + T element = supplier.get(generateLocator(locator, webElement, index), currentName, state); |
| 96 | + elements.add(element); |
| 97 | + } |
| 98 | + boolean anyElementsFound = !elements.isEmpty(); |
| 99 | + return count == ElementsCount.ANY |
| 100 | + || (count == ElementsCount.ZERO && !anyElementsFound) |
| 101 | + || (count == ElementsCount.MORE_THAN_ZERO && anyElementsFound); |
| 102 | + }, ignoredExceptions); |
| 103 | + |
| 104 | + return elements; |
100 | 105 | }
|
101 | 106 |
|
102 | 107 | protected void waitForElementsCount(By locator, ElementsCount count, ElementState state) throws TimeoutException {
|
|
0 commit comments