View Javadoc

1   package com.atlassian.webdriver.utils;
2   
3   import org.openqa.selenium.By;
4   import org.openqa.selenium.SearchContext;
5   import org.openqa.selenium.WebDriver;
6   import org.openqa.selenium.WebElement;
7   
8   import java.util.Iterator;
9   import java.util.List;
10  
11  /**
12   * Search utilties
13   */
14  public class Search
15  {
16  
17      private Search() {}
18  
19      /**
20       * Searches for an element that contains a specific child element. This is useful when multiple
21       * elements on a page are the same but a child node is unqiue.
22       *
23       * @param searchElements the parent elements to look for the child element within.
24       * @param childFind The child to look for.
25       * @return The parent web element that contains the child element.
26       */
27      public static WebElement findElementWithChildElement(By searchElements, By childFind, WebDriver driver)
28      {
29          return findElementWithChildElement(searchElements, childFind, driver.findElement(By.tagName("body")));
30      }
31  
32      public static WebElement findElementWithChildElement(By searchElements, By childFind, WebElement context)
33      {
34          List<WebElement> elements = context.findElements(searchElements);
35          Iterator<WebElement> iter = elements.iterator();
36  
37          while (iter.hasNext())
38          {
39              WebElement el = iter.next();
40              if (Check.elementExists(childFind, el))
41              {
42                  return el;
43              }
44          }
45  
46          return null;
47      }
48  
49      public static WebElement findElementWithText(By searchElements, String textValue, WebDriver driver)
50      {
51          return findElementWithText(searchElements, textValue, driver.findElement(By.tagName("body")));
52      }
53  
54      public static WebElement findElementWithText(By searchElements, String textValue, WebElement context)
55      {
56          List<WebElement> elements = context.findElements(searchElements);
57          Iterator<WebElement> iter = elements.iterator();
58  
59          while (iter.hasNext())
60          {
61              WebElement el = iter.next();
62              if (el.getText().equals(textValue))
63              {
64                  return el;
65              }
66          }
67  
68          return null;
69      }
70  
71      public static List<WebElement> findVisibleElements(By searchElements, SearchContext context)
72      {
73          List<WebElement> visibleElements = new java.util.ArrayList<WebElement>();
74          for (WebElement element : context.findElements(searchElements))
75          {
76              if (element.isDisplayed())
77              {
78                  visibleElements.add(element);
79              }
80          }
81          return visibleElements;
82      }
83  
84  
85  }