27
27
/**
28
28
* Utility class which contains standard assertions for HTML pages.
29
29
*
30
+ * <p>This class provides a collection of static assertion methods for testing
31
+ * HTML page content, structure, and behavior. All assertion methods throw
32
+ * {@link AssertionError} when the expected condition is not met.</p>
33
+ *
34
+ * <p>Common use cases include:</p>
35
+ * <ul>
36
+ * <li>Verifying page titles and content</li>
37
+ * <li>Checking for presence/absence of elements</li>
38
+ * <li>Validating form inputs and links</li>
39
+ * <li>Ensuring accessibility attributes are properly set</li>
40
+ * </ul>
41
+ *
30
42
* @author Daniel Gredler
31
43
* @author Mike Bowler
32
44
* @author Ahmed Ashour
45
+ * @author Ronald Broöö
33
46
*/
34
47
public final class WebAssert {
35
48
@@ -45,6 +58,8 @@ private WebAssert() {
45
58
*
46
59
* @param page the page to check
47
60
* @param title the expected title
61
+ * @throws AssertionError if the page title does not match the expected title
62
+ * @throws NullPointerException if page or title is null
48
63
*/
49
64
public static void assertTitleEquals (final HtmlPage page , final String title ) {
50
65
final String s = page .getTitleText ();
@@ -59,6 +74,8 @@ public static void assertTitleEquals(final HtmlPage page, final String title) {
59
74
*
60
75
* @param page the page to check
61
76
* @param titlePortion the substring which the page title is expected to contain
77
+ * @throws AssertionError if the page title does not contain the substring
78
+ * @throws NullPointerException if page or titlePortion is null
62
79
*/
63
80
public static void assertTitleContains (final HtmlPage page , final String titlePortion ) {
64
81
final String s = page .getTitleText ();
@@ -73,6 +90,8 @@ public static void assertTitleContains(final HtmlPage page, final String titlePo
73
90
*
74
91
* @param page the page to check
75
92
* @param regex the regular expression that the page title is expected to match
93
+ * @throws AssertionError if the page title does not match the regular expression
94
+ * @throws NullPointerException if page or regex is null
76
95
*/
77
96
public static void assertTitleMatches (final HtmlPage page , final String regex ) {
78
97
final String s = page .getTitleText ();
@@ -86,7 +105,9 @@ public static void assertTitleMatches(final HtmlPage page, final String regex) {
86
105
* Verifies that the specified page contains an element with the specified ID.
87
106
*
88
107
* @param page the page to check
89
- * @param id the expected ID of an element in the page
108
+ * @param id the ID of an element expected in the page
109
+ * @throws AssertionError if no element with the specified ID is found
110
+ * @throws NullPointerException if page or id is null
90
111
*/
91
112
public static void assertElementPresent (final HtmlPage page , final String id ) {
92
113
try {
@@ -101,8 +122,16 @@ public static void assertElementPresent(final HtmlPage page, final String id) {
101
122
/**
102
123
* Verifies that the specified page contains an element matching the specified XPath expression.
103
124
*
125
+ * <p><b>Example usage:</b></p>
126
+ * <pre>{@code
127
+ * WebAssert.assertElementPresentByXPath(page, "//div[@class='error']");
128
+ * WebAssert.assertElementPresentByXPath(page, "//input[@type='submit' and @value='Login']");
129
+ * }</pre>
130
+ *
104
131
* @param page the page to check
105
132
* @param xpath the XPath expression which is expected to match an element in the page
133
+ * @throws AssertionError if no elements match the XPath expression
134
+ * @throws NullPointerException if page or xpath is null
106
135
*/
107
136
public static void assertElementPresentByXPath (final HtmlPage page , final String xpath ) {
108
137
final List <?> elements = page .getByXPath (xpath );
@@ -118,6 +147,8 @@ public static void assertElementPresentByXPath(final HtmlPage page, final String
118
147
*
119
148
* @param page the page to check
120
149
* @param id the ID of an element which expected to not exist on the page
150
+ * @throws AssertionError if an element with the specified ID is found
151
+ * @throws NullPointerException if page or id is null
121
152
*/
122
153
public static void assertElementNotPresent (final HtmlPage page , final String id ) {
123
154
try {
@@ -136,12 +167,13 @@ public static void assertElementNotPresent(final HtmlPage page, final String id)
136
167
*
137
168
* @param page the page to check
138
169
* @param xpath the XPath expression which is expected to not match an element in the page
170
+ * @throws AssertionError if any elements match the XPath expression
139
171
*/
140
172
public static void assertElementNotPresentByXPath (final HtmlPage page , final String xpath ) {
141
173
final List <?> elements = page .getByXPath (xpath );
142
174
if (!elements .isEmpty ()) {
143
- final String msg = "The page does not contain any elements matching the XPath expression ' " + xpath
144
- + "'." ;
175
+ final String msg = "The page contains " + elements . size ()
176
+ + " element(s) matching the XPath expression '" + xpath + "'." ;
145
177
throw new AssertionError (msg );
146
178
}
147
179
}
@@ -151,6 +183,8 @@ public static void assertElementNotPresentByXPath(final HtmlPage page, final Str
151
183
*
152
184
* @param page the page to check
153
185
* @param text the text to check for
186
+ * @throws AssertionError if the page does not contain the specified text
187
+ * @throws NullPointerException if page or text is null
154
188
*/
155
189
public static void assertTextPresent (final HtmlPage page , final String text ) {
156
190
if (!page .asNormalizedText ().contains (text )) {
@@ -166,6 +200,9 @@ public static void assertTextPresent(final HtmlPage page, final String text) {
166
200
* @param page the page to check
167
201
* @param text the text to check for
168
202
* @param id the ID of the element which is expected to contain the specified text
203
+ * @throws AssertionError if the element does not contain the specified text
204
+ * @throws ElementNotFoundException if no element with the specified ID exists
205
+ * @throws NullPointerException if any parameter is null
169
206
*/
170
207
public static void assertTextPresentInElement (final HtmlPage page , final String text , final String id ) {
171
208
try {
@@ -187,6 +224,8 @@ public static void assertTextPresentInElement(final HtmlPage page, final String
187
224
*
188
225
* @param page the page to check
189
226
* @param text the text to check for
227
+ * @throws AssertionError if the page contains the specified text
228
+ * @throws NullPointerException if page or text is null
190
229
*/
191
230
public static void assertTextNotPresent (final HtmlPage page , final String text ) {
192
231
if (page .asNormalizedText ().contains (text )) {
@@ -223,6 +262,9 @@ public static void assertTextNotPresentInElement(final HtmlPage page, final Stri
223
262
*
224
263
* @param page the page to check
225
264
* @param id the ID of the link which the page is expected to contain
265
+ * @throws AssertionError if no link with the specified ID is found
266
+ * @see #assertLinkNotPresent(HtmlPage, String)
267
+ * @see #assertLinkPresentWithText(HtmlPage, String)
226
268
*/
227
269
public static void assertLinkPresent (final HtmlPage page , final String id ) {
228
270
try {
@@ -239,6 +281,9 @@ public static void assertLinkPresent(final HtmlPage page, final String id) {
239
281
*
240
282
* @param page the page to check
241
283
* @param id the ID of the link which the page is expected to not contain
284
+ * @throws AssertionError if a link with the specified ID is found
285
+ * @see #assertLinkPresent(HtmlPage, String)
286
+ * @see #assertLinkNotPresentWithText(HtmlPage, String)
242
287
*/
243
288
public static void assertLinkNotPresent (final HtmlPage page , final String id ) {
244
289
try {
@@ -298,6 +343,8 @@ public static void assertLinkNotPresentWithText(final HtmlPage page, final Strin
298
343
*
299
344
* @param page the page to check
300
345
* @param name the expected name of a form on the page
346
+ * @throws AssertionError if no form with the specified name is found
347
+ * @see #assertFormNotPresent(HtmlPage, String)
301
348
*/
302
349
public static void assertFormPresent (final HtmlPage page , final String name ) {
303
350
try {
@@ -314,6 +361,8 @@ public static void assertFormPresent(final HtmlPage page, final String name) {
314
361
*
315
362
* @param page the page to check
316
363
* @param name the name of a form which should not exist on the page
364
+ * @throws AssertionError if a form with the specified name is found
365
+ * @see #assertFormPresent(HtmlPage, String)
317
366
*/
318
367
public static void assertFormNotPresent (final HtmlPage page , final String name ) {
319
368
try {
@@ -331,6 +380,9 @@ public static void assertFormNotPresent(final HtmlPage page, final String name)
331
380
*
332
381
* @param page the page to check
333
382
* @param name the name of the input element to look for
383
+ * @throws AssertionError if no input element with the specified name is found
384
+ * @see #assertInputNotPresent(HtmlPage, String)
385
+ * @see #assertInputContainsValue(HtmlPage, String, String)
334
386
*/
335
387
public static void assertInputPresent (final HtmlPage page , final String name ) {
336
388
final String xpath = "//input[@name='" + name + "']" ;
@@ -345,12 +397,14 @@ public static void assertInputPresent(final HtmlPage page, final String name) {
345
397
*
346
398
* @param page the page to check
347
399
* @param name the name of the input element to look for
400
+ * @throws AssertionError if an input element with the specified name is found
401
+ * @throws NullPointerException if page or name is null
348
402
*/
349
403
public static void assertInputNotPresent (final HtmlPage page , final String name ) {
350
404
final String xpath = "//input[@name='" + name + "']" ;
351
405
final List <?> list = page .getByXPath (xpath );
352
406
if (!list .isEmpty ()) {
353
- throw new AssertionError ("Unable to find an input element named '" + name + "'." );
407
+ throw new AssertionError ("Found an input element named '" + name + "' when none was expected ." );
354
408
}
355
409
}
356
410
@@ -405,9 +459,13 @@ public static void assertInputDoesNotContainValue(final HtmlPage page, final Str
405
459
* all tabbable elements should have the <code>tabindex</code> attribute set.</p>
406
460
*
407
461
* <p>This method verifies that all tabbable elements have a valid value set for
408
- * the <code>tabindex</code> attribute.</p>
462
+ * the <code>tabindex</code> attribute. Valid values are positive integers,
463
+ * 0 (for default tab order), or -1 (to exclude from tab order).</p>
464
+ *
465
+ * <p>The following elements are checked: a, area, button, input, object, select, textarea</p>
409
466
*
410
467
* @param page the page to check
468
+ * @throws AssertionError if any tabbable element has an invalid or missing tabindex attribute
411
469
*/
412
470
public static void assertAllTabIndexAttributesSet (final HtmlPage page ) {
413
471
final List <String > tags =
@@ -429,7 +487,10 @@ public static void assertAllTabIndexAttributesSet(final HtmlPage page) {
429
487
* keyboard navigation. This method verifies that all the <code>accesskey</code> attributes on the
430
488
* specified page are unique.
431
489
*
490
+ * <p>Duplicate access keys can confuse users and make keyboard navigation unpredictable.</p>
491
+ *
432
492
* @param page the page to check
493
+ * @throws AssertionError if any access key is used more than once on the page
433
494
*/
434
495
public static void assertAllAccessKeyAttributesUnique (final HtmlPage page ) {
435
496
final List <String > list = new ArrayList <>();
@@ -448,6 +509,8 @@ public static void assertAllAccessKeyAttributesUnique(final HtmlPage page) {
448
509
* Verifies that all element IDs in the specified page are unique.
449
510
*
450
511
* @param page the page to check
512
+ * @throws AssertionError if any element ID is used more than once on the page
513
+ * @throws NullPointerException if page is null
451
514
*/
452
515
public static void assertAllIdAttributesUnique (final HtmlPage page ) {
453
516
final List <String > list = new ArrayList <>();
@@ -465,8 +528,10 @@ public static void assertAllIdAttributesUnique(final HtmlPage page) {
465
528
/**
466
529
* Assert that the specified parameter is not null. Throw a NullPointerException
467
530
* if a null is found.
531
+ *
468
532
* @param description the description to pass into the NullPointerException
469
533
* @param object the object to check for null
534
+ * @throws NullPointerException if the object is null
470
535
*/
471
536
public static void notNull (final String description , final Object object ) {
472
537
if (object == null ) {
0 commit comments