1 package com.atlassian.pageobjects.elements.testing;
2
3 import com.atlassian.annotations.PublicApi;
4 import com.atlassian.pageobjects.elements.CheckboxElement;
5 import com.atlassian.pageobjects.elements.PageElement;
6 import com.atlassian.webdriver.Elements;
7 import org.hamcrest.FeatureMatcher;
8 import org.hamcrest.Matcher;
9
10 import javax.annotation.Nonnull;
11 import javax.annotation.Nullable;
12 import java.util.Set;
13
14 import static com.atlassian.pageobjects.elements.PageElements.DATA_PREFIX;
15 import static com.google.common.base.Preconditions.checkNotNull;
16 import static java.lang.String.format;
17 import static org.hamcrest.Matchers.hasItem;
18 import static org.hamcrest.Matchers.is;
19
20
21
22
23
24
25 @PublicApi
26 public final class PageElementMatchers
27 {
28 private PageElementMatchers()
29 {
30 throw new AssertionError("Do not instantiate " + PageElementMatchers.class.getName());
31 }
32
33 @Nonnull
34 @SuppressWarnings("unchecked")
35 public static <PE extends PageElement, PEE extends PE> Matcher<PEE> asMatcherOf(@Nonnull Class<PEE> targetType,
36 @Nonnull Matcher<PE> original)
37 {
38 checkNotNull(targetType, "targetType");
39 checkNotNull(original, "original");
40
41 return (Matcher) original;
42 }
43
44 @Nonnull
45 public static Matcher<CheckboxElement> isChecked()
46 {
47 return new FeatureMatcher<CheckboxElement, Boolean>(is(true), "is checked", "is checked")
48 {
49 @Override
50 protected Boolean featureValueOf(CheckboxElement checkbox)
51 {
52 return checkbox.isChecked();
53 }
54 };
55 }
56
57 @Nonnull
58 public static Matcher<PageElement> withAttribute(@Nonnull final String name, @Nullable String expectedValue)
59 {
60 return withAttributeThat(name, is(expectedValue));
61 }
62
63 @Nonnull
64 public static Matcher<PageElement> withAttributeThat(@Nonnull final String name,
65 @Nonnull Matcher<String> valueMatcher)
66 {
67 checkNotNull(name, "name");
68 checkNotNull(valueMatcher, "valueMatcher");
69 String featureDescription = format("attribute '%s'", name);
70
71 return new FeatureMatcher<PageElement, String>(valueMatcher, featureDescription, featureDescription)
72 {
73 @Override
74 protected String featureValueOf(PageElement actual)
75 {
76 return actual.getAttribute(name);
77 }
78 };
79 }
80
81 @Nonnull
82 public static Matcher<PageElement> withClass(@Nonnull String expectedClass)
83 {
84 return withClassThat(is(expectedClass));
85 }
86
87 @Nonnull
88 public static Matcher<PageElement> withClassThat(@Nonnull Matcher<String> classMatcher)
89 {
90 checkNotNull(classMatcher, "classMatcher");
91
92 return new FeatureMatcher<PageElement, Set<String>>(hasItem(classMatcher), "CSS class", "CSS class")
93 {
94 @Override
95 protected Set<String> featureValueOf(PageElement actual)
96 {
97 return actual.getCssClasses();
98 }
99 };
100 }
101
102 @Nonnull
103 public static Matcher<PageElement> withDataAttribute(@Nonnull final String name, @Nullable String expectedValue)
104 {
105 return withDataAttributeThat(name, is(expectedValue));
106 }
107
108 @Nonnull
109 public static Matcher<PageElement> withDataAttributeThat(@Nonnull final String name,
110 @Nonnull Matcher<String> valueMatcher)
111 {
112 checkNotNull(name, "name");
113
114 return withAttributeThat(DATA_PREFIX + name, valueMatcher);
115 }
116
117 @Nonnull
118 public static Matcher<PageElement> withId(@Nullable String expectedId)
119 {
120 return withIdThat(is(expectedId));
121 }
122
123 @Nonnull
124 public static Matcher<PageElement> withIdThat(@Nonnull Matcher<String> idMatcher)
125 {
126 return withAttributeThat(Elements.ATTRIBUTE_ID, idMatcher);
127 }
128
129 @Nonnull
130 public static Matcher<PageElement> withText(@Nullable String expectedText)
131 {
132 return withTextThat(is(expectedText));
133 }
134
135 @Nonnull
136 public static Matcher<PageElement> withTextThat(@Nonnull Matcher<String> textMatcher)
137 {
138 checkNotNull(textMatcher, "textMatcher");
139
140 return new FeatureMatcher<PageElement, String>(textMatcher, "text", "text")
141 {
142 @Override
143 protected String featureValueOf(PageElement actual)
144 {
145 return actual.getText();
146 }
147 };
148 }
149 }