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