View Javadoc

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