PageRenderTime 22ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-webdriver/atlassian-webdriver-core/src/main/java/com/atlassian/webdriver/utils/element/WebDriverPoller.java

https://bitbucket.org/atlassian/atlassian-selenium
Java | 240 lines | 83 code | 18 blank | 139 comment | 0 complexity | de56988f55695e889f9d46568234a116 MD5 | raw file
  1. package com.atlassian.webdriver.utils.element;
  2. import com.atlassian.pageobjects.PageBinder;
  3. import org.hamcrest.Matcher;
  4. import org.openqa.selenium.TimeoutException;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.WebElement;
  7. import org.openqa.selenium.support.ui.FluentWait;
  8. import org.openqa.selenium.support.ui.WebDriverWait;
  9. import javax.annotation.Nonnull;
  10. import javax.inject.Inject;
  11. import java.util.concurrent.TimeUnit;
  12. import java.util.function.Function;
  13. import java.util.function.Predicate;
  14. import static com.google.common.base.Preconditions.checkArgument;
  15. import static java.util.Objects.requireNonNull;
  16. /**
  17. * <p>
  18. * A component that can be used to wait for certain conditions to happen on the tested page.
  19. * </p>
  20. *
  21. * <p>
  22. * The conditions are expressed as a generic {@link Function} from {@link WebDriver} to {@code boolean}.
  23. * {@link ElementConditions} contains factory methods to easily create some commonly used conditions.
  24. * </p>
  25. *
  26. * <p>
  27. * The {@link #DEFAULT_TIMEOUT} and {@link #DEFAULT_TIMEOUT_UNIT} specify the default timeout used when
  28. * no explicit timeout is provided by the client, which is currently 30 seconds. Clients are encouraged to use
  29. * their own timeout specific to the situation.
  30. * </p>
  31. *
  32. * <p>
  33. * NOTE: the default poll interval used by this class is as in the underlying {@link WebDriverWait} and is currently
  34. * 500ms (subject to change as the underlying {@link WebDriverWait} implementation changes. This may be generally
  35. * acceptable, but may not be granular enough for some scenarios (e.g. performance testing).
  36. * </p>
  37. *
  38. * Example usage:
  39. * <pre>
  40. * <code>
  41. * {@literal @Inject private WebDriverPoller poller; *
  42. * // ...
  43. * // wait for 5s for a 'my-element' to be present on the page
  44. * poller.waitUntil(ElementConditions.isPresent(By.id("my-element")), 5);
  45. * }
  46. * </code>
  47. * </pre>
  48. *
  49. * <p>
  50. * As of 2.3, {@code WebDriverPoller} also supports waiting for {@code WebElement}-specific predicates and matchers,
  51. * which require the web element to be already located.
  52. * </p>
  53. *
  54. * <p>
  55. * This component can be injected into page objects running within a {@link PageBinder} context.
  56. * </p>
  57. *
  58. * <p>
  59. * For more sophisticated polling/waiting toolkit, check the {@code PageElement} API in the
  60. * atlassian-pageobjects-elements module.
  61. * </p>
  62. *
  63. * @since 2.2
  64. * @see ElementConditions
  65. * @see WebDriverWait
  66. */
  67. public final class WebDriverPoller
  68. {
  69. public static final long DEFAULT_TIMEOUT = 30;
  70. public static final TimeUnit DEFAULT_TIMEOUT_UNIT = TimeUnit.SECONDS;
  71. private final WebDriver webDriver;
  72. private final TimeUnit timeUnit;
  73. private final long timeout;
  74. @Inject
  75. public WebDriverPoller(@Nonnull WebDriver webDriver)
  76. {
  77. this(webDriver, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT_UNIT);
  78. }
  79. public WebDriverPoller(@Nonnull WebDriver webDriver, long timeout, @Nonnull TimeUnit timeUnit)
  80. {
  81. checkArgument(timeout > 0, "Timeout must be >0");
  82. this.webDriver = requireNonNull(webDriver, "webDriver");
  83. this.timeout = timeout;
  84. this.timeUnit = requireNonNull(timeUnit, "timeUnit");
  85. }
  86. @Nonnull
  87. public WebDriverPoller withDefaultTimeout(long timeout, @Nonnull TimeUnit timeUnit)
  88. {
  89. return new WebDriverPoller(webDriver, timeout, timeUnit);
  90. }
  91. /**
  92. * Wait until {@literal condition} is {@literal true}, up to the default timeout. The default timeout depends
  93. * on the arguments supplied while creating this {@code WebDriverPoller}.
  94. *
  95. * @param condition condition that must evaluate to {@literal true}
  96. * @throws TimeoutException if the condition does not come true before the timeout expires
  97. * @see #DEFAULT_TIMEOUT
  98. * @see #DEFAULT_TIMEOUT_UNIT
  99. */
  100. public void waitUntil(@Nonnull Function<WebDriver, Boolean> condition)
  101. {
  102. waitUntil(condition, timeout, timeUnit);
  103. }
  104. /**
  105. * Wait until {@literal condition} up to the {@literal timeoutInSeconds}.
  106. *
  107. * @param condition condition that must evaluate to {@literal true}
  108. * @param timeoutInSeconds timeout in seconds to wait for {@literal condition} to come {@code true}
  109. * @throws TimeoutException if the condition does not come true before the timeout expires
  110. */
  111. public void waitUntil(@Nonnull Function<WebDriver, Boolean> condition, long timeoutInSeconds)
  112. {
  113. new WebDriverWait(webDriver, timeoutInSeconds).until(condition);
  114. }
  115. /**
  116. * Wait until {@literal condition} up to the {@literal timeout} specified by {@literal unit}.
  117. *
  118. * @param condition condition that must evaluate to {@literal true}
  119. * @param timeout timeout to wait for {@literal condition} to come true
  120. * @param unit unit of the {@literal timeout}
  121. * @throws TimeoutException if the condition does not come true before the timeout expires
  122. */
  123. public void waitUntil(@Nonnull Function<WebDriver, Boolean> condition, long timeout, @Nonnull TimeUnit unit)
  124. {
  125. waitUntil(condition, unit.toSeconds(timeout));
  126. }
  127. /**
  128. * Wait until {@literal condition} is {@code true} for {@code element}, up to the default timeout. The default
  129. * timeout depends on the arguments supplied while creating this {@code WebDriverPoller}.
  130. *
  131. * @param element the element to examine
  132. * @param condition condition that must evaluate to {@literal true}, expressed by a {@link Predicate}
  133. * @throws TimeoutException if the condition does not come true before the timeout expires
  134. * @since 2.3
  135. *
  136. * @see #DEFAULT_TIMEOUT
  137. * @see #DEFAULT_TIMEOUT_UNIT
  138. */
  139. public void waitUntil(@Nonnull WebElement element, @Nonnull Predicate<WebElement> condition)
  140. {
  141. waitUntil(element, condition, timeout, timeUnit);
  142. }
  143. /**
  144. * Wait until {@literal condition} is {@code true} for {@code element}, up to the {@literal timeoutInSeconds}.
  145. *
  146. * @param element the element to examine
  147. * @param condition condition that must evaluate to {@literal true}, expressed by a {@link Predicate}
  148. * @param timeoutInSeconds timeout in seconds to wait for {@literal condition} to come {@code true}
  149. * @throws TimeoutException if the condition does not come true before the timeout expires
  150. * @since 2.3
  151. */
  152. public void waitUntil(@Nonnull WebElement element, @Nonnull Predicate<WebElement> condition, long timeoutInSeconds)
  153. {
  154. waitUntil(element, condition, timeoutInSeconds, TimeUnit.SECONDS);
  155. }
  156. /**
  157. * Wait until {@literal condition} is {@code true} for {@code element}, up to the {@literal timeout} specified
  158. * by {@literal unit}.
  159. *
  160. * @param element the element to examine
  161. * @param condition condition that must evaluate to {@literal true}, expressed by a {@link Predicate}
  162. * @param timeout timeout to wait for {@literal condition} to come true
  163. * @param unit unit of the {@literal timeout}
  164. * @throws TimeoutException if the condition does not come true before the timeout expires
  165. * @since 2.3
  166. */
  167. public void waitUntil(@Nonnull WebElement element, @Nonnull Predicate<WebElement> condition,
  168. long timeout, TimeUnit unit)
  169. {
  170. new FluentWait<WebElement>(requireNonNull(element, "element")).withTimeout(timeout, unit).until(condition::test);
  171. }
  172. /**
  173. * Wait until {@literal condition} is {@code true} for {@code element}, up to the default timeout. The default
  174. * timeout depends on the arguments supplied while creating this {@code WebDriverPoller}.
  175. *
  176. * @param element the element to examine
  177. * @param condition condition that must evaluate to {@code true}, expressed by a {@code Matcher}
  178. * @throws TimeoutException if the condition does not come true before the timeout expires
  179. * @since 2.3
  180. *
  181. * @see #DEFAULT_TIMEOUT
  182. * @see #DEFAULT_TIMEOUT_UNIT
  183. */
  184. public void waitUntil(@Nonnull WebElement element, @Nonnull Matcher<? super WebElement> condition)
  185. {
  186. waitUntil(element, condition, timeout, timeUnit);
  187. }
  188. /**
  189. * Wait until {@code condition} is {@code true} for {@code element}, up to the {@code timeoutInSeconds}.
  190. *
  191. * @param element the element to examine
  192. * @param condition condition that must evaluate to {@code true}, expressed by a {@link Matcher}
  193. * @param timeoutInSeconds timeout in seconds to wait for {@code condition} to come {@code true}
  194. * @throws TimeoutException if the condition does not come true before the timeout expires
  195. * @since 2.3
  196. */
  197. public void waitUntil(@Nonnull WebElement element, @Nonnull Matcher<? super WebElement> condition,
  198. long timeoutInSeconds)
  199. {
  200. waitUntil(element, condition, timeoutInSeconds, TimeUnit.SECONDS);
  201. }
  202. /**
  203. * Wait until {@literal condition} is {@code true} for {@code element}, up to the {@code timeout} specified
  204. * by {@code unit}.
  205. *
  206. * @param element the element to examine
  207. * @param condition condition that must evaluate to {@code true}, expressed by a {@link Matcher}
  208. * @param timeout timeout to wait for {@code condition} to come true
  209. * @param unit unit of the {@code timeout}
  210. * @throws TimeoutException if the condition does not come true before the timeout expires
  211. * @since 2.3
  212. */
  213. public void waitUntil(@Nonnull WebElement element, @Nonnull Matcher<? super WebElement> condition,
  214. long timeout, TimeUnit unit)
  215. {
  216. waitUntil(element, newMatcherPredicate(condition), timeout, unit);
  217. }
  218. static <E> Predicate<E> newMatcherPredicate(final Matcher<? super E> filter)
  219. {
  220. return filter::matches;
  221. }
  222. }