42
42
* @author Daniel Gredler
43
43
* @author Mike Bowler
44
44
* @author Ahmed Ashour
45
- * @author Ronald Broöö
45
+ * @author Ronald Brill
46
46
*/
47
47
public final class WebAssert {
48
48
@@ -64,7 +64,7 @@ private WebAssert() {
64
64
public static void assertTitleEquals (final HtmlPage page , final String title ) {
65
65
final String s = page .getTitleText ();
66
66
if (!title .equals (s )) {
67
- final String msg = "Actual page title '" + s + "' does not match expected page title '" + title + "'." ;
67
+ final String msg = "Page title '" + s + "' does not match expected title '" + title + "'." ;
68
68
throw new AssertionError (msg );
69
69
}
70
70
}
@@ -80,7 +80,7 @@ public static void assertTitleEquals(final HtmlPage page, final String title) {
80
80
public static void assertTitleContains (final HtmlPage page , final String titlePortion ) {
81
81
final String s = page .getTitleText ();
82
82
if (!s .contains (titlePortion )) {
83
- final String msg = "Page title '" + s + "' does not contain the substring '" + titlePortion + "'." ;
83
+ final String msg = "Page title '" + s + "' does not contain the expected substring '" + titlePortion + "'." ;
84
84
throw new AssertionError (msg );
85
85
}
86
86
}
@@ -96,7 +96,7 @@ public static void assertTitleContains(final HtmlPage page, final String titlePo
96
96
public static void assertTitleMatches (final HtmlPage page , final String regex ) {
97
97
final String s = page .getTitleText ();
98
98
if (!s .matches (regex )) {
99
- final String msg = "Page title '" + s + "' does not match the regular expression '" + regex + "'." ;
99
+ final String msg = "Page title '" + s + "' does not match the expected regular expression '" + regex + "'." ;
100
100
throw new AssertionError (msg );
101
101
}
102
102
}
@@ -114,7 +114,7 @@ public static void assertElementPresent(final HtmlPage page, final String id) {
114
114
page .getHtmlElementById (id );
115
115
}
116
116
catch (final ElementNotFoundException e ) {
117
- final String msg = "The page does not contain an element with ID '" + id + "'." ;
117
+ final String msg = "Expected element with ID '" + id + "' was not found on the page ." ;
118
118
throw new AssertionError (msg , e );
119
119
}
120
120
}
@@ -136,8 +136,7 @@ public static void assertElementPresent(final HtmlPage page, final String id) {
136
136
public static void assertElementPresentByXPath (final HtmlPage page , final String xpath ) {
137
137
final List <?> elements = page .getByXPath (xpath );
138
138
if (elements .isEmpty ()) {
139
- final String msg = "The page does not contain any elements matching the XPath expression '" + xpath
140
- + "'." ;
139
+ final String msg = "No elements found matching the XPath expression '" + xpath + "'." ;
141
140
throw new AssertionError (msg );
142
141
}
143
142
}
@@ -146,7 +145,7 @@ public static void assertElementPresentByXPath(final HtmlPage page, final String
146
145
* Verifies that the specified page does not contain an element with the specified ID.
147
146
*
148
147
* @param page the page to check
149
- * @param id the ID of an element which expected to not exist on the page
148
+ * @param id the ID of an element which is expected to not exist on the page
150
149
* @throws AssertionError if an element with the specified ID is found
151
150
* @throws NullPointerException if page or id is null
152
151
*/
@@ -157,7 +156,7 @@ public static void assertElementNotPresent(final HtmlPage page, final String id)
157
156
catch (final ElementNotFoundException e ) {
158
157
return ;
159
158
}
160
- final String msg = "The page contains an element with ID '" + id + "' but should not ." ;
159
+ final String msg = "Found unexpected element with ID '" + id + "' on the page ." ;
161
160
throw new AssertionError (msg );
162
161
}
163
162
@@ -166,15 +165,15 @@ public static void assertElementNotPresent(final HtmlPage page, final String id)
166
165
* expression.
167
166
*
168
167
* @param page the page to check
169
- * @param xpath the XPath expression which is expected to not match an element in the page
168
+ * @param xpath the XPath expression which is expected to not match any element in the page
170
169
* @throws AssertionError if any elements match the XPath expression
171
170
*/
172
171
public static void assertElementNotPresentByXPath (final HtmlPage page , final String xpath ) {
173
172
final List <?> elements = page .getByXPath (xpath );
174
173
if (!elements .isEmpty ()) {
175
- final String msg = "The page contains " + elements .size ()
176
- + " element(s) matching the XPath expression '"
177
- + xpath + "' but should not contain any ." ;
174
+ final String msg = "Found " + elements .size ()
175
+ + " unexpected element(s) matching the XPath expression '"
176
+ + xpath + "'." ;
178
177
throw new AssertionError (msg );
179
178
}
180
179
}
@@ -189,7 +188,7 @@ public static void assertElementNotPresentByXPath(final HtmlPage page, final Str
189
188
*/
190
189
public static void assertTextPresent (final HtmlPage page , final String text ) {
191
190
if (!page .asNormalizedText ().contains (text )) {
192
- final String msg = "The page does not contain the text '" + text + "'." ;
191
+ final String msg = "Expected text '" + text + "' was not found on the page ." ;
193
192
throw new AssertionError (msg );
194
193
}
195
194
}
@@ -209,13 +208,12 @@ public static void assertTextPresentInElement(final HtmlPage page, final String
209
208
try {
210
209
final HtmlElement element = page .getHtmlElementById (id );
211
210
if (!element .asNormalizedText ().contains (text )) {
212
- final String msg = "The element with ID '" + id + "' does not contain the text '" + text + "'." ;
211
+ final String msg = "Element with ID '" + id + "' does not contain the expected text '" + text + "'." ;
213
212
throw new AssertionError (msg );
214
213
}
215
214
}
216
215
catch (final ElementNotFoundException e ) {
217
- final String msg = "Unable to verify that the element with ID '" + id + "' contains the text '" + text
218
- + "' because the specified element does not exist." ;
216
+ final String msg = "Cannot verify text content: element with ID '" + id + "' was not found on the page." ;
219
217
throw new AssertionError (msg , e );
220
218
}
221
219
}
@@ -230,7 +228,7 @@ public static void assertTextPresentInElement(final HtmlPage page, final String
230
228
*/
231
229
public static void assertTextNotPresent (final HtmlPage page , final String text ) {
232
230
if (page .asNormalizedText ().contains (text )) {
233
- final String msg = "The page contains the text '" + text + "' but should not ." ;
231
+ final String msg = "Found unexpected text '" + text + "' on the page ." ;
234
232
throw new AssertionError (msg );
235
233
}
236
234
}
@@ -247,13 +245,12 @@ public static void assertTextNotPresentInElement(final HtmlPage page, final Stri
247
245
try {
248
246
final HtmlElement element = page .getHtmlElementById (id );
249
247
if (element .asNormalizedText ().contains (text )) {
250
- final String msg = "The element with ID '" + id + "' contains the text '" + text + "' but should not ." ;
248
+ final String msg = "Element with ID '" + id + "' contains unexpected text '" + text + "'." ;
251
249
throw new AssertionError (msg );
252
250
}
253
251
}
254
252
catch (final ElementNotFoundException e ) {
255
- final String msg = "Unable to verify that the element with ID '" + id + "' does not contain the text '"
256
- + text + "' because the specified element does not exist." ;
253
+ final String msg = "Cannot verify text content: element with ID '" + id + "' was not found on the page." ;
257
254
throw new AssertionError (msg );
258
255
}
259
256
}
@@ -272,7 +269,7 @@ public static void assertLinkPresent(final HtmlPage page, final String id) {
272
269
page .getDocumentElement ().getOneHtmlElementByAttribute ("a" , DomElement .ID_ATTRIBUTE , id );
273
270
}
274
271
catch (final ElementNotFoundException e ) {
275
- final String msg = "The page does not contain a link with ID '" + id + "'." ;
272
+ final String msg = "Expected link with ID '" + id + "' was not found on the page ." ;
276
273
throw new AssertionError (msg , e );
277
274
}
278
275
}
@@ -289,11 +286,11 @@ public static void assertLinkPresent(final HtmlPage page, final String id) {
289
286
public static void assertLinkNotPresent (final HtmlPage page , final String id ) {
290
287
try {
291
288
page .getDocumentElement ().getOneHtmlElementByAttribute ("a" , DomElement .ID_ATTRIBUTE , id );
292
- // Not expected.
293
- final String msg = "The page contains a link with ID '" + id + "' but should not." ;
289
+ final String msg = "Found unexpected link with ID '" + id + "' on the page." ;
294
290
throw new AssertionError (msg );
295
291
}
296
292
catch (final ElementNotFoundException expected ) {
293
+ // Expected behavior - link should not be present
297
294
}
298
295
}
299
296
@@ -313,7 +310,7 @@ public static void assertLinkPresentWithText(final HtmlPage page, final String t
313
310
}
314
311
}
315
312
if (!found ) {
316
- final String msg = "The page does not contain a link with text '" + text + "'." ;
313
+ final String msg = "Expected link containing text '" + text + "' was not found on the page ." ;
317
314
throw new AssertionError (msg );
318
315
}
319
316
}
@@ -334,7 +331,7 @@ public static void assertLinkNotPresentWithText(final HtmlPage page, final Strin
334
331
}
335
332
}
336
333
if (found ) {
337
- final String msg = "The page contains a link with text '" + text + "' but should not ." ;
334
+ final String msg = "Found unexpected link containing text '" + text + "' on the page ." ;
338
335
throw new AssertionError (msg );
339
336
}
340
337
}
@@ -352,7 +349,7 @@ public static void assertFormPresent(final HtmlPage page, final String name) {
352
349
page .getFormByName (name );
353
350
}
354
351
catch (final ElementNotFoundException e ) {
355
- final String msg = "The page does not contain a form named '" + name + "'." ;
352
+ final String msg = "Expected form with name '" + name + "' was not found on the page ." ;
356
353
throw new AssertionError (msg , e );
357
354
}
358
355
}
@@ -372,7 +369,7 @@ public static void assertFormNotPresent(final HtmlPage page, final String name)
372
369
catch (final ElementNotFoundException e ) {
373
370
return ;
374
371
}
375
- final String msg = "The page contains a form named '" + name + "' but should not ." ;
372
+ final String msg = "Found unexpected form with name '" + name + "' on the page ." ;
376
373
throw new AssertionError (msg );
377
374
}
378
375
@@ -389,7 +386,7 @@ public static void assertInputPresent(final HtmlPage page, final String name) {
389
386
final String xpath = "//input[@name='" + name + "']" ;
390
387
final List <?> list = page .getByXPath (xpath );
391
388
if (list .isEmpty ()) {
392
- throw new AssertionError ("Unable to find an input element named '" + name + "'." );
389
+ throw new AssertionError ("Expected input element with name '" + name + "' was not found on the page ." );
393
390
}
394
391
}
395
392
@@ -405,7 +402,7 @@ public static void assertInputNotPresent(final HtmlPage page, final String name)
405
402
final String xpath = "//input[@name='" + name + "']" ;
406
403
final List <?> list = page .getByXPath (xpath );
407
404
if (!list .isEmpty ()) {
408
- throw new AssertionError ("Found an input element named '" + name + "' when none was expected ." );
405
+ throw new AssertionError ("Found unexpected input element with name '" + name + "' on the page ." );
409
406
}
410
407
}
411
408
@@ -421,13 +418,13 @@ public static void assertInputContainsValue(final HtmlPage page, final String na
421
418
final String xpath = "//input[@name='" + name + "']" ;
422
419
final List <?> list = page .getByXPath (xpath );
423
420
if (list .isEmpty ()) {
424
- throw new AssertionError ("Unable to find an input element named '" + name + "'." );
421
+ throw new AssertionError ("Expected input element with name '" + name + "' was not found on the page ." );
425
422
}
426
423
final HtmlInput input = (HtmlInput ) list .get (0 );
427
424
final String s = input .getValue ();
428
425
if (!s .equals (value )) {
429
- throw new AssertionError ("The input element named '" + name + "' contains the value '" + s
430
- + "', not the expected value '" + value + "'." );
426
+ throw new AssertionError ("Input element '" + name + "' has value '" + s
427
+ + "' but expected '" + value + "'." );
431
428
}
432
429
}
433
430
@@ -443,13 +440,12 @@ public static void assertInputDoesNotContainValue(final HtmlPage page, final Str
443
440
final String xpath = "//input[@name='" + name + "']" ;
444
441
final List <?> list = page .getByXPath (xpath );
445
442
if (list .isEmpty ()) {
446
- throw new AssertionError ("Unable to find an input element named '" + name + "'." );
443
+ throw new AssertionError ("Expected input element with name '" + name + "' was not found on the page ." );
447
444
}
448
445
final HtmlInput input = (HtmlInput ) list .get (0 );
449
446
final String s = input .getValue ();
450
447
if (s .equals (value )) {
451
- throw new AssertionError ("The input element named '" + name + "' contains the value '" + s
452
- + "', not the expected value '" + value + "'." );
448
+ throw new AssertionError ("Input element '" + name + "' has unexpected value '" + s + "'." );
453
449
}
454
450
}
455
451
@@ -477,7 +473,7 @@ public static void assertAllTabIndexAttributesSet(final HtmlPage page) {
477
473
final Short tabIndex = element .getTabIndex ();
478
474
if (tabIndex == null || HtmlElement .TAB_INDEX_OUT_OF_BOUNDS .equals (tabIndex )) {
479
475
final String s = element .getAttributeDirect ("tabindex" );
480
- throw new AssertionError ("Illegal value for tab index: '" + s + "'." );
476
+ throw new AssertionError ("Invalid tabindex value '" + s + "' found on element ." );
481
477
}
482
478
}
483
479
}
@@ -499,7 +495,7 @@ public static void assertAllAccessKeyAttributesUnique(final HtmlPage page) {
499
495
final String key = element .getAttributeDirect ("accesskey" );
500
496
if (key != null && !key .isEmpty ()) {
501
497
if (list .contains (key )) {
502
- throw new AssertionError ("The access key '" + key + "' is not unique ." );
498
+ throw new AssertionError ("Duplicate access key '" + key + "' found on the page ." );
503
499
}
504
500
list .add (key );
505
501
}
@@ -519,7 +515,7 @@ public static void assertAllIdAttributesUnique(final HtmlPage page) {
519
515
final String id = element .getId ();
520
516
if (id != null && !id .isEmpty ()) {
521
517
if (list .contains (id )) {
522
- throw new AssertionError ("The element ID '" + id + "' is not unique ." );
518
+ throw new AssertionError ("Duplicate element ID '" + id + "' found on the page ." );
523
519
}
524
520
list .add (id );
525
521
}
0 commit comments