View Javadoc

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