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