View Javadoc

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    * This provides helper methods for selenium assertions.  This
9    * mostly means waiting for events to occur (i.e. a dropdown to
10   * appear after a certain timeout, etc)
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       * This will wait until an element is visible.  If it doesnt become visible in
34       * maxMillis fail.
35       *
36       * @param locator   the selenium element locator
37       * @param maxMillis how long to wait as most in milliseconds
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      * This will wait until an element is not present.  If it doesnt become not present in
116      * maxMillis
117      *
118      * @param locator   the selenium element locator
119      * @param maxMillis how long to wait as most in milliseconds
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      * @param text Asserts that text is present in the current page
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      * @param text Asserts that text is not present in the current page
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      * Asserts that a given element has a specified value
206      * @param locator Locator for element using the standard selenium locator syntax
207      * @param value The value the element is expected to contain
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      * Asserts that a given element is present
216      * @param locator Locator for the element that should be present given using the standard selenium locator syntax
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      * Asserts that a given element is not present on the current page
225      * @param locator Locator for the element that should not be present given using the standard selenium locator syntax
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      * Asserts that a given element is present and is visible. Under some browsers just calling the seleinium.isVisible method
234      * on an element that doesn't exist causes selenium to throw an exception.
235      * @param locator Locator for the element that should be visible specified in the standard selenium syntax
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      * Asserts that a given element is not present and visible. Calling selenium's native selenium.isVisible method on
244      * an element that doesn't exist causes selenium to throw an exception
245      * @param locator Locator for the element that should not be visible specified in the standard selenium syntax
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      * Asserts that a particular piece of HTML is present in the HTML source. It is recommended that the elementPresent, elementHasText or some other method
254      * be used because browsers idiosyncratically add white space to the HTML source
255      * @param html Lower case representation of HTML string that should not be present
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      * Asserts that a particular piece of HTML is not present in the HTML source. It is recommended that the elementNotPresent, elementDoesntHaveText or
264      * some other method be used because browsers idiosyncratically add white space to the HTML source
265      * @param html Lower case representation of HTML string that should not be present
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      * Asserts that the element specified by the locator contains the specified text
274      * @param locator Locator given in standard selenium syntax
275      * @param text The text that the element designated by the locator should contain
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      * Asserts that the element specified by the locator does not contain the specified text
284      * @param locator Locator given in standard selenium syntax
285      * @param text The text that the element designated by the locator should not contain
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      * Asserts that the element given by the locator has an attribute which contains the required value.
294      * @param locator Locator given in standard selenium syntax
295      * @param attribute The element attribute
296      * @param value The value expected to be found in the element's attribute
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      * Asserts that the element given by the locator has an attribute which does not contain the given value.
306      * @param locator Locator given in standard selenium syntax
307      * @param attribute The element attribute
308      * @param value The value expected to be found in the element's attribute
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      * Asserts that a link containing the given text appears on the page
318      * @param text The text that a link on the page should contain
319      * @see #linkVisibleWithText(String) also
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      * Asserts that no link exists on the page containing the given text
328      * @param text The text that no link on the page should contain
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      * Asserts that a link containin the given text is present and visible.
337      * @param text The text that a link on the page should contain
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      * Asserts that two elements (located by selenium syntax) are vertically within deltaPixels of each other.
347      * @param locator1 Locator for element 1 given in standard selenium syntax
348      * @param locator2 Locator for element 2 given in standard selenium syntax
349      * @param deltaPixels The maximum allowable distance between the two element
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      * Asserts that an element contains the given text.
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      * Asserts that an element does not contain the given text.
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 }