View Javadoc

1   package com.atlassian.webdriver.waiter.webdriver;
2   
3   import com.atlassian.annotations.ExperimentalApi;
4   import com.atlassian.webdriver.AtlassianWebDriver;
5   import com.atlassian.webdriver.waiter.Waiter;
6   import com.atlassian.webdriver.waiter.WaiterQuery;
7   
8   import java.util.concurrent.TimeUnit;
9   import javax.inject.Inject;
10  
11  /**
12   *
13   * <strong>WARNING</strong>: This API is still experimental and may be changed between versions.
14   *
15   * @since 2.1.0
16   */
17  @ExperimentalApi
18  public class WebDriverWaiter implements Waiter
19  {
20      // Default wait time is 30 seconds
21      private static final long DEFAULT_INTERVAL_TIME = 30;
22      private static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.SECONDS;
23  
24      private AtlassianWebDriver driver;
25  
26      @Inject
27      public WebDriverWaiter(AtlassianWebDriver driver)
28      {
29          this.driver = driver;
30      }
31  
32      /**
33       * Creates a {@link com.atlassian.webdriver.waiter.WaiterQuery} that uses the default interval {@link #DEFAULT_INTERVAL_TIME}
34       * @return
35       */
36      public WaiterQuery until()
37      {
38          return until(DEFAULT_INTERVAL_TIME, DEFAULT_TIME_UNIT);
39      }
40  
41      /**
42       * Creates a {@link com.atlassian.webdriver.waiter.WaiterQuery} which allows the timeout interval
43       * for the waiter to be set.
44       * @param timeout in seconds for the waiter to run for before failing.
45       * @return a {@link com.atlassian.webdriver.waiter.WaiterQuery} with the timeout interval set to the provided interval
46       */
47      public WaiterQuery until(long timeout)
48      {
49          return until(timeout, TimeUnit.SECONDS);
50      }
51  
52      /**
53       * Creates a {@link com.atlassian.webdriver.waiter.WaiterQuery} which setgs the timeout interval
54       * to a more specific value in seconds or milliseconds.
55       * @param time the maximum time to wait
56       * @param unit the time unit of the <code>time</code> argument
57       * @return a {@link com.atlassian.webdriver.waiter.WaiterQuery} with the timeout interval set to the provided interval
58       */
59      public WaiterQuery until(long time, TimeUnit unit)
60      {
61          long millisTimeout;
62  
63          switch(unit)
64          {
65              case SECONDS:
66                  millisTimeout = time * 1000;
67                  break;
68              case MILLISECONDS:
69                  millisTimeout = time;
70                  break;
71              case MINUTES:
72                  millisTimeout = 60 * 1000 * time;
73                  break;
74              default:
75                  throw new UnsupportedOperationException("The provided TimeUnit: " + unit + " is not supported");
76          }
77  
78          return new WebDriverWaiterQuery(new WebDriverQueryBuilder(driver, millisTimeout));
79      }
80  }