Skip to content

Commit 978b341

Browse files
committed
WebAssert messages reviewed and improved
1 parent 8525aab commit 978b341

File tree

3 files changed

+70
-76
lines changed

3 files changed

+70
-76
lines changed

src/changes/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
<body>
1010
<release version="4.15.0" date="August 17, 2025" description="Chrome/Edge 139, Firefox 141, core-js, Bugfixes">
11+
<action type="update" dev="rbri">
12+
WebAssert messages reviewed and improved.
13+
</action>
14+
<action type="update" dev="rbri">
15+
WebAssert got various improvements and more complete unit tests.
16+
</action>
1117
<action type="update" dev="rbri">
1218
Improve WebAssert javadoc.
1319
</action>

src/main/java/org/htmlunit/WebAssert.java

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* @author Daniel Gredler
4343
* @author Mike Bowler
4444
* @author Ahmed Ashour
45-
* @author Ronald Broöö
45+
* @author Ronald Brill
4646
*/
4747
public final class WebAssert {
4848

@@ -64,7 +64,7 @@ private WebAssert() {
6464
public static void assertTitleEquals(final HtmlPage page, final String title) {
6565
final String s = page.getTitleText();
6666
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 + "'.";
6868
throw new AssertionError(msg);
6969
}
7070
}
@@ -80,7 +80,7 @@ public static void assertTitleEquals(final HtmlPage page, final String title) {
8080
public static void assertTitleContains(final HtmlPage page, final String titlePortion) {
8181
final String s = page.getTitleText();
8282
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 + "'.";
8484
throw new AssertionError(msg);
8585
}
8686
}
@@ -96,7 +96,7 @@ public static void assertTitleContains(final HtmlPage page, final String titlePo
9696
public static void assertTitleMatches(final HtmlPage page, final String regex) {
9797
final String s = page.getTitleText();
9898
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 + "'.";
100100
throw new AssertionError(msg);
101101
}
102102
}
@@ -114,7 +114,7 @@ public static void assertElementPresent(final HtmlPage page, final String id) {
114114
page.getHtmlElementById(id);
115115
}
116116
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.";
118118
throw new AssertionError(msg, e);
119119
}
120120
}
@@ -136,8 +136,7 @@ public static void assertElementPresent(final HtmlPage page, final String id) {
136136
public static void assertElementPresentByXPath(final HtmlPage page, final String xpath) {
137137
final List<?> elements = page.getByXPath(xpath);
138138
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 + "'.";
141140
throw new AssertionError(msg);
142141
}
143142
}
@@ -146,7 +145,7 @@ public static void assertElementPresentByXPath(final HtmlPage page, final String
146145
* Verifies that the specified page does not contain an element with the specified ID.
147146
*
148147
* @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
150149
* @throws AssertionError if an element with the specified ID is found
151150
* @throws NullPointerException if page or id is null
152151
*/
@@ -157,7 +156,7 @@ public static void assertElementNotPresent(final HtmlPage page, final String id)
157156
catch (final ElementNotFoundException e) {
158157
return;
159158
}
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.";
161160
throw new AssertionError(msg);
162161
}
163162

