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   import java.util.Arrays;
8   
9   /**
10   * This provides helper methods for selenium assertions.  This
11   * mostly means waiting for events to occur (i.e. a dropdown to
12   * appear after a certain timeout, etc)
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       * This will wait until an element is visible.  If it doesnt become visible in
36       * maxMillis fail.
37       *
38       * @param locator   the selenium element locator
39       * @param maxMillis how long to wait as most in milliseconds
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      * This will wait until an element is not present.  If it doesnt become not present in
118      * maxMillis
119      *
120      * @param locator   the selenium element locator
121      * @param maxMillis how long to wait as most in milliseconds
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      * @param text Asserts that text is present in the current page
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      * @param text Asserts that text is not present in the current page
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      * Asserts that a given element has a specified value
208      * @param locator Locator for element using the standard selenium locator syntax
209      * @param value The value the element is expected to contain
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      * Asserts that a given element is present
218      * @param locator Locator for the element that should be present given using the standard selenium locator syntax
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      * Asserts that a given element is not present on the current page
227      * @param locator Locator for the element that should not be present given using the standard selenium locator syntax
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      * Asserts that a given element is present and is visible. Under some browsers just calling the seleinium.isVisible method
236      * on an element that doesn't exist causes selenium to throw an exception.
237      * @param locator Locator for the element that should be visible specified in the standard selenium syntax
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      * Asserts that a given element is not present and visible. Calling selenium's native selenium.isVisible method on
246      * an element that doesn't exist causes selenium to throw an exception
247      * @param locator Locator for the element that should not be visible specified in the standard selenium syntax
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      * Asserts that a particular piece of HTML is present in the HTML source. It is recommended that the elementPresent, elementHasText or some other method
256      * be used because browsers idiosyncratically add white space to the HTML source
257      * @param html Lower case representation of HTML string that should not be present
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      * Asserts that a particular piece of HTML is not present in the HTML source. It is recommended that the elementNotPresent, elementDoesntHaveText or
266      * some other method be used because browsers idiosyncratically add white space to the HTML source
267      * @param html Lower case representation of HTML string that should not be present
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      * Asserts that the element specified by the locator contains the specified text
276      * @param locator Locator given in standard selenium syntax
277      * @param text The text that the element designated by the locator should contain
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      * Asserts that the element specified by the locator does not contain the specified text
286      * @param locator Locator given in standard selenium syntax
287      * @param text The text that the element designated by the locator should not contain
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      * Asserts that the element given by the locator has an attribute which contains the required value.
296      * @param locator Locator given in standard selenium syntax
297      * @param attribute The element attribute
298      * @param value The value expected to be found in the element's attribute
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      * Asserts that the element given by the locator has an attribute which does not contain the given value.
308      * @param locator Locator given in standard selenium syntax
309      * @param attribute The element attribute
310      * @param value The value expected to be found in the element's attribute
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      * Asserts that a link containing the given text appears on the page
320      * @param text The text that a link on the page should contain
321      * @see #linkVisibleWithText(String) also
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      * Asserts that no link exists on the page containing the given text
330      * @param text The text that no link on the page should contain
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      * Asserts that a link containin the given text is present and visible.
339      * @param text The text that a link on the page should contain
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      * Asserts that two elements (located by selenium syntax) are vertically within deltaPixels of each other.
349      * @param locator1 Locator for element 1 given in standard selenium syntax
350      * @param locator2 Locator for element 2 given in standard selenium syntax
351      * @param deltaPixels The maximum allowable distance between the two element
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      * Asserts that an element contains the given text.
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      * Asserts that an element does not contain the given text.
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 }