View Javadoc

1   package com.atlassian.selenium;
2   
3   import com.thoughtworks.selenium.Selenium;
4   import com.atlassian.selenium.pageobjects.PageElement;
5   import junit.framework.Assert;
6   import org.apache.log4j.Logger;
7   
8   import java.util.Arrays;
9   
10  /**
11   * This provides helper methods for selenium assertions.  This
12   * mostly means waiting for events to occur (i.e. a dropdown to
13   * appear after a certain timeout, etc)
14   */
15  public class SeleniumAssertions
16  {
17      private static final Logger log = Logger.getLogger(SeleniumAssertions.class);
18  
19      private final Selenium client;
20      private final long conditionCheckInterval;
21      private final long defaultMaxWait;
22  
23      public SeleniumAssertions(Selenium client, SeleniumConfiguration config)
24      {
25          this.client = client;
26          this.conditionCheckInterval = config.getConditionCheckInterval();
27          this.defaultMaxWait = config.getActionWait();
28      }
29  
30      public void visibleByTimeout(String locator)
31      {
32          byTimeout(Conditions.isVisible(locator));
33      }
34  
35      public void visibleByTimeout(PageElement element)
36      {
37          visibleByTimeout(element.getLocator());
38      }
39  
40      /**
41       * This will wait until an element is visible.  If it doesnt become visible in
42       * maxMillis fail.
43       *
44       * @param locator   the selenium element locator
45       * @param maxMillis how long to wait as most in milliseconds
46       */
47      public void visibleByTimeout(String locator, long maxMillis)
48      {
49          byTimeout(Conditions.isVisible(locator), maxMillis);
50      }
51  
52      public void visibleByTimeout(PageElement element, long maxMillis)
53      {
54          visibleByTimeout(element.getLocator(), maxMillis);
55      }
56  
57      public void notVisibleByTimeout(String locator)
58      {
59          byTimeout(Conditions.isNotVisible(locator));
60      }
61  
62      public void notVisibleByTimeout(PageElement element)
63      {
64          notVisibleByTimeout(element.getLocator());
65      }
66  
67      public void notVisibleByTimeout(String locator, long maxMillis)
68      {
69          byTimeout(Conditions.isNotVisible(locator), maxMillis);
70      }
71  
72      public void notVisibleByTimeout(PageElement element, long maxMillis)
73      {
74          notVisibleByTimeout(element.getLocator(), maxMillis);
75      }
76  
77      public void elementPresentByTimeout(String locator)
78      {
79          byTimeout(Conditions.isPresent(locator));
80      }
81  
82      public void elementPresentByTimeout(PageElement element)
83      {
84          elementPresentByTimeout(element.getLocator());
85      }
86  
87      public void elementPresentByTimeout(String locator, long maxMillis)
88      {
89          byTimeout(Conditions.isPresent(locator), maxMillis);
90      }
91  
92      public void elementPresentByTimeout(PageElement element, long maxMillis)
93      {
94          elementPresentByTimeout(element.getLocator(), maxMillis);
95      }
96  
97  
98      public void elementPresentUntilTimeout(String locator)
99      {
100         untilTimeout(Conditions.isPresent(locator));
101     }
102 
103     public void elementPresentUntilTimeout(PageElement element)
104     {
105         elementPresentUntilTimeout(element.getLocator());        
106     }
107 
108     public void elementPresentUntilTimeout(String locator, long maxMillis)
109     {
110         untilTimeout(Conditions.isPresent(locator), maxMillis);
111     }
112 
113     public void elementPresentUntilTimeout(PageElement element, long maxMillis)
114     {
115         elementPresentUntilTimeout(element.getLocator(), maxMillis);
116     }
117     
118     public void elementNotPresentByTimeout(String locator)
119     {
120         byTimeout(Conditions.isNotPresent(locator));
121     }
122 
123     public void elementNotPresentByTimeout(PageElement element)
124     {
125         elementNotPresentByTimeout(element.getLocator());
126     }
127 
128     public void elementNotPresentUntilTimeout(String locator)
129     {
130         untilTimeout(Conditions.isNotPresent(locator));
131     }
132 
133     public void elementNotPresentUntilTimeout(PageElement element)
134     {
135         elementNotPresentUntilTimeout(element.getLocator());
136     }
137 
138     public void elementNotPresentUntilTimeout(String locator, long maxMillis)
139     {
140         untilTimeout(Conditions.isNotPresent(locator), maxMillis);
141     }
142 
143     public void elementNotPresentUntilTimeout(PageElement element, long maxMillis)
144     {
145         elementNotPresentUntilTimeout(element.getLocator(), maxMillis);
146     }
147     
148     public void textPresentByTimeout(String text, long maxMillis)
149     {
150         byTimeout(Conditions.isTextPresent(text), maxMillis);
151     }
152 
153     public void textPresentByTimeout(String text)
154     {
155         byTimeout(Conditions.isTextPresent(text));
156     }
157 
158     public void textNotPresentByTimeout(String text, long maxMillis)
159     {
160         byTimeout(Conditions.isTextNotPresent(text), maxMillis);
161     }
162 
163     public void textNotPresentByTimeout(String text)
164     {
165         byTimeout(Conditions.isTextNotPresent(text));
166     }
167 
168     /**
169      * This will wait until an element is not present.  If it doesnt become not present in
170      * maxMillis
171      *
172      * @param locator   the selenium element locator
173      * @param maxMillis how long to wait as most in milliseconds
174      */
175     public void elementNotPresentByTimeout(String locator, long maxMillis)
176     {
177         byTimeout(Conditions.isNotPresent(locator), maxMillis);
178     }
179 
180     public void elementNotPresentByTimeout(PageElement element, long maxMillis)
181     {
182         elementNotPresentByTimeout(element.getLocator(), maxMillis);
183     }
184 
185     public void byTimeout(Condition condition)
186     {
187         byTimeout(condition, defaultMaxWait);
188     }
189 
190     public void byTimeout(Condition condition, long maxWaitTime)
191     {
192         long startTime = System.currentTimeMillis();
193         while (true)
194         {
195             if (System.currentTimeMillis() - startTime >= maxWaitTime)
196             {
197                 log.error("Page source:\n" + client.getHtmlSource());
198                 throw new AssertionError("Waited " + maxWaitTime + " ms for [" + condition.errorMessage() + "], but it never became true.");
199             }
200 
201             if (condition.executeTest(client))
202                 break;
203 
204             try
205             {
206                 Thread.sleep(conditionCheckInterval);
207             }
208             catch (InterruptedException e)
209             {
210                 throw new RuntimeException("Thread was interupted", e);
211             }
212         }
213     }
214 
215     public void untilTimeout(Condition condition)
216     {
217         untilTimeout(condition, defaultMaxWait);
218     }
219 
220     public void untilTimeout(Condition condition, long maxWaitTime)
221     {
222         long startTime = System.currentTimeMillis();
223         while (true)
224         {
225             if (System.currentTimeMillis() - startTime >= maxWaitTime)
226             {
227                 break;
228             }
229 
230             if (!condition.executeTest(client))
231             {
232                 log.error("Page source:\n" + client.getHtmlSource());
233                 throw new AssertionError("Condition [" + condition.errorMessage() + "], became before timeout.");
234             }
235 
236             try
237             {
238                 Thread.sleep(conditionCheckInterval);
239             }
240             catch (InterruptedException e)
241             {
242                 throw new RuntimeException("Thread was interupted", e);
243             }
244         }
245     }
246 
247     /**
248      * @param text Asserts that text is present in the current page
249      */
250     public void textPresent(String text)
251     {
252         Assert.assertTrue("Expected text not found in response: '" + text + "'", client.isTextPresent(text));
253     }
254 
255     /**
256      * @param text Asserts that text is not present in the current page
257      */
258     public void textNotPresent(String text)
259     {
260         Assert.assertFalse("Un-expected text found in response: '" + text + "'", client.isTextPresent(text));
261     }
262 
263     /**
264      * Asserts that a given element has a specified value
265      * @param locator Locator for element using the standard selenium locator syntax
266      * @param value The value the element is expected to contain
267      */
268     public void formElementEquals(String locator, String value)
269     {
270         Assert.assertEquals("Element with id '" + locator + "' did not have the expected value '" + value + "'", value, client.getValue(locator));
271     }
272 
273     /**
274      * Asserts that a given element is present
275      * @param locator Locator for the element that should be present given using the standard selenium locator syntax
276      */
277     public void elementPresent(String locator)
278     {
279         Assert.assertTrue("Expected element not found in response: '" + locator + "'", client.isElementPresent(locator));
280     }
281 
282     public void elementPresent(PageElement element)
283     {
284         elementPresent(element.getLocator());
285     }
286 
287 
288     /**
289      * Asserts that a given element is not present on the current page
290      * @param locator Locator for the element that should not be present given using the standard selenium locator syntax
291      */
292     public void elementNotPresent(String locator)
293     {
294         Assert.assertFalse("Un-expected element found in response: '" + locator + "'", client.isElementPresent(locator));
295     }
296 
297     public void elementNotPresent(PageElement element)
298     {
299         elementNotPresent(element.getLocator());
300     }
301 
302     /**
303      * Asserts that a given element is present and is visible. Under some browsers just calling the seleinium.isVisible method
304      * on an element that doesn't exist causes selenium to throw an exception.
305      * @param locator Locator for the element that should be visible specified in the standard selenium syntax
306      */
307     public void elementVisible(String locator)
308     {
309         Assert.assertTrue("Expected element not visible in response: '" + locator + "'", client.isElementPresent(locator) && client.isVisible(locator));
310     }
311 
312 
313     public void elementVisible(PageElement element)
314     {
315         elementVisible(element.getLocator());
316     }
317     /**
318      * Asserts that a given element is not present and visible. Calling selenium's native selenium.isVisible method on
319      * an element that doesn't exist causes selenium to throw an exception
320      * @param locator Locator for the element that should not be visible specified in the standard selenium syntax
321      */
322     public void elementNotVisible(String locator)
323     {
324         Assert.assertFalse("Un-expected element visible in response: '" + locator + "'", client.isElementPresent(locator) && client.isVisible(locator));
325     }
326 
327 
328     public void elementNotVisible(PageElement element)
329     {
330         elementNotVisible(element.getLocator());
331     }
332     /**
333      * Asserts that a given element is visible and also contains the given text.
334      * @param locator Locator for the element that should be visible specified in the standard selenium syntax
335      * @param text the text that the element should contain
336      */
337     public void elementVisibleContainsText(String locator, String text)
338     {
339         elementVisible(locator);
340         elementContainsText(locator, text);
341     }
342 
343     public void elementVisibleContainsText(PageElement element, String text)
344     {
345         elementVisibleContainsText(element.getLocator(), text);
346     }
347 
348     /**
349      * Asserts that a particular piece of HTML is present in the HTML source. It is recommended that the elementPresent, elementHasText or some other method
350      * be used because browsers idiosyncratically add white space to the HTML source
351      * @param html Lower case representation of HTML string that should not be present
352      */
353     public void htmlPresent(String html)
354     {
355         Assert.assertTrue("Expected HTML not found in response: '" + html + "'", client.getHtmlSource().toLowerCase().indexOf(html) >= 0);
356     }
357 
358     /**
359      * Asserts that a particular piece of HTML is not present in the HTML source. It is recommended that the elementNotPresent, elementDoesntHaveText or
360      * some other method be used because browsers idiosyncratically add white space to the HTML source
361      * @param html Lower case representation of HTML string that should not be present
362      */
363     public void htmlNotPresent(String html)
364     {
365         Assert.assertFalse("Unexpected HTML found in response: '" + html + "'", client.getHtmlSource().toLowerCase().indexOf(html) >= 0);
366     }
367 
368     /**
369      * Asserts that the element specified by the locator contains the specified text
370      * @param locator Locator given in standard selenium syntax
371      * @param text The text that the element designated by the locator should contain
372      */
373     public void elementHasText(String locator, String text)
374     {
375         Assert.assertTrue("Element(s) with locator '" + locator +"' did not contain text '"+ text + "'", (client.getText(locator).indexOf(text) >= 0));
376     }
377 
378     public void elementHasText(PageElement element, String text)
379     {
380         elementHasText(element.getLocator(), text);
381     }
382 
383     /**
384      * Asserts that the element specified by the locator does not contain the specified text
385      * @param locator Locator given in standard selenium syntax
386      * @param text The text that the element designated by the locator should not contain
387      */
388     public void elementDoesntHaveText(String locator, String text)
389     {
390         Assert.assertFalse("Element(s) with locator '" + locator +"' did contained text '"+ text + "'", (client.getText(locator).indexOf(text) >= 0));
391     }
392 
393     public void elementDoesntHaveText(PageElement element, String text)
394     {
395         elementDoesntHaveText(element.getLocator(), text);
396     }
397 
398     /**
399      * Asserts that the element given by the locator has an attribute which contains the required value.
400      * @param locator Locator given in standard selenium syntax
401      * @param attribute The element attribute
402      * @param value The value expected to be found in the element's attribute
403      */
404     public void attributeContainsValue(String locator, String attribute, String value)
405     {
406         String attributeValue = client.getAttribute(locator + "@" + attribute);
407         Assert.assertTrue("Element with locator '" + locator + "' did not contain value '" + value + "' in attribute '" + attribute + "=" + attributeValue + "'", (attributeValue.indexOf(value) >= 0));
408     }
409 
410     public void attributeContainsValue(PageElement element, String attribute, String value)
411     {
412         attributeContainsValue(element.getLocator(), attribute, value);
413     }
414 
415     /**
416      * Asserts that the element given by the locator has an attribute which does not contain the given value.
417      * @param locator Locator given in standard selenium syntax
418      * @param attribute The element attribute
419      * @param value The value expected to be found in the element's attribute
420      */
421     public void attributeDoesntContainValue(String locator, String attribute, String value)
422     {
423         String attributeValue = client.getAttribute(locator + "@" + attribute);
424         Assert.assertFalse("Element with locator '" + locator + "' did not contain value '" + value + "' in attribute '" + attribute + "'", (attributeValue.indexOf(value) >= 0));
425     }
426 
427     public void attributeDoesntContainValue(PageElement element, String attribute, String value)
428     {
429         attributeDoesntContainValue(element.getLocator(), attribute, value);
430     }
431 
432     /**
433      * Asserts that a link containing the given text appears on the page
434      * @param text The text that a link on the page should contain
435      * @see #linkVisibleWithText(String) also
436      */
437     public void linkPresentWithText(String text)
438     {
439         Assert.assertTrue("Expected link with text not found in response: '" + text + "'", client.isElementPresent("link=" + text));
440     }
441 
442     /**
443      * Asserts that no link exists on the page containing the given text
444      * @param text The text that no link on the page should contain
445      */
446     public void linkNotPresentWithText(String text)
447     {
448         Assert.assertFalse("Unexpected link with text found in response: '" + text + "'", client.isElementPresent("link=" + text));
449     }
450 
451     /**
452      * Asserts that a link containin the given text is present and visible.
453      * @param text The text that a link on the page should contain
454      */
455     public void linkVisibleWithText(String text)
456     {
457         linkPresentWithText(text);
458         Assert.assertTrue("Expected link with text not visible: '" + text + "'", client.isVisible("link=" + text));
459     }
460 
461     /**
462      * Asserts that two elements (located by selenium syntax) are vertically within deltaPixels of each other.
463      * @param locator1 Locator for element 1 given in standard selenium syntax
464      * @param locator2 Locator for element 2 given in standard selenium syntax
465      * @param deltaPixels The maximum allowable distance between the two element
466      */
467     public void elementsVerticallyAligned(String locator1, String locator2, int deltaPixels)
468     {
469         int middle1 = client.getElementPositionTop(locator1).intValue() + (client.getElementHeight(locator1).intValue() / 2);
470         int middle2 = client.getElementPositionTop(locator2).intValue() + (client.getElementHeight(locator2).intValue() / 2);
471         String message = "Vertical position of element '" + locator1 + "' (" + middle1 + ") was not within " + deltaPixels +
472             " pixels of the vertical position of element '" + locator2 + "' (" + middle2 + ")";
473         Assert.assertTrue(message, Math.abs(middle1 - middle2) <= deltaPixels);
474     }
475 
476     public void elementsVerticallyAligned(PageElement element1, PageElement element2, int deltaPixels)
477     {
478         elementsVerticallyAligned(element1.getLocator(), element2.getLocator(), deltaPixels);
479     }
480 
481     public void elementsSameHeight(final String locator1, final String locator2, final int deltaPixels)
482     {
483         int height1 = client.getElementHeight(locator1).intValue();
484         int height2 = client.getElementHeight(locator2).intValue();
485         String message = "Height of element '" + locator1 + "' (" + height1 + ") was not within " + deltaPixels +
486             " pixels of the height of element '" + locator2 + "' (" + height2 + ")";
487         Assert.assertTrue(message, Math.abs(height1 - height2) <= deltaPixels);
488     }
489 
490     public void elementsSameHeight(PageElement element1, PageElement element2, final int deltaPixels)
491     {
492         elementsSameHeight(element1.getLocator(), element2.getLocator(), deltaPixels);
493     }
494 
495     /**
496      * Asserts that an element contains the given text.
497      */
498     public void elementContainsText(String locator, String text)
499     {
500         String elementText = client.getText(locator);
501         Assert.assertTrue("Element(s) with locator '" + locator +"' did not contain text '"+ text + "', but contained '" + elementText + "'",
502             elementText.indexOf(text) >= 0);
503     }
504 
505     public void elementContainsText(PageElement element, String text)
506     {
507         elementContainsText(element.getLocator(), text);        
508     }
509 
510 
511     /**
512      * Asserts that an element does not contain the given text.
513      */
514     public void elementDoesNotContainText(String locator, String text)
515     {
516         Assert.assertFalse("Element(s) with locator '" + locator +"' did contained text '"+ text + "'",
517             client.getText(locator).indexOf(text) >= 0);
518     }
519 
520     public void elementDoesNotContainText(PageElement element, String text)
521     {
522         elementDoesNotContainText(element.getLocator(), text);        
523     }
524 
525     public void windowClosed(String windowName)
526     {
527         Assert.assertFalse(Arrays.asList(client.getAllWindowNames()).contains(windowName));
528     }
529 
530     public void windowOpen(String windowName)
531     {
532         Assert.assertTrue(Arrays.asList(client.getAllWindowNames()).contains(windowName));
533     }
534 }