1   package com.atlassian.selenium;
2   
3   import com.thoughtworks.selenium.SeleniumException;
4   
5   import java.util.List;
6   import java.util.LinkedList;
7   
8   public class XpathHelper
9   {
10      public static void clickButtonWithClass(String className, SeleniumClient client)
11      {
12          clickElementWithClass("button", className, client);
13      }
14  
15      public static void clickElementWithClass(String elementName, String className, SeleniumClient client)
16      {
17          client.click("//" + elementWithClass(elementName, className));
18      }
19  
20      public static String elementWithClass(String className)
21      {
22          return elementWithClass("*", className);
23      }
24  
25      public static String elementWithClass(String elementName, String className)
26      {
27          return elementName + "[contains(@class, '" + className + "')]";
28      }
29  
30      public static String[] getAllMatchingAttributes(String query, String attribute, SeleniumClient client)
31      {
32          List<String> matches = new LinkedList<String>();
33          try
34          {
35              int i = 1;
36              for(String attr = getAttribute(client, query, attribute, i++);
37                  attr != null;
38                  attr = getAttribute(client, query, attribute, i++))
39              {
40                  matches.add(attr);
41              }
42          }
43          catch (SeleniumException se)
44          {
45             // This exception will be thrown to break the for loop
46             //  the implementation of xpathcount seems to return too many matches.
47              // So using it to work out how many times to loop was problematic
48  
49          }
50          return matches.toArray(new String[matches.size()]);
51      }
52  
53      private static String getAttribute(SeleniumClient client, String query, String attribute, int i)
54      {
55          return client.getAttribute(query + "[" + i + "]/@" + attribute);
56      }
57  }
58