View Javadoc

1   package com.atlassian.pageobjects.elements;
2   
3   import com.atlassian.pageobjects.PageBinder;
4   import com.atlassian.pageobjects.elements.timeout.TimeoutType;
5   import com.atlassian.webdriver.AtlassianWebDriver;
6   import com.google.common.collect.Lists;
7   import org.openqa.selenium.By;
8   import org.openqa.selenium.WebElement;
9   
10  import javax.annotation.Nonnull;
11  import javax.inject.Inject;
12  import java.util.List;
13  
14  /**
15   * {@link com.atlassian.pageobjects.elements.PageElementFinder} for the global search context (whole page).
16   *
17   */
18  public class GlobalElementFinder implements PageElementFinder {
19  
20      @Inject
21      AtlassianWebDriver driver;
22  
23      @Inject
24      PageBinder pageBinder;
25  
26      @Nonnull
27      public PageElement find(@Nonnull final By by)
28      {
29          return pageBinder.bind(WebDriverElement.class, by);
30      }
31  
32      @Nonnull
33      public PageElement find(@Nonnull final By by, @Nonnull TimeoutType timeoutType)
34      {
35          return pageBinder.bind(WebDriverElement.class, by, timeoutType);
36      }
37  
38      @Nonnull
39      public <T extends PageElement> T find(@Nonnull final By by, @Nonnull Class<T> elementClass)
40      {
41          return pageBinder.bind(WebDriverElementMappings.findMapping(elementClass), by);
42      }
43  
44      @Nonnull
45      public <T extends PageElement> T find(@Nonnull final By by, @Nonnull Class<T> elementClass,
46                                            @Nonnull TimeoutType timeoutType)
47      {
48          return pageBinder.bind(WebDriverElementMappings.findMapping(elementClass), by, timeoutType);
49      }
50  
51      @Nonnull
52      public List<PageElement> findAll(@Nonnull final By by)
53      {
54          return findAll(by, TimeoutType.DEFAULT);
55      }
56  
57      @Nonnull
58      public List<PageElement> findAll(@Nonnull final By by, @Nonnull TimeoutType timeoutType)
59      {
60          return findAll(by, PageElement.class, timeoutType);
61      }
62  
63      @Nonnull
64      public <T extends PageElement> List<T> findAll(@Nonnull final By by, @Nonnull Class<T> elementClass)
65      {
66          return findAll(by, elementClass, TimeoutType.DEFAULT);
67      }
68  
69      @Nonnull
70      public <T extends PageElement> List<T> findAll(@Nonnull final By by, @Nonnull Class<T> elementClass,
71                                                     @Nonnull TimeoutType timeoutType)
72      {
73          List<T> elements = Lists.newLinkedList();
74          List<WebElement> webElements = driver.findElements(by);
75  
76          for(int i = 0; i < webElements.size(); i++)
77          {
78              elements.add(pageBinder.bind(WebDriverElementMappings.findMapping(elementClass),
79                      WebDriverLocators.list(webElements.get(i), by, i, WebDriverLocators.root()), timeoutType));
80          }
81  
82          return elements;
83      }
84  }