View Javadoc

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