View Javadoc

1   package com.atlassian.webdriver;
2   
3   import com.atlassian.annotations.PublicApi;
4   import com.google.common.base.Function;
5   import com.google.common.base.Predicates;
6   import com.google.common.collect.FluentIterable;
7   import com.google.common.collect.ImmutableSet;
8   import org.apache.commons.lang.StringUtils;
9   
10  import javax.annotation.Nonnull;
11  import javax.annotation.Nullable;
12  import java.util.Collections;
13  import java.util.Set;
14  
15  import static com.google.common.base.Preconditions.checkNotNull;
16  
17  /**
18   * Utilities related to manipulating/querying of web elements (of any sort).
19   * <p/>
20   * NOTE: this class does not directly depend on any flavour of elements (e.g. WebDriver's
21   * {@link org.openqa.selenium.WebElement}).
22   *
23   * @since 2.3
24   */
25  @PublicApi
26  public final class Elements
27  {
28      public static final String TAG_BODY = "body";
29      public static final String TAG_DIV = "div";
30      public static final String TAG_TR = "tr";
31      public static final String TAG_TD = "td";
32      public static final String TAG_UL = "ul";
33      public static final String TAG_LI = "li";
34  
35      public static final String ATTRIBUTE_CLASS = "class";
36      public static final String ATTRIBUTE_ID = "id";
37  
38      private Elements()
39      {
40          throw new AssertionError("Do not instantiate " + getClass().getSimpleName());
41      }
42  
43      /**
44       * Extract CSS class names from the CSS "class" attribute value.
45       *
46       * @param rawClassValue the CSS "class" attribute value
47       * @return set extracted CSS class names, or an empty set, if {@code rawClassValue} is {@code null},
48       * or a blank string; all extracted class names will be lower cased
49       */
50      @Nonnull
51      @SuppressWarnings("ConstantConditions")
52      public static Set<String> getCssClasses(@Nullable String rawClassValue)
53      {
54          if (StringUtils.isBlank(rawClassValue))
55          {
56              return Collections.emptySet();
57          }
58  
59          return FluentIterable.from(ImmutableSet.copyOf(rawClassValue.split("\\s+")))
60                  .transform(new Function<String, String>()
61                  {
62                      @Nullable
63                      @Override
64                      public String apply(@Nullable String cssClass)
65                      {
66                          return StringUtils.isBlank(cssClass) ? null : cssClass.toLowerCase();
67                      }
68                  })
69                  .filter(Predicates.notNull())
70                  .toSet();
71      }
72  
73      /**
74       * Checks to see if a raw {@code rawClassValue} contains a specific class of not. The check is case-insensitive.
75       *
76       * @param expectedClassName CSS class name to check for
77       * @param rawClassValue raw value of the "class" attribute
78       * @return {@code true}, if {@code rawClassValue} contains CSS {@code expectedClassName} (case-insensitive),
79       * {@code false} otherwise
80       */
81      public static boolean hasCssClass(@Nonnull String expectedClassName, @Nullable String rawClassValue)
82      {
83          checkNotNull(expectedClassName, "className");
84  
85          return rawClassValue != null && getCssClasses(rawClassValue).contains(expectedClassName.toLowerCase());
86      }
87  }