1 package com.atlassian.selenium;
2
3 import com.thoughtworks.selenium.Selenium;
4 import junit.framework.Assert;
5 import org.apache.log4j.Logger;
6
7 import java.util.Arrays;
8
9
10
11
12
13
14 public class SeleniumAssertions
15 {
16 private static final Logger log = Logger.getLogger(SeleniumAssertions.class);
17
18 private final Selenium client;
19 private final long conditionCheckInterval;
20 private final long defaultMaxWait;
21
22 public SeleniumAssertions(Selenium client, SeleniumConfiguration config)
23 {
24 this.client = client;
25 this.conditionCheckInterval = config.getConditionCheckInterval();
26 this.defaultMaxWait = config.getActionWait();
27 }
28
29 public void visibleByTimeout(String locator)
30 {
31 byTimeout(Conditions.isVisible(locator));
32 }
33
34
35
36
37
38
39
40
41 public void visibleByTimeout(String locator, long maxMillis)
42 {
43 byTimeout(Conditions.isVisible(locator), maxMillis);
44 }
45
46 public void notVisibleByTimeout(String locator)
47 {
48 byTimeout(Conditions.isNotVisible(locator));
49 }
50
51 public void notVisibleByTimeout(String locator, long maxMillis)
52 {
53 byTimeout(Conditions.isNotVisible(locator), maxMillis);
54 }
55
56 public void elementPresentByTimeout(String locator)
57 {
58 byTimeout(Conditions.isPresent(locator));
59 }
60
61 public void elementPresentByTimeout(String locator, long maxMillis)
62 {
63 byTimeout(Conditions.isPresent(locator), maxMillis);
64 }
65
66 public void elementPresentUntilTimeout(String locator)
67 {
68 untilTimeout(Conditions.isPresent(locator));
69 }
70
71 public void elementPresentUntilTimeout(String locator, long maxMillis)
72 {
73 untilTimeout(Conditions.isPresent(locator), maxMillis);
74 }
75
76 public void elementNotPresentByTimeout(String locator)
77 {
78 byTimeout(Conditions.isNotPresent(locator));
79 }
80
81
82
83 public void elementNotPresentUntilTimeout(String locator)
84 {
85 untilTimeout(Conditions.isNotPresent(locator));
86 }
87
88
89 public void elementNotPresentUntilTimeout(String locator, long maxMillis)
90 {
91 untilTimeout(Conditions.isNotPresent(locator), maxMillis);
92 }
93
94
95
96 public void textPresentByTimeout(String text, long maxMillis)
97 {
98 byTimeout(Conditions.isTextPresent(text), maxMillis);
99 }
100
101 public void textPresentByTimeout(String text)
102 {
103 byTimeout(Conditions.isTextPresent(text));
104 }
105
106 public void textNotPresentByTimeout(String text, long maxMillis)
107 {
108 byTimeout(Conditions.isTextNotPresent(text), maxMillis);
109 }
110
111 public void textNotPresentByTimeout(String text)
112 {
113 byTimeout(Conditions.isTextNotPresent(text));
114 }
115
116
117
118
119
120
121
122
123 public void elementNotPresentByTimeout(String locator, long maxMillis)
124 {
125 byTimeout(Conditions.isNotPresent(locator), maxMillis);
126 }
127
128 public void byTimeout(Condition condition)
129 {
130 byTimeout(condition, defaultMaxWait);
131 }
132
133 public void byTimeout(Condition condition, long maxWaitTime)
134 {
135 long startTime = System.currentTimeMillis();
136 while (true)
137 {
138 if (System.currentTimeMillis() - startTime >= maxWaitTime)
139 {
140 log.error("Page source:\n" + client.getHtmlSource());
141 throw new AssertionError("Waited " + maxWaitTime + " ms for [" + condition.errorMessage() + "], but it never became true.");
142 }
143
144 if (condition.executeTest(client))
145 break;
146
147 try
148 {
149 Thread.sleep(conditionCheckInterval);
150 }
151 catch (InterruptedException e)
152 {
153 throw new RuntimeException("Thread was interupted", e);
154 }
155 }
156 }
157
158 public void untilTimeout(Condition condition)
159 {
160 untilTimeout(condition, defaultMaxWait);
161 }
162
163 public void untilTimeout(Condition condition, long maxWaitTime)
164 {
165 long startTime = System.currentTimeMillis();
166 while (true)
167 {
168 if (System.currentTimeMillis() - startTime >= maxWaitTime)
169 {
170 break;
171 }
172
173 if (!condition.executeTest(client))
174 {
175 log.error("Page source:\n" + client.getHtmlSource());
176 throw new AssertionError("Condition [" + condition.errorMessage() + "], became before timeout.");
177 }
178
179 try
180 {
181 Thread.sleep(conditionCheckInterval);
182 }
183 catch (InterruptedException e)
184 {
185 throw new RuntimeException("Thread was interupted", e);
186 }
187 }
188 }
189
190
191
192
193 public void textPresent(String text)
194 {
195 Assert.assertTrue("Expected text not found in response: '" + text + "'", client.isTextPresent(text));
196 }
197
198
199
200
201 public void textNotPresent(String text)
202 {
203 Assert.assertFalse("Un-expected text found in response: '" + text + "'", client.isTextPresent(text));
204 }
205
206
207
208
209
210
211 public void formElementEquals(String locator, String value)
212 {
213 Assert.assertEquals("Element with id '" + locator + "' did not have the expected value '" + value + "'", value, client.getValue(locator));
214 }
215
216
217
218
219
220 public void elementPresent(String locator)
221 {
222 Assert.assertTrue("Expected element not found in response: '" + locator + "'", client.isElementPresent(locator));
223 }
224
225
226
227
228
229 public void elementNotPresent(String locator)
230 {
231 Assert.assertFalse("Un-expected element found in response: '" + locator + "'", client.isElementPresent(locator));
232 }
233
234
235
236
237
238
239 public void elementVisible(String locator)
240 {
241 Assert.assertTrue("Expected element not visible in response: '" + locator + "'", client.isElementPresent(locator) && client.isVisible(locator));
242 }
243
244
245
246
247
248
249 public void elementNotVisible(String locator)
250 {
251 Assert.assertFalse("Un-expected element visible in response: '" + locator + "'", client.isElementPresent(locator) && client.isVisible(locator));
252 }
253
254
255
256
257
258
259 public void htmlPresent(String html)
260 {
261 Assert.assertTrue("Expected HTML not found in response: '" + html + "'", client.getHtmlSource().toLowerCase().indexOf(html) >= 0);
262 }
263
264
265
266
267
268
269 public void htmlNotPresent(String html)
270 {
271 Assert.assertFalse("Unexpected HTML found in response: '" + html + "'", client.getHtmlSource().toLowerCase().indexOf(html) >= 0);
272 }
273
274
275
276
277
278
279 public void elementHasText(String locator, String text)
280 {
281 Assert.assertTrue("Element(s) with locator '" + locator +"' did not contain text '"+ text + "'", (client.getText(locator).indexOf(text) >= 0));
282 }
283
284
285
286
287
288
289 public void elementDoesntHaveText(String locator, String text)
290 {
291 Assert.assertFalse("Element(s) with locator '" + locator +"' did contained text '"+ text + "'", (client.getText(locator).indexOf(text) >= 0));
292 }
293
294
295
296
297
298
299
300 public void attributeContainsValue(String locator, String attribute, String value)
301 {
302 String attributeValue = client.getAttribute(locator + "@" + attribute);
303 Assert.assertTrue("Element with locator '" + locator + "' did not contain value '" + value + "' in attribute '" + attribute + "=" + attributeValue + "'", (attributeValue.indexOf(value) >= 0));
304 }
305
306
307
308
309
310
311
312 public void attributeDoesntContainValue(String locator, String attribute, String value)
313 {
314 String attributeValue = client.getAttribute(locator + "@" + attribute);
315 Assert.assertFalse("Element with locator '" + locator + "' did not contain value '" + value + "' in attribute '" + attribute + "'", (attributeValue.indexOf(value) >= 0));
316 }
317
318
319
320
321
322
323 public void linkPresentWithText(String text)
324 {
325 Assert.assertTrue("Expected link with text not found in response: '" + text + "'", client.isElementPresent("link=" + text));
326 }
327
328
329
330
331
332 public void linkNotPresentWithText(String text)
333 {
334 Assert.assertFalse("Unexpected link with text found in response: '" + text + "'", client.isElementPresent("link=" + text));
335 }
336
337
338
339
340
341 public void linkVisibleWithText(String text)
342 {
343 linkPresentWithText(text);
344 Assert.assertTrue("Expected link with text not visible: '" + text + "'", client.isVisible("link=" + text));
345 }
346
347
348
349
350
351
352
353 public void elementsVerticallyAligned(String locator1, String locator2, int deltaPixels)
354 {
355 int middle1 = client.getElementPositionTop(locator1).intValue() + (client.getElementHeight(locator1).intValue() / 2);
356 int middle2 = client.getElementPositionTop(locator2).intValue() + (client.getElementHeight(locator2).intValue() / 2);
357 String message = "Vertical position of element '" + locator1 + "' (" + middle1 + ") was not within " + deltaPixels +
358 " pixels of the vertical position of element '" + locator2 + "' (" + middle2 + ")";
359 Assert.assertTrue(message, Math.abs(middle1 - middle2) <= deltaPixels);
360 }
361
362
363 public void elementsSameHeight(final String locator1, final String locator2, final int deltaPixels)
364 {
365 int height1 = client.getElementHeight(locator1).intValue();
366 int height2 = client.getElementHeight(locator2).intValue();
367 String message = "Height of element '" + locator1 + "' (" + height1 + ") was not within " + deltaPixels +
368 " pixels of the height of element '" + locator2 + "' (" + height2 + ")";
369 Assert.assertTrue(message, Math.abs(height1 - height2) <= deltaPixels);
370 }
371
372
373
374
375 public void elementContainsText(String locator, String text)
376 {
377 String elementText = client.getText(locator);
378 Assert.assertTrue("Element(s) with locator '" + locator +"' did not contain text '"+ text + "', but contained '" + elementText + "'",
379 elementText.indexOf(text) >= 0);
380 }
381
382
383
384
385 public void elementDoesNotContainText(String locator, String text)
386 {
387 Assert.assertFalse("Element(s) with locator '" + locator +"' did contained text '"+ text + "'",
388 client.getText(locator).indexOf(text) >= 0);
389 }
390
391 public void windowClosed(String windowName)
392 {
393 Assert.assertFalse(Arrays.asList(client.getAllWindowNames()).contains(windowName));
394 }
395
396 public void windowOpen(String windowName)
397 {
398 Assert.assertTrue(Arrays.asList(client.getAllWindowNames()).contains(windowName));
399 }
400 }