View Javadoc

1   package com.atlassian.webdriver.utils;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.openqa.selenium.*;
5   
6   import java.util.List;
7   
8   
9   /**
10   * Utilities for doing simple checks on a page.
11   *
12   * @since 2.0
13   */
14  public final class Check
15  {
16  
17      private Check()
18      {
19          throw new AssertionError(Check.class.getName() + " is not supposed to be instantiated");
20      }
21  
22      /**
23       * Checks that an element that matches the by param exists within another element.
24       */
25      public static boolean elementExists(By by, SearchContext el)
26      {
27          try
28          {
29              el.findElement(by);
30          }
31          catch (NoSuchElementException e)
32          {
33              return false;
34          }
35          return true;
36      }
37  
38      public static boolean elementIsVisible(By by, SearchContext context)
39      {
40          try
41          {
42              WebElement lookFor = context.findElement(by);
43              return isVisible(lookFor);
44          }
45          catch (NoSuchElementException e)
46          {
47              return false;
48          }
49      }
50  
51      public static boolean elementsAreVisible(By by, SearchContext context)
52      {
53          List<WebElement> elements = context.findElements(by);
54  
55          if (elements.size() > 0)
56          {
57              for (WebElement lookFor : elements)
58              {
59                  if (!isVisible(lookFor))
60                  {
61                      return false;
62                  }
63              }
64  
65              return true;
66          }
67  
68          return false;
69      }
70  
71      private static boolean isVisible(WebElement webElement)
72      {
73          try
74          {
75              return webElement.isDisplayed();
76          }
77          catch (StaleElementReferenceException e)
78          {
79              // element got stale since it's been retrieve, just return false naively
80              return false;
81          }
82      }
83  
84      /**
85       * Checks to see if a specified element contains a specific class of not. The check is case-insensitive.
86       *
87       * @param className CSS class name to check for
88       * @param element element to check
89       * @return <code>true</code>, if <tt>element</tt> has CSS class with given <tt>className</tt>
90       */
91      public static boolean hasClass(String className, WebElement element)
92      {
93          final String classNameLowerCase = className.toLowerCase();
94          String classValue = element.getAttribute("class");
95          if (StringUtils.isEmpty(classValue))
96          {
97              return false;
98          }
99          classValue = classValue.toLowerCase();
100         if (!classValue.contains(classNameLowerCase))
101         {
102             return false;
103         }
104         for (String singleClass : classValue.split("\\s+"))
105         {
106             if (classNameLowerCase.equals(singleClass))
107             {
108                 return true;
109             }
110         }
111         return false;
112     }
113 
114 }