|
| 1 | +package software.xdev.selenium.elements; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.util.function.Function; |
| 5 | +import java.util.stream.Collectors; |
| 6 | +import java.util.stream.IntStream; |
| 7 | +import java.util.stream.Stream; |
| 8 | + |
| 9 | +import org.openqa.selenium.By; |
| 10 | +import org.openqa.selenium.JavascriptExecutor; |
| 11 | +import org.openqa.selenium.SearchContext; |
| 12 | +import org.openqa.selenium.WebDriver; |
| 13 | +import org.openqa.selenium.WebElement; |
| 14 | +import org.openqa.selenium.support.pagefactory.ByChained; |
| 15 | +import org.openqa.selenium.support.ui.WebDriverWait; |
| 16 | + |
| 17 | +import software.xdev.selenium.elements.instantiator.ElementInstantiator; |
| 18 | +import software.xdev.selenium.elements.instantiator.ElementInstantiatorInstance; |
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * Shorthand interface for common element lookup and interaction strategies/methods. |
| 23 | + */ |
| 24 | +public interface CanFindElements |
| 25 | +{ |
| 26 | + Duration DEFAULT_WAIT_UNTIL_DURATION = Duration.ofSeconds(10); |
| 27 | + |
| 28 | + default By byAttribute(final String attribute, final String value) |
| 29 | + { |
| 30 | + return By.cssSelector("[" + attribute + "='" + value + "']"); |
| 31 | + } |
| 32 | + |
| 33 | + default By byClassNamePart(final String className) |
| 34 | + { |
| 35 | + return By.cssSelector("." + className); |
| 36 | + } |
| 37 | + |
| 38 | + default WebElement waitForFirstChained(final By... chainedBys) |
| 39 | + { |
| 40 | + return this.waitForFirst(new ByChained(chainedBys)); |
| 41 | + } |
| 42 | + |
| 43 | + default WebElement waitForFirstAnd(final By... andBys) |
| 44 | + { |
| 45 | + return this.waitForFirst(new ByAnd(andBys)); |
| 46 | + } |
| 47 | + |
| 48 | + default WebElement waitForFirst(final By by) |
| 49 | + { |
| 50 | + return this.waitUntil(wd -> this.determineSearchContext(wd).findElement(by)); |
| 51 | + } |
| 52 | + |
| 53 | + default <T extends WebElement> T waitForFirst(final Class<T> clazz) |
| 54 | + { |
| 55 | + return this.waitForFirst(clazz, null); |
| 56 | + } |
| 57 | + |
| 58 | + default <T extends WebElement> T waitForFirst( |
| 59 | + final Class<T> clazz, |
| 60 | + final By additionalAndBy) |
| 61 | + { |
| 62 | + return this.waitForFirst(clazz, additionalAndBy, DEFAULT_WAIT_UNTIL_DURATION); |
| 63 | + } |
| 64 | + |
| 65 | + default <T extends WebElement> T waitForFirst( |
| 66 | + final Class<T> clazz, |
| 67 | + final By additionalAndBy, |
| 68 | + final Duration duration) |
| 69 | + { |
| 70 | + return this.elementProxyCreator().find( |
| 71 | + by -> this.waitUntil( |
| 72 | + wd -> this.determineSearchContext(wd) |
| 73 | + .findElement(additionalAndBy != null ? new ByAnd(by, additionalAndBy) : by), |
| 74 | + duration), |
| 75 | + clazz); |
| 76 | + } |
| 77 | + |
| 78 | + default ElementInstantiator elementProxyCreator() |
| 79 | + { |
| 80 | + return ElementInstantiatorInstance.instance(); |
| 81 | + } |
| 82 | + |
| 83 | + default <V> V waitUntil(final Function<WebDriver, V> isTrue) |
| 84 | + { |
| 85 | + return this.waitUntil(isTrue, DEFAULT_WAIT_UNTIL_DURATION); |
| 86 | + } |
| 87 | + |
| 88 | + default <V> V waitUntil(final Function<WebDriver, V> isTrue, final Duration duration) |
| 89 | + { |
| 90 | + return new WebDriverWait(this.getWebDriver(), duration).until(isTrue); |
| 91 | + } |
| 92 | + |
| 93 | + default <T extends WebElement> T waitForFirstByClassName(final Class<T> clazz, final String className) |
| 94 | + { |
| 95 | + return this.waitForFirst(clazz, By.cssSelector("." + className)); |
| 96 | + } |
| 97 | + |
| 98 | + default SearchContext determineSearchContext(final WebDriver webDriver) |
| 99 | + { |
| 100 | + return webDriver; |
| 101 | + } |
| 102 | + |
| 103 | + WebDriver getWebDriver(); |
| 104 | + |
| 105 | + default Object executeScript(final String script, final Object... args) |
| 106 | + { |
| 107 | + if(this.getWebDriver() instanceof final JavascriptExecutor jsExecutor) |
| 108 | + { |
| 109 | + return jsExecutor.executeScript(script, args); |
| 110 | + } |
| 111 | + |
| 112 | + throw new UnsupportedOperationException("WebDriver can't execute JS"); |
| 113 | + } |
| 114 | + |
| 115 | + default Object callFunction(final String methodName, final Object... args) |
| 116 | + { |
| 117 | + final String paramPlaceholders = IntStream.range(0, args.length) |
| 118 | + .mapToObj(i -> "arguments[" + (i + 1) + "]") // Offset by 1! |
| 119 | + .collect(Collectors.joining(",")); |
| 120 | + |
| 121 | + return this.executeScript( |
| 122 | + "return arguments[0]." + methodName + "(" + paramPlaceholders + ")", |
| 123 | + Stream.concat(Stream.of(this), Stream.of(args)).toArray(Object[]::new)); |
| 124 | + } |
| 125 | +} |
0 commit comments