View Javadoc

1   package com.atlassian.webdriver.waiter.webdriver.retriever;
2   
3   import com.google.common.base.Preconditions;
4   import org.openqa.selenium.By;
5   import org.openqa.selenium.SearchContext;
6   import org.openqa.selenium.WebElement;
7   
8   /**
9    * @since 2.1.0
10   */
11  public class WebElementRetriever
12  {
13      private final Supplier<WebElement> elementSupplier;
14  
15      private WebElementRetriever(Supplier<WebElement> elementSupplier)
16      {
17          this.elementSupplier = Preconditions.checkNotNull(elementSupplier, "The element supplier can not be null.");
18      }
19  
20      public static WebElementRetriever newLocatorRetriever(final By locator, final SearchContext context)
21      {
22          Preconditions.checkNotNull(locator, "The locator can not be null.");
23          Preconditions.checkNotNull(context, "The search context can not be null.");
24          return new WebElementRetriever(new Supplier<WebElement>()
25          {
26              @Override
27              public WebElement supply()
28              {
29                  return context.findElement(locator);
30              }
31          });
32      }
33  
34      public static WebElementRetriever newWebElementRetriever(final WebElement webElement)
35      {
36          Preconditions.checkNotNull(webElement, "The webelement can not be null.");
37          return new WebElementRetriever(new Supplier<WebElement>()
38          {
39              @Override
40              public WebElement supply()
41              {
42                  return webElement;
43              }
44          });
45      }
46  
47      public WebElement retrieveElement()
48      {
49          return elementSupplier.supply();
50      }
51  }