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.apache.commons.lang.StringUtils;
7 import org.openqa.selenium.*;
8
9 import javax.annotation.Nullable;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13
14
15
16
17 public final class WebDriverQueryFunctions
18 {
19 private static final String CLASS_ATTR_NAME = "class";
20
21 private WebDriverQueryFunctions()
22 {
23 throw new AssertionError("Don't instantiate me");
24 }
25
26 public static Supplier<Boolean> isPresent(final SearchContext searchContext, final By locator)
27 {
28 return new Supplier<Boolean>()
29 {
30 public Boolean get()
31 {
32 return Check.elementExists(locator, searchContext);
33 }
34 };
35 }
36
37 public static Function<WebElement, Boolean> isPresent()
38 {
39 return new Function<WebElement, Boolean>()
40 {
41 public Boolean apply(@Nullable final WebElement from)
42 {
43
44 return true;
45 }
46 };
47 }
48
49 public static Function<WebElement, Boolean> isVisible()
50 {
51 return new Function<WebElement, Boolean>()
52 {
53 public Boolean apply(@Nullable final WebElement from)
54 {
55 if (from instanceof RenderedWebElement)
56 {
57 return ((RenderedWebElement)from).isDisplayed();
58 }
59 else return false;
60 }
61 };
62 }
63
64 public static Function<WebElement, Boolean> isEnabled()
65 {
66 return new Function<WebElement, Boolean>()
67 {
68 public Boolean apply(@Nullable final WebElement from)
69 {
70 return from.isEnabled();
71 }
72 };
73 }
74
75 public static Function<WebElement, Boolean> isSelected()
76 {
77 return new Function<WebElement, Boolean>()
78 {
79 public Boolean apply(@Nullable final WebElement from)
80 {
81 return from.isSelected();
82 }
83 };
84 }
85
86 public static Function<WebElement, String> getTagName()
87 {
88 return new Function<WebElement, String>()
89 {
90 public String apply(@Nullable final WebElement from)
91 {
92 return from.getTagName();
93 }
94 };
95 }
96
97 public static Function<WebElement, String> getText()
98 {
99 return new Function<WebElement, String>()
100 {
101 public String apply(@Nullable final WebElement from)
102 {
103 return from.getText();
104 }
105 };
106 }
107
108 public static Function<WebElement, String> getValue()
109 {
110 return new Function<WebElement, String>()
111 {
112 public String apply(@Nullable final WebElement from)
113 {
114 return from.getValue();
115 }
116 };
117 }
118
119 public static Function<WebElement, String> getAttribute(final String attributeName)
120 {
121 checkNotNull(attributeName);
122 return new Function<WebElement, String>()
123 {
124 public String apply(@Nullable final WebElement from)
125 {
126 return from.getAttribute(attributeName);
127 }
128 };
129 }
130
131 public static Function<WebElement, Boolean> hasAttribute(final String attributeName, final String expectedValue)
132 {
133 checkNotNull(attributeName);
134 checkNotNull(expectedValue);
135 return new Function<WebElement, Boolean>()
136 {
137 public Boolean apply(@Nullable final WebElement from)
138 {
139 return expectedValue.equals(from.getAttribute(attributeName));
140 }
141 };
142 }
143
144 public static Function<WebElement, Boolean> hasClass(final String className)
145 {
146 checkNotNull(className);
147 return new Function<WebElement, Boolean>()
148 {
149 public Boolean apply(@Nullable final WebElement from)
150 {
151 final String classNameLowerCase = className.toLowerCase();
152 String classValue = from.getAttribute(CLASS_ATTR_NAME);
153 if (StringUtils.isEmpty(classValue))
154 {
155 return false;
156 }
157 classValue = classValue.toLowerCase();
158 if (!classValue.contains(classNameLowerCase))
159 {
160 return false;
161 }
162 for (String singleClass : classValue.split("\\s"))
163 {
164 if (classNameLowerCase.equals(singleClass))
165 {
166 return true;
167 }
168 }
169 return false;
170 }
171 };
172 }
173
174 public static Function<WebElement, Boolean> hasText(final String text)
175 {
176 checkNotNull(text);
177 return new Function<WebElement, Boolean>()
178 {
179 public Boolean apply(@Nullable final WebElement from)
180 {
181 return text.equals(from.getText());
182 }
183 };
184 }
185 }