@@ -166,15 +165,15 @@ public static void assertElementNotPresent(final HtmlPage page, final String id)
166165
* expression.
167166
*
168167
* @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
170169
* @throws AssertionError if any elements match the XPath expression
171170
*/
172171
public static void assertElementNotPresentByXPath(final HtmlPage page, final String xpath) {
173172
final List<?> elements = page.getByXPath(xpath);
174173
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 + "'.";
178177
throw new AssertionError(msg);
179178
}
180179
}
@@ -189,7 +188,7 @@ public static void assertElementNotPresentByXPath(final HtmlPage page, final Str
189188
*/
190189
public static void assertTextPresent(final HtmlPage page, final String text) {
191190
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.";
193192
throw new AssertionError(msg);
194193
}
195194
}
@@ -209,13 +208,12 @@ public static void assertTextPresentInElement(final HtmlPage page, final String
209208
try {
210209
final HtmlElement element = page.getHtmlElementById(id);
211210
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 + "'.";
213212
throw new AssertionError(msg);
214213
}
215214
}
216215
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.";
219217
throw new AssertionError(msg, e);
220218
}
221219
}
@@ -230,7 +228,7 @@ public static void assertTextPresentInElement(final HtmlPage page, final String
230228
*/
231229
public static void assertTextNotPresent(final HtmlPage page, final String text) {
232230
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.";
234232
throw new AssertionError(msg);
235233
}
236234
}
@@ -247,13 +245,12 @@ public static void assertTextNotPresentInElement(final HtmlPage page, final Stri
247245
try {
248246
final HtmlElement element = page.getHtmlElementById(id);
249247
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 + "'.";
251249
throw new AssertionError(msg);
252250
}
253251
}
254252
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.";
257254
throw new AssertionError(msg);
258255
}
259256
}
@@ -272,7 +269,7 @@ public static void assertLinkPresent(final HtmlPage page, final String id) {
272269
page.getDocumentElement().getOneHtmlElementByAttribute("a", DomElement.ID_ATTRIBUTE, id);
273270
}
274271
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.";
276273
throw new AssertionError(msg, e);
277274
}
278275
}
@@ -289,11 +286,11 @@ public static void assertLinkPresent(final HtmlPage page, final String id) {
289286
public static void assertLinkNotPresent(final HtmlPage page, final String id) {
290287
try {
291288
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.";
294290
throw new AssertionError(msg);
295291
}
296292
catch (final ElementNotFoundException expected) {
293+
// Expected behavior - link should not be present
297294
}
298295
}
299296

@@ -313,7 +310,7 @@ public static void assertLinkPresentWithText(final HtmlPage page, final String t
313310
}
314311
}
315312
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.";
317314
throw new AssertionError(msg);
318315
}
319316
}
@@ -334,7 +331,7 @@ public static void assertLinkNotPresentWithText(final HtmlPage page, final Strin
334331
}
335332
}
336333
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.";
338335
throw new AssertionError(msg);
339336
}
340337
}
@@ -352,7 +349,7 @@ public static void assertFormPresent(final HtmlPage page, final String name) {
352349
page.getFormByName(name);
353350
}
354351
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.";
356353
throw new AssertionError(msg, e);
357354
}
358355
}
@@ -372,7 +369,7 @@ public static void assertFormNotPresent(final HtmlPage page, final String name)
372369
catch (final ElementNotFoundException e) {
373370
return;
374371
}
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.";
376373
throw new AssertionError(msg);
377374
}
378375

@@ -389,7 +386,7 @@ public static void assertInputPresent(final HtmlPage page, final String name) {
389386
final String xpath = "//input[@name='" + name + "']";
390387
final List<?> list = page.getByXPath(xpath);
391388
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.");
393390
}
394391
}
395392

@@ -405,7 +402,7 @@ public static void assertInputNotPresent(final HtmlPage page, final String name)
405402
final String xpath = "//input[@name='" + name + "']";
406403
final List<?> list = page.getByXPath(xpath);
407404
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.");
409406
}
410407
}
411408

@@ -421,13 +418,13 @@ public static void assertInputContainsValue(final HtmlPage page, final String na
421418
final String xpath = "//input[@name='" + name + "']";
422419
final List<?> list = page.getByXPath(xpath);
423420
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.");
425422
}
426423
final HtmlInput input = (HtmlInput) list.get(0);
427424
final String s = input.getValue();
428425
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 + "'.");
431428
}
432429
}
433430

@@ -443,13 +440,12 @@ public static void assertInputDoesNotContainValue(final HtmlPage page, final Str
443440
final String xpath = "//input[@name='" + name + "']";
444441
final List<?> list = page.getByXPath(xpath);
445442
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.");
447444
}
448445
final HtmlInput input = (HtmlInput) list.get(0);
449446
final String s = input.getValue();
450447
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 + "'.");
453449
}
454450
}
455451

@@ -477,7 +473,7 @@ public static void assertAllTabIndexAttributesSet(final HtmlPage page) {
477473
final Short tabIndex = element.getTabIndex();
478474
if (tabIndex == null || HtmlElement.TAB_INDEX_OUT_OF_BOUNDS.equals(tabIndex)) {
479475
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.");
481477
}
482478
}
483479
}
@@ -499,7 +495,7 @@ public static void assertAllAccessKeyAttributesUnique(final HtmlPage page) {
499495
final String key = element.getAttributeDirect("accesskey");
500496
if (key != null && !key.isEmpty()) {
501497
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.");
503499
}
504500
list.add(key);
505501
}
@@ -519,7 +515,7 @@ public static void assertAllIdAttributesUnique(final HtmlPage page) {
519515
final String id = element.getId();
520516
if (id != null && !id.isEmpty()) {
521517
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.");
523519
}
524520
list.add(id);
525521
}

0 commit comments

Comments
 (0)