-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathWebAssertTest.java
More file actions
411 lines (324 loc) · 16.6 KB
/
WebAssertTest.java
File metadata and controls
411 lines (324 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Copyright (c) 2002-2026 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.htmlunit.html.HtmlPage;
import org.junit.jupiter.api.Test;
/**
* Tests for {@link WebAssert}.
*
* @author Daniel Gredler
* @author Ronald Brill
*/
public class WebAssertTest extends SimpleWebTestCase {
/**
* @throws Exception if an error occurs
*/
@Test
public void assertTitleEquals() throws Exception {
final String html = DOCTYPE_HTML + "<html><head><title>foo</title></head><body>bar</body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertTitleEquals(page, "foo");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertTitleEquals(page, "bar"));
assertEquals("Page title 'foo' does not match expected title 'bar'.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertTitleContains() throws Exception {
final String html = DOCTYPE_HTML + "<html><head><title>foo</title></head><body>bar</body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertTitleContains(page, "o");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertTitleContains(page, "a"));
assertEquals("Page title 'foo' does not contain the expected substring 'a'.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertTitleMatches() throws Exception {
final String html = DOCTYPE_HTML + "<html><head><title>foo</title></head><body>bar</body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertTitleMatches(page, "f..");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertTitleMatches(page, "b.."));
assertEquals("Page title 'foo' does not match the expected regular expression 'b..'.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertElementPresent() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertElementPresent(page, "a");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertElementPresent(page, "b"));
assertEquals("Expected element with ID 'b' was not found on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertElementPresentByXPath() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertElementPresentByXPath(page, "html/body/div");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertElementPresentByXPath(page, "ul"));
assertEquals("No elements found matching the XPath expression 'ul'.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertElementNotPresent() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertElementNotPresent(page, "b");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertElementNotPresent(page, "a"));
assertEquals("Found unexpected element with ID 'a' on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertElementNotPresentByXPath() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertElementNotPresentByXPath(page, "ul");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertElementNotPresentByXPath(page, "html/body/div"));
assertEquals("Found 1 unexpected element(s) matching the XPath expression 'html/body/div'.",
error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertTextPresent() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertTextPresent(page, "bar");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertTextPresent(page, "baz"));
assertEquals("Expected text 'baz' was not found on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertTextPresentInElement() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertTextPresentInElement(page, "bar", "a");
AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertTextPresentInElement(page, "baz", "a"));
assertEquals("Element with ID 'a' does not contain the expected text 'baz'.", error.getMessage());
error = assertThrows(AssertionError.class, () -> WebAssert.assertTextPresentInElement(page, "bar", "b"));
assertEquals("Cannot verify text content: element with ID 'b' was not found on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertTextNotPresent() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertTextNotPresent(page, "baz");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertTextNotPresent(page, "bar"));
assertEquals("Found unexpected text 'bar' on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertTextNotPresentInElement() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><div id='a'>bar</div></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertTextNotPresentInElement(page, "baz", "a");
AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertTextNotPresentInElement(page, "bar", "a"));
assertEquals("Element with ID 'a' contains unexpected text 'bar'.", error.getMessage());
error = assertThrows(AssertionError.class, () -> WebAssert.assertTextNotPresentInElement(page, "bar", "b"));
assertEquals("Cannot verify text content: element with ID 'b' was not found on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertLinkPresent() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertLinkPresent(page, "x");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertLinkPresent(page, "z"));
assertEquals("Expected link with ID 'z' was not found on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertLinkNotPresent() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertLinkNotPresent(page, "z");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertLinkNotPresent(page, "x"));
assertEquals("Found unexpected link with ID 'x' on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertLinkPresentWithText() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertLinkPresentWithText(page, "r");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertLinkPresentWithText(page, "x"));
assertEquals("Expected link containing text 'x' was not found on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertLinkNotPresentWithText() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertLinkNotPresentWithText(page, "x");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertLinkNotPresentWithText(page, "r"));
assertEquals("Found unexpected link containing text 'r' on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertFormPresent() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><form name='f'>bar</form></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertFormPresent(page, "f");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertFormPresent(page, "x"));
assertEquals("Expected form with name 'x' was not found on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertFormNotPresent() throws Exception {
final String html = DOCTYPE_HTML + "<html><body><form name='f'>bar</form></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertFormNotPresent(page, "x");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertFormNotPresent(page, "f"));
assertEquals("Found unexpected form with name 'f' on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertInputPresent() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertInputPresent(page, "i");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertInputPresent(page, "q"));
assertEquals("Expected input element with name 'q' was not found on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertInputNotPresent() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertInputNotPresent(page, "q");
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertInputNotPresent(page, "i"));
assertEquals("Found unexpected input element with name 'i' on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertInputContainsValue() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertInputContainsValue(page, "i", "x");
AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertInputContainsValue(page, "i", "z"));
assertEquals("Input element 'i' has value 'x' but expected 'z'.", error.getMessage());
error = assertThrows(AssertionError.class, () -> WebAssert.assertInputContainsValue(page, "q", "x"));
assertEquals("Expected input element with name 'q' was not found on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertInputDoesNotContainValue() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
final HtmlPage page = loadPage(html);
WebAssert.assertInputDoesNotContainValue(page, "i", "z");
AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertInputDoesNotContainValue(page, "i", "x"));
assertEquals("Input element 'i' has unexpected value 'x'.", error.getMessage());
error = assertThrows(AssertionError.class, () -> WebAssert.assertInputDoesNotContainValue(page, "q", "x"));
assertEquals("Expected input element with name 'q' was not found on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertAllTabIndexAttributesSet() throws Exception {
final String html1 = DOCTYPE_HTML + "<html><body><a href='#' tabindex='1'>foo</a></body></html>";
final HtmlPage page1 = loadPage(html1);
WebAssert.assertAllTabIndexAttributesSet(page1);
final String html2 = DOCTYPE_HTML + "<html><body><a href='#'>foo</a></body></html>";
final HtmlPage page2 = loadPage(html2);
AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertAllTabIndexAttributesSet(page2));
assertEquals("Invalid tabindex value '' found on element.", error.getMessage());
final String html3 = DOCTYPE_HTML + "<html><body><a href='#' tabindex='x'>foo</a></body></html>";
final HtmlPage page3 = loadPage(html3);
error = assertThrows(AssertionError.class, () -> WebAssert.assertAllTabIndexAttributesSet(page3));
assertEquals("Invalid tabindex value 'x' found on element.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertAllAccessKeyAttributesUnique() throws Exception {
final String html1 = DOCTYPE_HTML + "<html><body><a accesskey='k'>foo</a></body></html>";
final HtmlPage page1 = loadPage(html1);
WebAssert.assertAllAccessKeyAttributesUnique(page1);
final String html2 = DOCTYPE_HTML
+ "<html><body><a accesskey='k'>foo</a><a accesskey='k'>bar</a></body></html>";
final HtmlPage page2 = loadPage(html2);
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertAllAccessKeyAttributesUnique(page2));
assertEquals("Duplicate access key 'k' found on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertAllIdAttributesUnique() throws Exception {
final String html1 = DOCTYPE_HTML + "<html><body><a id='k'>foo</a></body></html>";
final HtmlPage page1 = loadPage(html1);
WebAssert.assertAllIdAttributesUnique(page1);
final String html2 = DOCTYPE_HTML + "<html><body><a id='k'>foo</a><a id='k'>bar</a></body></html>";
final HtmlPage page2 = loadPage(html2);
final AssertionError error = assertThrows(AssertionError.class, () -> WebAssert.assertAllIdAttributesUnique(page2));
assertEquals("Duplicate element ID 'k' found on the page.", error.getMessage());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void assertTitleEqualsNullHandling() throws Exception {
final String html = DOCTYPE_HTML + "<html><head><title>foo</title></head><body>bar</body></html>";
final HtmlPage page = loadPage(html);
assertThrows(NullPointerException.class, () -> WebAssert.assertTitleEquals(null, "title"), "Should throw NullPointerException when page is null");
assertThrows(NullPointerException.class, () -> WebAssert.assertTitleEquals(page, null), "Should throw NullPointerException when title is null");
}
}