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.pageobjects.elements.timeout.Timeouts;
6   import com.atlassian.webdriver.AtlassianWebDriver;
7   import com.atlassian.webdriver.utils.Check;
8   import com.google.common.collect.Lists;
9   import org.openqa.selenium.By;
10  import org.openqa.selenium.Dimension;
11  import org.openqa.selenium.Point;
12  import org.openqa.selenium.WebElement;
13  
14  import javax.inject.Inject;
15  import java.util.List;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  /**
20   * Implementation of {@link PageElement} that waits for element to be
21   * present before executing each actions.
22   * 
23   */
24  public class WebDriverElement implements PageElement
25  {
26      @Inject
27      protected AtlassianWebDriver driver;
28  
29      @Inject
30      protected PageBinder pageBinder;
31  
32      @Inject
33      protected Timeouts timeouts;
34  
35      protected final WebDriverLocatable locatable;
36      protected final TimeoutType defaultTimeout;
37  
38      static WebElement getWebElement(PageElement element)
39      {
40          if (!WebDriverElement.class.isInstance(element))
41          {
42              throw new IllegalStateException("Unknown implementation of PageElement, cannot use to retrieve WebElement");
43          }
44          return WebDriverElement.class.cast(element).asWebElement();
45      }
46  
47      /**
48       * Creates a WebDriverElement within the driver's search context and default timeout
49       * @param locator The locator mechanism to use.
50       */
51      public WebDriverElement(By locator)
52      {
53          this(locator, TimeoutType.DEFAULT);
54      }
55  
56      /**
57       * Creates a WebDriverElement within the driver's search context and given timeout type.
58       * 
59       * @param locator The locator mechanism to use.
60       * @param timeoutType default timeout of this element
61       */
62      public WebDriverElement(By locator, TimeoutType timeoutType)
63      {
64          this(locator, WebDriverLocators.root(), timeoutType);
65      }
66  
67      /**
68       * Creates a WebDriverElement within a given parent and default timeout.
69       *
70       * @param locator The locator mechanism to use.
71       * @param parent The locatable parent of this element.
72       */
73      public WebDriverElement(By locator, WebDriverLocatable parent)
74      {
75          this(locator, parent, TimeoutType.DEFAULT);
76      }
77  
78      /**
79       * Creates a WebDriverElement within a given parent and given timeout type.
80       *
81       * @param locator The locator mechanism to use.
82       * @param parent The locatable parent of this element.
83       * @param timeoutType default timeout of this element
84       */
85      public WebDriverElement(By locator, WebDriverLocatable parent, TimeoutType timeoutType)
86      {
87          this(WebDriverLocators.nested(locator, parent), timeoutType);
88      }
89  
90      /**
91       * Creates a WebDriverElement with the given locatable and timeout type.
92       *
93       * @param locatable WebDriverLocatable that that locate this element
94       * @param timeoutType default timeout of this element
95       */
96      public WebDriverElement(WebDriverLocatable locatable, TimeoutType timeoutType)
97      {
98          this.locatable = checkNotNull(locatable, "locatable");
99          this.defaultTimeout = checkNotNull(timeoutType, "timeoutType");
100     }
101 
102     protected long timeout()
103     {
104         return timeouts.timeoutFor(defaultTimeout);
105     }
106 
107     protected WebDriverLocatable.LocateTimeout createTimout()
108     {
109         return new WebDriverLocatable.LocateTimeout.Builder()
110                 .timeout(timeout())
111                 .pollInterval(timeouts.timeoutFor(TimeoutType.EVALUATION_INTERVAL))
112                 .build();
113     }
114 
115     protected WebElement waitForWebElement()
116     {
117         return (WebElement) locatable.waitUntilLocated(driver, createTimout());
118     }
119     
120     public boolean isPresent()
121     {
122         return locatable.isPresent(driver, createTimout());
123     }
124 
125     public boolean isVisible()
126     {
127         WebElement element = waitForWebElement();
128         return element.isDisplayed();
129     }
130 
131     public boolean isEnabled()
132     {
133         return waitForWebElement().isEnabled();
134     }
135 
136     public boolean isSelected()
137     {
138         return waitForWebElement().isSelected();
139     }
140 
141     public boolean hasClass(final String className)
142     {
143         return Check.hasClass(className, waitForWebElement());
144     }
145 
146     public String getAttribute(final String name)
147     {
148         return waitForWebElement().getAttribute(name);
149     }
150 
151     public boolean hasAttribute(final String name, final String value)
152     {
153         return value.equals(getAttribute(name));
154     }
155 
156     public String getText()
157     {
158         return waitForWebElement().getText();
159     }
160 
161     public String getTagName()
162     {
163         return waitForWebElement().getTagName();
164     }
165 
166     public String getValue()
167     {
168         return waitForWebElement().getAttribute("value");
169     }
170 
171     @Override
172     public Point getLocation()
173     {
174         return waitForWebElement().getLocation();
175     }
176 
177     @Override
178     public Dimension getSize()
179     {
180         return waitForWebElement().getSize();
181     }
182 
183     public PageElement click()
184     {
185         waitForWebElement().click();
186         return this;
187     }
188 
189     public PageElement type(final CharSequence... keysToSend)
190     {
191         waitForWebElement().sendKeys(keysToSend);
192         return this;
193     }
194 
195     public PageElement select()
196     {
197         WebElement el = waitForWebElement();
198         if (!el.isSelected()) {
199             el.click();
200         }
201         return this;
202     }
203 
204     public PageElement toggle()
205     {
206         WebElement el = waitForWebElement();
207         el.click();
208         return this;
209     }
210 
211     public PageElement clear()
212     {
213         waitForWebElement().clear();
214         return this;
215     }
216 
217     public TimedElement timed()
218     {
219        return pageBinder.bind(WebDriverTimedElement.class, locatable, defaultTimeout);
220     }
221 
222     public PageElementJavascript javascript()
223     {
224         return new WebDriverElementJavascript(this);
225     }
226 
227     public PageElement find(By locator)
228     {
229         return pageBinder.bind(WebDriverElement.class, locator, locatable);
230     }
231 
232     public PageElement find(By locator, TimeoutType timeoutType)
233     {
234         return pageBinder.bind(WebDriverElement.class, locator, locatable, timeoutType);
235     }
236 
237     public <T extends PageElement> T find(By locator, Class<T> elementClass)
238     {
239         return pageBinder.bind(WebDriverElementMappings.findMapping(elementClass), locator, locatable);
240     }
241 
242     public <T extends PageElement> T find(By locator, Class<T> elementClass, TimeoutType timeoutType)
243     {
244         return pageBinder.bind(WebDriverElementMappings.findMapping(elementClass), locator, locatable, timeoutType);
245     }
246 
247     public List<PageElement> findAll(final By locator)
248     {
249         return findAll(locator, defaultTimeout);
250     }
251 
252     public List<PageElement> findAll(By locator, TimeoutType timeoutType)
253     {
254         return findAll(locator, PageElement.class, timeoutType);
255     }
256 
257     public <T extends PageElement> List<T> findAll(By locator, Class<T> elementClass)
258     {
259         return findAll(locator, elementClass, defaultTimeout);
260     }
261 
262     public <T extends PageElement> List<T> findAll(By locator, Class<T> elementClass, TimeoutType timeoutType)
263     {
264         List<T> elements = Lists.newLinkedList();
265         List<WebElement> webElements = waitForWebElement().findElements(locator);
266 
267         for(int i = 0; i < webElements.size(); i++)
268         {
269             elements.add(pageBinder.bind(WebDriverElementMappings.findMapping(elementClass),
270                     WebDriverLocators.list(webElements.get(i), locator, i, locatable), timeoutType));
271         }
272         return elements;
273     }
274 
275     /**
276      * This allows retrieving the webelement from the page element.
277      *
278      * @return the web element that represents the page element.
279      */
280     public WebElement asWebElement()
281     {
282         return waitForWebElement();
283     }
284 
285     public PageElement withTimeout(TimeoutType timeoutType)
286     {
287         if (this.defaultTimeout == timeoutType)
288         {
289             return this;
290         }
291         return pageBinder.bind(WebDriverElement.class, locatable, checkNotNull(timeoutType));
292     }
293 
294     @Override
295     public String toString()
296     {
297         return "WebDriverElement[locatable=" + locatable + ",defaultTimeout=" + defaultTimeout + "]";
298     }
299 }