View Javadoc

1   package com.atlassian.pageobjects.elements.query.webdriver;
2   
3   import com.atlassian.webdriver.utils.Check;
4   import com.google.common.base.Function;
5   import com.google.common.base.Supplier;
6   import org.openqa.selenium.By;
7   import org.openqa.selenium.Dimension;
8   import org.openqa.selenium.Point;
9   import org.openqa.selenium.SearchContext;
10  import org.openqa.selenium.WebElement;
11  
12  import static com.google.common.base.Preconditions.checkNotNull;
13  
14  /**
15   * Collection of functions for implementing timed queries in WebDriver.
16   *
17   */
18  public final class WebDriverQueryFunctions
19  {
20      private WebDriverQueryFunctions()
21      {
22          throw new AssertionError("Don't instantiate me");
23      }
24  
25      private static abstract class AbstractWebElementFunction<T> implements Function<WebElement,T>
26      {
27          private final String name;
28  
29          public AbstractWebElementFunction(String name)
30          {
31              this.name = name;
32          }
33  
34          @Override
35          public String toString()
36          {
37              return name;
38          }
39      }
40  
41      private static abstract class AbstractWebElementPredicate extends AbstractWebElementFunction<Boolean>
42      {
43  
44          public AbstractWebElementPredicate(String name)
45          {
46              super(name);
47          }
48      }
49  
50      public static Supplier<Boolean> isPresent(final SearchContext searchContext, final By locator)
51      {
52          return new Supplier<Boolean>()
53          {
54              public Boolean get()
55              {
56                  return Check.elementExists(locator, searchContext);
57              }
58  
59              @Override
60              public String toString()
61              {
62                  return "isPresent";
63              }
64          };
65  
66      }
67  
68      public static Function<WebElement, Boolean> isPresent()
69      {
70          return new AbstractWebElementPredicate("isPresent")
71          {
72              public Boolean apply(WebElement from)
73              {
74                  // if we're here, the element was found
75                  return true;
76              }
77  
78  
79          };
80      }
81  
82      public static Function<WebElement, Boolean> isVisible()
83      {
84          return new AbstractWebElementPredicate("isVisible")
85          {
86              public Boolean apply(WebElement from)
87              {
88                  return from.isDisplayed();
89              }
90          };
91      }
92  
93      public static Function<WebElement, Boolean> isEnabled()
94      {
95          return new AbstractWebElementPredicate("isEnabled")
96          {
97              public Boolean apply(WebElement from)
98              {
99                  return from.isEnabled();
100             }
101         };
102     }
103 
104     public static Function<WebElement, Boolean> isSelected()
105     {
106         return new AbstractWebElementPredicate("isSelected")
107         {
108             public Boolean apply(WebElement from)
109             {
110                 return from.isSelected();
111             }
112         };
113     }
114 
115     public static Function<WebElement, String> getTagName()
116     {
117         return new Function<WebElement, String>()
118         {
119             public String apply(WebElement from)
120             {
121                 return from.getTagName();
122             }
123         };
124     }
125 
126     public static Function<WebElement, String> getText()
127     {
128         return new Function<WebElement, String>()
129         {
130             public String apply(WebElement from)
131             {
132                 return from.getText();
133             }
134         };
135     }
136 
137     public static Function<WebElement, String> getValue()
138     {
139         return new Function<WebElement, String>()
140         {
141             public String apply(WebElement from)
142             {
143                 return from.getAttribute("value");
144             }
145         };
146     }
147 
148     public static Function<WebElement, Point> getLocation()
149     {
150         return new Function<WebElement, Point>()
151         {
152             public Point apply(WebElement from)
153             {
154                 return from.getLocation();
155             }
156         };
157     }
158 
159     public static Function<WebElement, Dimension> getSize()
160     {
161         return new Function<WebElement, Dimension>()
162         {
163             public Dimension apply(WebElement from)
164             {
165                 return from.getSize();
166             }
167         };
168     }
169 
170     public static Function<WebElement, String> getAttribute(final String attributeName)
171     {
172         checkNotNull(attributeName);
173         return new Function<WebElement, String>()
174         {
175             public String apply(WebElement from)
176             {
177                 return from.getAttribute(attributeName);
178             }
179         };
180     }
181 
182     public static Function<WebElement, Boolean> hasAttribute(final String attributeName, final String expectedValue)
183     {
184         checkNotNull(attributeName);
185         checkNotNull(expectedValue);
186         return new AbstractWebElementPredicate("hasAttribute")
187         {
188             public Boolean apply(WebElement from)
189             {
190                 return expectedValue.equals(from.getAttribute(attributeName));
191             }
192         };
193     }
194 
195     public static Function<WebElement, Boolean> hasClass(final String className)
196     {
197         checkNotNull(className);
198         return new AbstractWebElementPredicate("hasClass")
199         {
200             public Boolean apply(WebElement from)
201             {
202                 return Check.hasClass(className, from);
203             }
204         };
205     }
206 
207     public static Function<WebElement, Boolean> hasText(final String text)
208     {
209         checkNotNull(text);
210         return new AbstractWebElementPredicate("hasText")
211         {
212             public Boolean apply(WebElement from)
213             {
214                 // in case of text contains is the most common case
215                 return from.getText().contains(text);
216             }
217         };
218     }
219 
220     public static Function<WebElement, Boolean> hasValue(final String value)
221     {
222         checkNotNull(value);
223         return new AbstractWebElementPredicate("hasValue")
224         {
225             public Boolean apply(WebElement from)
226             {
227                 return value.equals(from.getAttribute("value"));
228             }
229         };
230     }
231 }