1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import static org.junit.Assert.fail;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.htmlunit.SimpleWebTestCase;
23 import org.htmlunit.WebClient;
24 import org.htmlunit.junit.BrowserRunner;
25 import org.htmlunit.junit.annotation.Alerts;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.w3c.dom.NodeList;
29
30
31
32
33
34
35
36
37
38
39
40
41
42 @RunWith(BrowserRunner.class)
43 public class HtmlElementTest extends SimpleWebTestCase {
44
45
46
47
48
49 @Test
50 public void hasAttributeWith() throws Exception {
51 final String html = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
52 final HtmlPage page = loadPage(html);
53
54 final HtmlElement node = page.getHtmlElementById("tag");
55 assertTrue("Element should have attribute", node.hasAttribute("id"));
56 }
57
58
59
60
61
62 @Test
63 public void hasAttributeWithMissingValue() throws Exception {
64 final String html = DOCTYPE_HTML + "<html><head></head><body id='tag' attrib>text</body></html>";
65 final HtmlPage page = loadPage(html);
66
67 final HtmlElement node = page.getHtmlElementById("tag");
68 assertTrue("Element should have attribute", node.hasAttribute("attrib"));
69 }
70
71
72
73
74
75 @Test
76 public void hasAttributeNone() throws Exception {
77 final String html = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
78 final HtmlPage page = loadPage(html);
79
80 final HtmlElement node = page.getHtmlElementById("tag");
81 assertFalse("Element should not have attribute", node.hasAttribute("foo"));
82 }
83
84
85
86
87
88 @Test
89 public void hasAttributeNSWith() throws Exception {
90 final String html = DOCTYPE_HTML
91 + "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
92 final HtmlPage page = loadPage(html);
93
94 final HtmlElement node = page.getHtmlElementById("tag");
95 assertTrue("Element should have attribute", node.hasAttributeNS("http://foobar", "foo"));
96 }
97
98
99
100
101
102 @Test
103 public void hasAttributeNSNone() throws Exception {
104 final String html = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
105 final HtmlPage page = loadPage(html);
106
107 final HtmlElement node = page.getHtmlElementById("tag");
108 assertFalse("Element should not have attribute", node.hasAttributeNS("http://foobar", "foo"));
109 }
110
111
112
113
114
115 @Test
116 public void getAttributeWith() throws Exception {
117 final String html = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
118 final HtmlPage page = loadPage(html);
119
120 final HtmlElement node = page.getHtmlElementById("tag");
121 assertEquals("Element should have attribute", "tag", node.getId());
122 }
123
124
125
126
127
128 @Test
129 public void getAttributeWithMissingValue() throws Exception {
130 final String html = DOCTYPE_HTML + "<html><head></head><body id='tag' attrib>text</body></html>";
131 final HtmlPage page = loadPage(html);
132
133 final HtmlElement node = page.getHtmlElementById("tag");
134 assertEquals("", node.getAttribute("attrib"));
135 assertTrue(DomElement.ATTRIBUTE_VALUE_EMPTY == node.getAttribute("attrib"));
136 }
137
138
139
140
141
142 @Test
143 public void getAttributeWithEmptyValue() throws Exception {
144 final String html = DOCTYPE_HTML + "<html><head></head><body id='tag' attrib=''>text</body></html>";
145 final HtmlPage page = loadPage(html);
146
147 final HtmlElement node = page.getHtmlElementById("tag");
148 assertEquals("", node.getAttribute("attrib"));
149 assertTrue(DomElement.ATTRIBUTE_VALUE_EMPTY == node.getAttribute("attrib"));
150 }
151
152
153
154
155
156 @Test
157 public void getAttributeNone() throws Exception {
158 final String html = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
159 final HtmlPage page = loadPage(html);
160
161 final HtmlElement node = page.getHtmlElementById("tag");
162 assertEquals("Element should not have attribute", "", node.getAttribute("foo"));
163 }
164
165
166
167
168
169 @Test
170 public void getAttributeNSWith() throws Exception {
171 final String html = DOCTYPE_HTML
172 + "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
173 final HtmlPage page = loadPage(html);
174
175 final HtmlElement node = page.getHtmlElementById("tag");
176 assertEquals("Element should have attribute", "bar", node.getAttributeNS("http://foobar", "foo"));
177 }
178
179
180
181
182
183 @Test
184 public void getAttributeNSNone() throws Exception {
185 final String html = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
186 final HtmlPage page = loadPage(html);
187
188 final HtmlElement node = page.getHtmlElementById("tag");
189 assertEquals("Element should not have attribute", "", node.getAttributeNS("http://foobar", "foo"));
190 }
191
192
193
194
195
196 @Test
197 public void getNamespaceURIWith() throws Exception {
198 final String html = DOCTYPE_HTML
199 + "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
200 final HtmlPage page = loadPage(html);
201
202 final HtmlElement node = page.getHtmlElementById("tag");
203 for (final DomAttr attr : node.getAttributesMap().values()) {
204 if ("ns:foo".equals(attr.getName())) {
205 assertEquals("Element should have a namespace URI", "http://foobar", attr.getNamespaceURI());
206 return;
207 }
208 }
209 fail("Attribute ns:foo not found.");
210 }
211
212
213
214
215
216 @Test
217 public void getNamespaceURINone() throws Exception {
218 final String html = DOCTYPE_HTML
219 + "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
220 final HtmlPage page = loadPage(html);
221
222 final HtmlElement node = page.getHtmlElementById("tag");
223 for (final DomAttr attr : node.getAttributesMap().values()) {
224 if ("id".equals(attr.getName())) {
225 assertEquals("Element should not have a namespace URI", null, attr.getNamespaceURI());
226 return;
227 }
228 }
229 fail("Attribute ns:foo not found.");
230 }
231
232
233
234
235
236 @Test
237 public void getLocalNameWith() throws Exception {
238 final String html = DOCTYPE_HTML
239 + "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
240 final HtmlPage page = loadPage(html);
241
242 final HtmlElement node = page.getHtmlElementById("tag");
243 for (final DomAttr attr : node.getAttributesMap().values()) {
244 if ("ns:foo".equals(attr.getName())) {
245 assertEquals("Element should have a local name", "foo", attr.getLocalName());
246 return;
247 }
248 }
249 fail("Attribute ns:foo not found.");
250 }
251
252
253
254
255
256 @Test
257 public void getLocalNameNone() throws Exception {
258 final String html = DOCTYPE_HTML
259 + "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
260 final HtmlPage page = loadPage(html);
261
262 final HtmlElement node = page.getHtmlElementById("tag");
263 for (final DomAttr attr : node.getAttributesMap().values()) {
264 if ("id".equals(attr.getName())) {
265
266 assertEquals("Element should not have a local name", "id", attr.getLocalName());
267 return;
268 }
269 }
270 fail("Attribute ns:foo not found.");
271 }
272
273
274
275
276
277 @Test
278 public void getPrefixWith() throws Exception {
279 final String html = DOCTYPE_HTML
280 + "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
281 final HtmlPage page = loadPage(html);
282
283 final HtmlElement node = page.getHtmlElementById("tag");
284 for (final DomAttr attr : node.getAttributesMap().values()) {
285 if ("ns:foo".equals(attr.getName())) {
286 assertEquals("Element should have a prefix", "ns", attr.getPrefix());
287 return;
288 }
289 }
290 fail("Attribute ns:foo not found.");
291 }
292
293
294
295
296
297 @Test
298 public void getPrefixNone() throws Exception {
299 final String html = DOCTYPE_HTML
300 + "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
301 final HtmlPage page = loadPage(html);
302
303 final HtmlElement node = page.getHtmlElementById("tag");
304 for (final DomAttr attr : node.getAttributesMap().values()) {
305 if ("id".equals(attr.getName())) {
306 assertEquals("Element should not have a prefix", null, attr.getPrefix());
307 return;
308 }
309 }
310 fail("Attribute ns:foo not found.");
311 }
312
313
314
315
316
317 @Test
318 public void setPrefix() throws Exception {
319 final String html = DOCTYPE_HTML
320 + "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
321 final HtmlPage page = loadPage(html);
322
323 final HtmlElement node = page.getHtmlElementById("tag");
324 for (final DomAttr attr : node.getAttributesMap().values()) {
325 if ("ns:foo".equals(attr.getName())) {
326 attr.setPrefix("other");
327 assertEquals("Element should have a changed prefix", "other", attr.getPrefix());
328 assertEquals("setPrefix should change qualified name", "other:foo", attr.getName());
329 return;
330 }
331 }
332 fail("Attribute ns:foo not found.");
333 }
334
335
336
337
338
339 @Test
340 public void setAttributeWith() throws Exception {
341 final String html = DOCTYPE_HTML
342 + "<html><head></head><body id='tag'>text</body></html>";
343 final HtmlPage page = loadPage(html);
344
345 final HtmlElement node = page.getHtmlElementById("tag");
346 node.setAttribute("id", "other");
347 assertEquals("Element should have attribute", "other", node.getId());
348 }
349
350
351
352
353
354 @Test
355 public void setAttributeNone() throws Exception {
356 final String html = DOCTYPE_HTML
357 + "<html><head></head><body id='tag'>text</body></html>";
358 final HtmlPage page = loadPage(html);
359
360 final HtmlElement node = page.getHtmlElementById("tag");
361 node.setAttribute("foo", "other");
362 assertEquals("Element should have attribute", "other", node.getAttribute("foo"));
363 }
364
365
366
367
368
369 @Test
370 public void setAttributeNSWith() throws Exception {
371 final String html = DOCTYPE_HTML
372 + "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
373 final HtmlPage page = loadPage(html);
374
375 final HtmlElement node = page.getHtmlElementById("tag");
376 node.setAttributeNS("http://foobar", "ns:foo", "other");
377 assertEquals("Element should have attribute", "other", node.getAttributeNS("http://foobar", "foo"));
378 }
379
380
381
382
383
384 @Test
385 public void setAttributeNSNone() throws Exception {
386 final String html = DOCTYPE_HTML
387 + "<html><head></head><body id='tag'>text</body></html>";
388 final HtmlPage page = loadPage(html);
389
390 final HtmlElement node = page.getHtmlElementById("tag");
391 node.setAttributeNS("http://foobar", "ns:foo", "other");
392 assertEquals("Element should not have attribute", "other", node.getAttributeNS("http://foobar", "foo"));
393 }
394
395
396
397
398
399 @Test
400 public void removeAttributeWith() throws Exception {
401 final String html = DOCTYPE_HTML
402 + "<html><head></head><body id='tag'>text</body></html>";
403 final HtmlPage page = loadPage(html);
404
405 final HtmlElement node = page.getHtmlElementById("tag");
406 node.removeAttribute("id");
407 assertEquals("Element should not have removed attribute", "", node.getId());
408 }
409
410
411
412
413
414 @Test
415 public void removeAttributeNone() throws Exception {
416 final String html = DOCTYPE_HTML
417 + "<html><head></head><body id='tag'>text</body></html>";
418 final HtmlPage page = loadPage(html);
419
420 final HtmlElement node = page.getHtmlElementById("tag");
421 node.removeAttribute("foo");
422 assertEquals("Element should not have attribute", "", node.getAttribute("foo"));
423 }
424
425
426
427
428
429 @Test
430 public void removeAttributeNSWith() throws Exception {
431 final String html = DOCTYPE_HTML
432 + "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
433 final HtmlPage page = loadPage(html);
434
435 final HtmlElement node = page.getHtmlElementById("tag");
436 node.removeAttributeNS("http://foobar", "foo");
437 assertEquals("Element should not have removed attribute", "",
438 node.getAttributeNS("http://foobar", "foo"));
439 }
440
441
442
443
444
445 @Test
446 public void removeAttributeNSNone() throws Exception {
447 final String html = DOCTYPE_HTML
448 + "<html><head></head><body id='tag'>text</body></html>";
449 final HtmlPage page = loadPage(html);
450
451 final HtmlElement node = page.getHtmlElementById("tag");
452 node.removeAttributeNS("http://foobar", "foo");
453 assertEquals("Element should not have attribute", "", node.getAttributeNS("http://foobar", "foo"));
454 }
455
456
457
458
459 @Test
460 public void getEnclosingForm() throws Exception {
461 final String htmlContent = DOCTYPE_HTML
462 + "<html><head><title>foo</title></head><body>\n"
463 + "<form id='form1'>\n"
464 + "<table><tr><td><input type='text' id='foo'/></td></tr></table>\n"
465 + "</form></body></html>";
466 final HtmlPage page = loadPage(htmlContent);
467 final HtmlForm form = page.getHtmlElementById("form1");
468
469 final HtmlInput input = page.getHtmlElementById("foo");
470 assertSame(form, input.getEnclosingForm());
471 }
472
473
474
475
476 @Test
477 public void getEnclosing() throws Exception {
478 final String htmlContent = DOCTYPE_HTML
479 + "<html><head><title>foo</title></head><body>\n"
480 + "<form id='form1'>\n"
481 + "<table id='table1'>\n"
482 + "<tr id='tr1'><td id='td1'>foo</td></tr>\n"
483 + "<tr id='tr2'><td id='td2'>foo</td></tr>\n"
484 + "</table>\n"
485 + "</form></body></html>";
486 final HtmlPage page = loadPage(htmlContent);
487
488 final HtmlElement td1 = page.getHtmlElementById("td1");
489 assertEquals("tr1", td1.getEnclosingElement("tr").getId());
490 assertEquals("tr1", td1.getEnclosingElement("TR").getId());
491 assertEquals("table1", td1.getEnclosingElement("table").getId());
492 assertEquals("form1", td1.getEnclosingElement("form").getId());
493
494 final HtmlElement td2 = page.getHtmlElementById("td2");
495 assertEquals("tr2", td2.getEnclosingElement("tr").getId());
496 assertEquals("tr2", td2.getEnclosingElement("TR").getId());
497 assertEquals("table1", td2.getEnclosingElement("table").getId());
498 assertEquals("form1", td2.getEnclosingElement("form").getId());
499 }
500
501
502
503
504 @Test
505 public void asNormalizedTextWithComments() throws Exception {
506 final String htmlContent = DOCTYPE_HTML
507 + "<html><head><title>foo</title></head><body>\n"
508 + "<p id='p1'>foo<!--bar--></p>\n"
509 + "</body></html>";
510 final HtmlPage page = loadPage(htmlContent);
511 final HtmlElement element = page.getHtmlElementById("p1");
512 assertEquals("foo", element.asNormalizedText());
513 }
514
515
516
517
518 @Test
519 public void constants() {
520 assertEquals("", DomElement.ATTRIBUTE_NOT_DEFINED);
521 assertEquals("", DomElement.ATTRIBUTE_VALUE_EMPTY);
522 assertTrue("Not the same object",
523 DomElement.ATTRIBUTE_NOT_DEFINED != DomElement.ATTRIBUTE_VALUE_EMPTY);
524 }
525
526 static class HtmlAttributeChangeListenerTestImpl implements HtmlAttributeChangeListener {
527 private final List<String> collectedValues_ = new ArrayList<>();
528 @Override
529 @Test
530 public void attributeAdded(final HtmlAttributeChangeEvent event) {
531 collectedValues_.add("attributeAdded: " + event.getHtmlElement().getTagName() + ','
532 + event.getName() + ',' + event.getValue());
533 }
534 @Override
535 @Test
536 public void attributeRemoved(final HtmlAttributeChangeEvent event) {
537 collectedValues_.add("attributeRemoved: " + event.getHtmlElement().getTagName() + ','
538 + event.getName() + ',' + event.getValue());
539 }
540
541 @Override
542 @Test
543 public void attributeReplaced(final HtmlAttributeChangeEvent event) {
544 collectedValues_.add("attributeReplaced: " + event.getHtmlElement().getTagName() + ','
545 + event.getName() + ',' + event.getValue());
546 }
547 List<String> getCollectedValues() {
548 return collectedValues_;
549 }
550 }
551
552
553
554
555 @Test
556 public void htmlAttributeChangeListener_AddAttribute() throws Exception {
557 final String htmlContent = DOCTYPE_HTML
558 + "<html><head><title>foo</title>\n"
559 + "<script>\n"
560 + " function clickMe() {\n"
561 + " var p1 = document.getElementById('p1');\n"
562 + " p1.setAttribute('title', 'myTitle');\n"
563 + " }\n"
564 + "</script>\n"
565 + "</head>\n"
566 + "<body id='myBody'>\n"
567 + "<p id='p1'></p>\n"
568 + "<input id='myButton' type='button' onclick='clickMe()'>\n"
569 + "</body></html>";
570
571 final String[] expectedValues =
572 {"attributeAdded: p,title,myTitle",
573 "attributeAdded: p,title,myTitle",
574 "attributeAdded: p,title,myTitle"};
575 final HtmlPage page = loadPage(htmlContent);
576 final HtmlBody body = page.getHtmlElementById("myBody");
577 final HtmlElement p1 = page.getHtmlElementById("p1");
578
579 final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
580 p1.addHtmlAttributeChangeListener(listenerImpl);
581 body.addHtmlAttributeChangeListener(listenerImpl);
582 page.addHtmlAttributeChangeListener(listenerImpl);
583 final HtmlButtonInput myButton = page.getHtmlElementById("myButton");
584
585 myButton.click();
586 assertEquals(expectedValues, listenerImpl.getCollectedValues());
587 }
588
589
590
591
592 @Test
593 public void htmlAttributeChangeListener_ReplaceAttribute() throws Exception {
594 final String htmlContent = DOCTYPE_HTML
595 + "<html><head><title>foo</title>\n"
596 + "<script>\n"
597 + " function clickMe() {\n"
598 + " var p1 = document.getElementById('p1');\n"
599 + " p1.setAttribute('title', p1.getAttribute('title') + 'a');\n"
600 + " }\n"
601 + "</script>\n"
602 + "</head>\n"
603 + "<body id='myBody'>\n"
604 + "<p id='p1' title='myTitle'></p>\n"
605 + "<input id='myButton' type='button' onclick='clickMe()'>\n"
606 + "</body></html>";
607
608 final String[] expectedValues =
609 {"attributeReplaced: p,title,myTitle",
610 "attributeReplaced: p,title,myTitle",
611 "attributeReplaced: p,title,myTitle"};
612 final HtmlPage page = loadPage(htmlContent);
613 final HtmlBody body = page.getHtmlElementById("myBody");
614 final HtmlElement p1 = page.getHtmlElementById("p1");
615 final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
616 page.addHtmlAttributeChangeListener(listenerImpl);
617 body.addHtmlAttributeChangeListener(listenerImpl);
618 p1.addHtmlAttributeChangeListener(listenerImpl);
619 final HtmlButtonInput myButton = page.getHtmlElementById("myButton");
620
621 myButton.click();
622 assertEquals(expectedValues, listenerImpl.getCollectedValues());
623 assertEquals("myTitle" + 'a', p1.getAttribute("title"));
624 }
625
626
627
628
629 @Test
630 public void htmlAttributeChangeListener_RemoveAttribute() throws Exception {
631 final String htmlContent = DOCTYPE_HTML
632 + "<html><head><title>foo</title>\n"
633 + "<script>\n"
634 + " function clickMe() {\n"
635 + " var p1 = document.getElementById('p1');\n"
636 + " p1.removeAttribute('title');\n"
637 + " }\n"
638 + "</script>\n"
639 + "</head>\n"
640 + "<body id='myBody'>\n"
641 + "<p id='p1' title='myTitle'></p>\n"
642 + "<input id='myButton' type='button' onclick='clickMe()'>\n"
643 + "</body></html>";
644
645 final String[] expectedValues =
646 {"attributeRemoved: p,title,myTitle",
647 "attributeRemoved: p,title,myTitle",
648 "attributeRemoved: p,title,myTitle"};
649 final HtmlPage page = loadPage(htmlContent);
650 final HtmlBody body = page.getHtmlElementById("myBody");
651 final HtmlElement p1 = page.getHtmlElementById("p1");
652 final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
653 page.addHtmlAttributeChangeListener(listenerImpl);
654 body.addHtmlAttributeChangeListener(listenerImpl);
655 p1.addHtmlAttributeChangeListener(listenerImpl);
656 final HtmlButtonInput myButton = page.getHtmlElementById("myButton");
657
658 myButton.click();
659 assertEquals(expectedValues, listenerImpl.getCollectedValues());
660 assertSame(DomElement.ATTRIBUTE_NOT_DEFINED, p1.getAttribute("title"));
661 }
662
663
664
665
666 @Test
667 public void htmlAttributeChangeListener_RemoveListener() throws Exception {
668 final String htmlContent = DOCTYPE_HTML
669 + "<html><head><title>foo</title>\n"
670 + "<script>\n"
671 + " function clickMe() {\n"
672 + " var p1 = document.getElementById('p1');\n"
673 + " p1.setAttribute('title', p1.getAttribute('title') + 'a');\n"
674 + " }\n"
675 + "</script>\n"
676 + "</head>\n"
677 + "<body>\n"
678 + "<p id='p1' title='myTitle'></p>\n"
679 + "<input id='myButton' type='button' onclick='clickMe()'>\n"
680 + "</body></html>";
681
682 final String[] expectedValues = {"attributeReplaced: p,title,myTitle"};
683 final HtmlPage page = loadPage(htmlContent);
684 final HtmlElement p1 = page.getHtmlElementById("p1");
685 final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
686 p1.addHtmlAttributeChangeListener(listenerImpl);
687 final HtmlButtonInput myButton = page.getHtmlElementById("myButton");
688
689 myButton.click();
690 p1.removeHtmlAttributeChangeListener(listenerImpl);
691 myButton.click();
692 assertEquals(expectedValues, listenerImpl.getCollectedValues());
693 assertEquals("myTitle" + 'a' + 'a', p1.getAttribute("title"));
694 }
695
696
697
698
699 @Test
700 public void mouseOver() throws Exception {
701 final String html = DOCTYPE_HTML
702 + "<html>\n"
703 + "<head>\n"
704 + "<script>\n"
705 + " function mouseOverMe() {\n"
706 + " document.getElementById('myTextarea').value+='mouseover-';\n"
707 + " }\n"
708 + "</script>\n"
709 + "</head>\n"
710 + "<body id='myBody' onmouseover='mouseOverMe()'>\n"
711 + "<textarea id='myTextarea'></textarea>\n"
712 + "</body></html>";
713 final HtmlPage page = loadPage(html);
714 final HtmlBody body = page.getHtmlElementById("myBody");
715 body.mouseOver();
716 final HtmlTextArea textArea = page.getHtmlElementById("myTextarea");
717 assertEquals("mouseover-", textArea.getText());
718 }
719
720
721
722
723 @Test
724 public void mouseMove() throws Exception {
725 final String html = DOCTYPE_HTML
726 + "<html>\n"
727 + "<head>\n"
728 + "<script>\n"
729 + " function mouseMoveMe() {\n"
730 + " document.getElementById('myTextarea').value+='mousemove-';\n"
731 + " }\n"
732 + "</script>\n"
733 + "</head>\n"
734 + "<body id='myBody' onmousemove='mouseMoveMe()'>\n"
735 + "<textarea id='myTextarea'></textarea>\n"
736 + "</body></html>";
737 final HtmlPage page = loadPage(html);
738 final HtmlBody body = page.getHtmlElementById("myBody");
739 body.mouseMove();
740 final HtmlTextArea textArea = page.getHtmlElementById("myTextarea");
741 assertEquals("mousemove-", textArea.getText());
742 }
743
744
745
746
747 @Test
748 public void mouseOut() throws Exception {
749 final String html = DOCTYPE_HTML
750 + "<html>\n"
751 + "<head>\n"
752 + "<script>\n"
753 + " function mouseOutMe() {\n"
754 + " document.getElementById('myTextarea').value+='mouseout-';\n"
755 + " }\n"
756 + "</script>\n"
757 + "</head>\n"
758 + "<body id='myBody' onmouseout='mouseOutMe()'>\n"
759 + "<textarea id='myTextarea'></textarea>\n"
760 + "</body></html>";
761 final HtmlPage page = loadPage(html);
762 final HtmlBody body = page.getHtmlElementById("myBody");
763 body.mouseOut();
764 final HtmlTextArea textArea = page.getHtmlElementById("myTextarea");
765 assertEquals("mouseout-", textArea.getText());
766 }
767
768
769
770
771 @Test
772 @Alerts("mousedown-0")
773 public void mouseDown() throws Exception {
774 final String html = DOCTYPE_HTML
775 + "<html>\n"
776 + "<head>\n"
777 + "<script>\n"
778 + " function mouseDownMe(e) {\n"
779 + " document.getElementById('myTextarea').value+='mousedown-' + e.button;\n"
780 + " }\n"
781 + "</script>\n"
782 + "</head>\n"
783 + "<body id='myBody' onmousedown='mouseDownMe(event)'>\n"
784 + "<textarea id='myTextarea'></textarea>\n"
785 + "</body></html>";
786
787 final HtmlPage page = loadPage(html);
788 final HtmlBody body = page.getHtmlElementById("myBody");
789 body.mouseDown();
790 final HtmlTextArea textArea = page.getHtmlElementById("myTextarea");
791 assertEquals(getExpectedAlerts()[0], textArea.getText());
792 }
793
794
795
796
797 @Test
798 public void mouseUp() throws Exception {
799 final String html = DOCTYPE_HTML
800 + "<html>\n"
801 + "<head>\n"
802 + "<script>\n"
803 + " function mouseUpMe() {\n"
804 + " document.getElementById('myTextarea').value+='mouseup-';\n"
805 + " }\n"
806 + "</script>\n"
807 + "</head>\n"
808 + "<body id='myBody' onmouseup='mouseUpMe()'>\n"
809 + "<textarea id='myTextarea'></textarea>\n"
810 + "</body></html>";
811 final HtmlPage page = loadPage(html);
812 final HtmlBody body = page.getHtmlElementById("myBody");
813 body.mouseUp();
814 final HtmlTextArea textArea = page.getHtmlElementById("myTextarea");
815 assertEquals("mouseup-", textArea.getText());
816 }
817
818
819
820
821 @Test
822 @Alerts("mousedown-2-mouseup-2-contextmenu-2-")
823 public void rightClick() throws Exception {
824 final String html = DOCTYPE_HTML
825 + "<html>\n"
826 + "<head>\n"
827 + "<script>\n"
828 + " function divMouseEvent(e) {\n"
829 + " var textarea = document.getElementById('myTextarea');\n"
830 + " if (window.event)\n"
831 + " textarea.value += event.type + '-' + event.button + '-';\n"
832 + " else\n"
833 + " textarea.value += e.type + '-' + e.which + '-';\n"
834 + " }\n"
835 + " function loadFunction(e) {\n"
836 + " document.getElementById('myDiv').onmousedown = divMouseEvent;\n"
837 + " document.getElementById('myDiv').onmouseup = divMouseEvent;\n"
838 + " document.getElementById('myDiv').oncontextmenu = divMouseEvent;\n"
839 + " }\n"
840 + "</script>\n"
841 + "</head>\n"
842 + "<body onload='loadFunction()'>\n"
843 + " <div id='myDiv'>Hello</div><br>\n"
844 + " <textarea id='myTextarea'></textarea>\n"
845 + "</body></html>";
846
847 final HtmlPage page = loadPage(html);
848 final HtmlDivision div = page.getHtmlElementById("myDiv");
849 div.rightClick();
850 final HtmlTextArea textArea = page.getHtmlElementById("myTextarea");
851 assertEquals(getExpectedAlerts()[0], textArea.getText());
852 }
853
854
855
856
857
858
859 @Test
860 @Alerts("mousedown-0-mouseup-0-")
861 public void mouse_Down_Up() throws Exception {
862 final String html = DOCTYPE_HTML
863 + "<html>\n"
864 + "<head>\n"
865 + "<script>\n"
866 + " function divMouseEvent(e) {\n"
867 + " var textarea = document.getElementById('myTextarea');\n"
868 + " if (window.event)\n"
869 + " textarea.value += event.type + '-' + event.button + '-';\n"
870 + " else\n"
871 + " textarea.value += e.type + '-' + e.which + '-';\n"
872 + " }\n"
873 + " function loadFunction(e) {\n"
874 + " document.getElementById('myDiv').onmousedown = divMouseEvent;\n"
875 + " document.getElementById('myDiv').onmouseup = divMouseEvent;\n"
876 + " }\n"
877 + "</script>\n"
878 + "</head>\n"
879 + "<body onload='loadFunction()'>\n"
880 + " <div id='myDiv'>Hello</div><br>\n"
881 + " <textarea id='myTextarea'></textarea>\n"
882 + "</body></html>";
883
884 final HtmlPage page = loadPage(html);
885 final HtmlDivision div = page.getHtmlElementById("myDiv");
886 div.mouseDown();
887 div.mouseUp();
888 final HtmlTextArea textArea = page.getHtmlElementById("myTextarea");
889 assertEquals(getExpectedAlerts()[0], textArea.getText());
890 }
891
892
893
894
895 @Test
896 public void asXml_separateLineforEmptyElements() throws Exception {
897 final String html = DOCTYPE_HTML
898 + "<html><head><title>foo</title></head>\n"
899 + "<body><table><tr><td></tr></table>\n"
900 + "</body></html>";
901 final HtmlPage page = loadPage(html);
902 assertTrue(page.asXml().indexOf("/> ") == -1);
903 }
904
905
906
907
908 @Test
909 public void type() throws Exception {
910 final String html = DOCTYPE_HTML
911 + "<html><head><script>\n"
912 + " function test() {\n"
913 + " alert(document.getElementById('myInput').value);\n"
914 + " }\n"
915 + "</script></head>\n"
916 + "<body>\n"
917 + " <input id='myButton' type='button' onclick='test()'>\n"
918 + " <input id='myInput' onclick='test()'>\n"
919 + "</body></html>";
920
921 final List<String> collectedAlerts = new ArrayList<>();
922 final HtmlPage page = loadPage(html, collectedAlerts);
923 final HtmlTextInput input = page.getHtmlElementById("myInput");
924 input.type("Hello Cruel World");
925 assertEquals("", input.getValueAttribute());
926 assertEquals("Hello Cruel World", input.getValue());
927 page.getHtmlElementById("myButton").click();
928
929 final String[] expectedAlerts = {"Hello Cruel World"};
930 assertEquals(expectedAlerts, collectedAlerts);
931 }
932
933
934
935
936 @Test
937 public void typeOnFocus() throws Exception {
938 final String html = DOCTYPE_HTML
939 + "<html><head><title>foo</title></head><body>\n"
940 + "<form>\n"
941 + " <input type='text' id='textfield1' onfocus='alert(1)'>\n"
942 + "</form>\n"
943 + "</body></html>";
944 final String[] expectedAlerts = {"1"};
945 final List<String> collectedAlerts = new ArrayList<>();
946 final HtmlPage page = loadPage(html, collectedAlerts);
947
948 page.getHtmlElementById("textfield1").type('a');
949 assertEquals(expectedAlerts, collectedAlerts);
950 }
951
952
953
954
955 @Test
956 public void asNormalizedText() throws Exception {
957 final String html = DOCTYPE_HTML
958 + "<html>\n"
959 + "<head>\n"
960 + " <title>test</title>\n"
961 + "</head>\n"
962 + "<body>Welcome\n"
963 + "<div style='visibility:hidden'>to the big</div>\n"
964 + "<div style='display:none'>\n"
965 + " <div style='display:block'><span style='visibility:visible'>world</span></div>\n"
966 + "</div>\n"
967 + "</body>\n"
968 + "</html>";
969
970 final HtmlPage page = loadPage(html);
971 assertEquals("test\nWelcome", page.asNormalizedText());
972 }
973
974
975
976
977 @Test
978 public void asNormalizedTextOverridingVisibility() throws Exception {
979 final String html = DOCTYPE_HTML
980 + "<html>\n"
981 + "<head>\n"
982 + " <title>test</title>\n"
983 + "</head>\n"
984 + "<body>Welcome\n"
985 + "<p style='visibility:hidden'>hidden text\n"
986 + "<FONT COLOR='#FF0000' style='visibility:visible'>to the world</FONT>\n"
987 + "some more hidden text</p>\n"
988 + "</body>\n"
989 + "</html>";
990
991 final HtmlPage page = loadPage(html);
992 assertEquals("test\nWelcome\nto the world", page.asNormalizedText());
993 }
994
995
996
997
998 @Test
999 public void asNormalizedTextVisibilityCollapse() throws Exception {
1000 final String html = DOCTYPE_HTML
1001 + "<html>\n"
1002 + "<head>\n"
1003 + " <title>test</title>\n"
1004 + "</head>\n"
1005 + "<body>Welcome\n"
1006 + "<p style='visibility:collapse'>hidden text\n"
1007 + "<font color='#FF0000' style='visibility:visible'>to the world</font>\n"
1008 + "some more hidden text</p>\n"
1009 + "</body>\n"
1010 + "</html>";
1011 final String expected = "test\nWelcome\nto the world";
1012
1013 final HtmlPage page = loadPage(html);
1014 assertEquals(expected, page.asNormalizedText());
1015 }
1016
1017
1018
1019
1020 @Test
1021 public void getNodeName() throws Exception {
1022 final String html
1023 = "<html xmlns='http://www.w3.org/1999/xhtml' xmlns:app='http://www.appcelerator.org'>\n"
1024 + "<head>\n"
1025 + "<script>\n"
1026 + "</script>\n"
1027 + "</head>\n"
1028 + "<body>\n"
1029 + "<dIv id='dIv1'></dIv>\n"
1030 + "<app:dIv id='dIv2'></app:dIv>\n"
1031 + "<another:dIv id='dIv3'></another:dIv>\n"
1032 + "</body></html>";
1033
1034 final HtmlPage page = loadPage(html);
1035 assertEquals("div", page.getHtmlElementById("dIv1").getNodeName());
1036 assertEquals("app:div", page.getHtmlElementById("dIv2").getNodeName());
1037 assertEquals("another:div", page.getHtmlElementById("dIv3").getNodeName());
1038 assertTrue(page.asXml().contains("<app:div "));
1039 }
1040
1041
1042
1043
1044 @Test
1045 @Alerts({"1", "2"})
1046 public void getElementsByTagName() throws Exception {
1047 final String html = DOCTYPE_HTML
1048 + "<html>\n"
1049 + "<head>\n"
1050 + "<script>\n"
1051 + " function test() {\n"
1052 + " var form = document.getElementById('myForm');\n"
1053 + " alert(form.getElementsByTagName('input').length);\n"
1054 + " alert(document.body.getElementsByTagName('input').length);\n"
1055 + " }\n"
1056 + "</script>\n"
1057 + "</head>\n"
1058 + "<body onload='test()'>\n"
1059 + "<form id='myForm'>\n"
1060 + " <input type='button' name='button1' value='pushme'>\n"
1061 + "</form>\n"
1062 + "<input type='button' name='button2'>\n"
1063 + "</body></html>";
1064
1065 final HtmlPage page = loadPageWithAlerts(html);
1066 assertEquals(1, page.getElementById("myForm").getElementsByTagName("input").getLength());
1067 assertEquals(2, page.getBody().getElementsByTagName("input").getLength());
1068 }
1069
1070
1071
1072
1073 @Test
1074 public void getElementsByTagName2() throws Exception {
1075 final String html = DOCTYPE_HTML
1076 + "<html><head><title>First</title></head>\n"
1077 + "<body>\n"
1078 + "<form><input type='button' name='button1' value='pushme'></form>\n"
1079 + "<div>a</div> <div>b</div> <div>c</div>\n"
1080 + "</body></html>";
1081
1082 final HtmlPage page = loadPage(html);
1083 final HtmlElement body = page.getBody();
1084
1085 NodeList inputs = body.getElementsByTagName("input");
1086 assertEquals(1, inputs.getLength());
1087 assertEquals("button", inputs.item(0).getAttributes().getNamedItem("type").getNodeValue());
1088
1089 final NodeList divs = body.getElementsByTagName("div");
1090 assertEquals(3, divs.getLength());
1091
1092 final HtmlDivision newDiv = new HtmlDivision(HtmlDivision.TAG_NAME, page, null);
1093 body.appendChild(newDiv);
1094 assertEquals(4, divs.getLength());
1095
1096
1097 inputs = page.getElementsByTagName("inPUT");
1098 assertEquals(1, inputs.getLength());
1099
1100
1101 inputs = page.getElementsByTagName("");
1102 assertEquals(0, inputs.getLength());
1103
1104
1105 inputs = page.getElementsByTagName(null);
1106 assertEquals(0, inputs.getLength());
1107 }
1108
1109
1110
1111
1112 @Test
1113 public void getElementsByAttribute() throws Exception {
1114 final String html = DOCTYPE_HTML
1115 + "<html>\n"
1116 + "<head></head>\n"
1117 + "<body>\n"
1118 + "<form id='myForm'>\n"
1119 + " <input type='button' name='buttonName' value='pushme'>\n"
1120 + " <select id='selectId' multiple>\n"
1121 + " <option value='option1' id='option1' selected>Option1</option>\n"
1122 + " <option value='option2' id='option2' selected='selected'>Option2</option>\n"
1123 + " </select>\n"
1124 + "</form>\n"
1125 + "</body></html>";
1126
1127 final HtmlPage page = loadPage(html);
1128
1129 final HtmlElement form = page.getHtmlElementById("myForm");
1130
1131 List<HtmlElement> elements = form.getElementsByAttribute("input", "value", "pushme");
1132 assertEquals(1, elements.size());
1133 assertEquals("<input type=\"button\" name=\"buttonName\" value=\"pushme\"/>",
1134 elements.get(0).asXml().replaceAll("\\r|\\n", ""));
1135
1136
1137 elements = form.getElementsByAttribute("iNPuT", "value", "pushme");
1138 assertEquals(1, elements.size());
1139 assertEquals("<input type=\"button\" name=\"buttonName\" value=\"pushme\"/>",
1140 elements.get(0).asXml().replaceAll("\\r|\\n", ""));
1141
1142
1143 elements = form.getElementsByAttribute("input", "value", "pushMe");
1144 assertTrue(elements.isEmpty());
1145
1146
1147 elements = form.getElementsByAttribute("option", "selected", "selected");
1148 assertEquals(1, elements.size());
1149 assertEquals("<option value=\"option2\" id=\"option2\" selected=\"selected\"> Option2</option>",
1150 elements.get(0).asXml().replaceAll("\\r|\\n", ""));
1151
1152
1153 elements = form.getElementsByAttribute("option", "selected", "");
1154 assertEquals(1, elements.size());
1155 assertEquals("<option value=\"option1\" id=\"option1\" selected=\"\"> Option1</option>",
1156 elements.get(0).asXml().replaceAll("\\r|\\n", ""));
1157 }
1158
1159
1160
1161
1162 @Test
1163 public void serialization() throws Exception {
1164 final String html = DOCTYPE_HTML + "<html><body><div id='d' a='b'></div></body></html>";
1165 HtmlPage page = loadPage(html);
1166 assertEquals("b", page.getElementById("d").getAttribute("a"));
1167 page = clone(page);
1168 assertEquals("b", page.getElementById("d").getAttribute("a"));
1169 }
1170
1171
1172
1173
1174
1175 @Test
1176 public void asXml() throws Exception {
1177 final String html = DOCTYPE_HTML
1178 + "<html>\n"
1179 + "<head>\n"
1180 + " <title>test</title>\n"
1181 + "</head>\n"
1182 + "<body>Welcome\n"
1183 + "<div id='div1' onclick=\"alert('hello')\">click me</div>\n"
1184 + "<div id='div2' onclick='alert(\"hello again\")'>click me again</div>\n"
1185 + "</body>\n"
1186 + "</html>";
1187
1188 final HtmlPage page = loadPage(html);
1189
1190 final String htmlDiv1XML = "<div id=\"div1\" onclick=\"alert('hello')\">\r\n click me"
1191 + "\r\n</div>\r\n";
1192 assertEquals(htmlDiv1XML, page.getElementById("div1").asXml());
1193
1194 final String htmlDiv2XML = "<div id=\"div2\" onclick=\"alert("hello again")\">\r\n click me again"
1195 + "\r\n</div>\r\n";
1196 assertEquals(htmlDiv2XML, page.getElementById("div2").asXml());
1197 }
1198
1199
1200
1201
1202 @Test
1203 @Alerts({"true", "false", "false"})
1204 public void isDisplayedJsDisabled() throws Exception {
1205 final String html = DOCTYPE_HTML
1206 + "<html><head>\n"
1207 + "</head>\n"
1208 + "</body>\n"
1209 + "<div id='d1'>hello</div>\n"
1210 + "<div id='d2' hidden>world</div>\n"
1211 + "<div id='d3' style='display:none;'>world</div>\n"
1212 + "</body></html>";
1213
1214 getWebClient().getOptions().setJavaScriptEnabled(false);
1215 final HtmlPage page = loadPage(html);
1216 assertEquals(Boolean.parseBoolean(getExpectedAlerts()[0]), page.getElementById("d1").isDisplayed());
1217 assertEquals(Boolean.parseBoolean(getExpectedAlerts()[1]), page.getElementById("d2").isDisplayed());
1218 assertEquals(Boolean.parseBoolean(getExpectedAlerts()[2]), page.getElementById("d3").isDisplayed());
1219 }
1220
1221
1222
1223
1224 @Test
1225 @Alerts({"true", "false", "false"})
1226 public void isDisplayedJsEngineDisabled() throws Exception {
1227 final String html = DOCTYPE_HTML
1228 + "<html><head>\n"
1229 + "</head>\n"
1230 + "</body>\n"
1231 + "<div id='d1'>hello</div>\n"
1232 + "<div id='d2' hidden>world</div>\n"
1233 + "<div id='d3' style='display:none;'>world</div>\n"
1234 + "</body></html>";
1235
1236 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
1237 final HtmlPage page = loadPage(webClient, html, null);
1238
1239 assertFalse(page.getWebClient().isJavaScriptEngineEnabled());
1240 assertEquals(Boolean.parseBoolean(getExpectedAlerts()[0]), page.getElementById("d1").isDisplayed());
1241 assertEquals(Boolean.parseBoolean(getExpectedAlerts()[1]), page.getElementById("d2").isDisplayed());
1242 assertEquals(Boolean.parseBoolean(getExpectedAlerts()[2]), page.getElementById("d3").isDisplayed());
1243 }
1244 }
1245
1246
1247
1248
1249 @Test
1250 @Alerts({"true", "false", "false"})
1251 public void clickJsEngineDisabled() throws Exception {
1252 final String html = DOCTYPE_HTML
1253 + "<html><head>\n"
1254 + "</head>\n"
1255 + "</body>\n"
1256 + "<div id='d1'>hello</div>\n"
1257 + "</body></html>";
1258
1259 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
1260 final HtmlPage page = loadPage(webClient, html, null);
1261
1262 assertFalse(page.getWebClient().isJavaScriptEngineEnabled());
1263 assertEquals(page, page.getElementById("d1").click());
1264 }
1265 }
1266
1267
1268
1269
1270 @Test
1271 public void acceptChar() throws Exception {
1272 final String html = DOCTYPE_HTML + "<html><body><input></body></html>";
1273 final HtmlPage page = loadPage(html);
1274 final String value = "abc[ ][\t][ ][\u2006]123あいう漢字[!@#$%^&*()-=_+]{}<>?/\\";
1275 final HtmlInput input = page.getFirstByXPath("//input");
1276 input.type(value);
1277 assertEquals(value, input.getValue());
1278 }
1279 }