-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathDomNodeTest.java
More file actions
860 lines (756 loc) · 32 KB
/
DomNodeTest.java
File metadata and controls
860 lines (756 loc) · 32 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/*
* 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.html;
import java.util.ArrayList;
import java.util.List;
import org.htmlunit.ElementNotFoundException;
import org.htmlunit.SimpleWebTestCase;
import org.htmlunit.WebClient;
import org.htmlunit.html.DomNode.DescendantHtmlElementsIterator;
import org.htmlunit.junit.annotation.Alerts;
import org.htmlunit.util.MimeType;
import org.htmlunit.xml.XmlPage;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.xml.sax.helpers.AttributesImpl;
/**
* Tests for {@link DomNode}.
*
* @author Chris Erskine
* @author Ahmed Ashour
* @author Ronald Brill
*/
public class DomNodeTest extends SimpleWebTestCase {
/**
* Test hasAttributes() on an element with attributes.
* @throws Exception if the test fails
*/
@Test
public void elementHasAttributesWith() throws Exception {
final String content = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
final HtmlPage page = loadPage(content);
final DomNode node = page.getElementById("tag");
assertTrue("Element should have attribute", node.hasAttributes());
}
/**
* Test hasAttributes() on an element with no attributes.
* @throws Exception if the test fails
*/
@Test
public void elementHasAttributesNone() throws Exception {
final String content = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
final HtmlPage page = loadPage(content);
final DomNode node = page.getElementById("tag");
final DomNode parent = node.getParentNode();
assertFalse("Element should not have attribute", parent.hasAttributes());
}
/**
* Test hasAttributes on a node that is not defined to have attributes.
* @throws Exception if the test fails
*/
@Test
public void nonElementHasAttributes() throws Exception {
final String content = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
final HtmlPage page = loadPage(content);
final DomNode node = page.getElementById("tag");
final DomNode child = node.getFirstChild();
assertFalse("Text should not have attribute", child.hasAttributes());
}
/**
* Test getPrefix on a node that is not defined to have a prefix.
* @throws Exception if the test fails
*/
@Test
public void nonElementGetPrefix() throws Exception {
final String content = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
final HtmlPage page = loadPage(content);
final DomNode node = page.getElementById("tag");
final DomNode child = node.getFirstChild();
assertEquals("Text should not have a prefix", null, child.getPrefix());
}
/**
* Test getNamespaceURI on a node that is not defined to have a namespace.
* @throws Exception if the test fails
*/
@Test
public void nonElementGetNamespaceURI() throws Exception {
final String content = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
final HtmlPage page = loadPage(content);
final DomNode node = page.getElementById("tag");
final DomNode child = node.getFirstChild();
assertEquals("Text should not have a prefix", null, child.getNamespaceURI());
}
/**
* Test getLocalName on a node that is not defined to have a local name.
* @throws Exception if the test fails
*/
@Test
public void nonElementGetLocalName() throws Exception {
final String content = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
final HtmlPage page = loadPage(content);
final DomNode node = page.getElementById("tag");
final DomNode child = node.getFirstChild();
assertEquals("Text should not have a prefix", null, child.getLocalName());
}
/**
* Test setPrefix on a node that is not defined to have a prefix.
* @throws Exception if the test fails
*/
@Test
public void nonElementSetPrefix() throws Exception {
final String content = DOCTYPE_HTML + "<html><head></head><body id='tag'>text</body></html>";
final HtmlPage page = loadPage(content);
final DomNode node = page.getElementById("tag");
final DomNode child = node.getFirstChild();
child.setPrefix("bar"); // This does nothing.
assertEquals("Text should not have a prefix", null, child.getPrefix());
}
/**
* @throws Exception if the test fails
*/
@Test
public void removeAllChildren() throws Exception {
final String content = DOCTYPE_HTML
+ "<html><head></head><body>\n"
+ "<p id='tag'><table>\n"
+ "<tr><td>row 1</td></tr>\n"
+ "<tr><td>row 2</td></tr>\n"
+ "</table></p></body></html>";
final HtmlPage page = loadPage(content);
final DomNode node = page.getElementById("tag");
node.removeAllChildren();
assertEquals("Did not remove all nodes", null, node.getFirstChild());
}
/**
* @throws Exception if the test fails
*/
@Test
public void replace() throws Exception {
final String content = DOCTYPE_HTML
+ "<html><head></head><body>\n"
+ "<br><div id='tag'></div><br><div id='tag2'/></body></html>";
final HtmlPage page = loadPage(content);
final DomNode node = page.getElementById("tag");
final DomNode previousSibling = node.getPreviousSibling();
final DomNode nextSibling = node.getNextSibling();
final DomNode parent = node.getParentNode();
// position among parent's children
final int position = readPositionAmongParentChildren(node);
final DomNode newNode = new DomText(page, "test");
node.replace(newNode);
assertSame("previous sibling", previousSibling, newNode.getPreviousSibling());
assertSame("next sibling", nextSibling, newNode.getNextSibling());
assertSame("parent", parent, newNode.getParentNode());
assertSame(newNode, previousSibling.getNextSibling());
assertSame(newNode, nextSibling.getPreviousSibling());
assertEquals(position, readPositionAmongParentChildren(newNode));
final AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute(null, "id", "id", null, "tag2"); // with the same id as the node to replace
final DomNode node2 = page.getHtmlElementById("tag2");
assertEquals("div", node2.getNodeName());
final DomNode node3 = page.getWebClient().getPageCreator().getHtmlParser()
.getFactory(HtmlSpan.TAG_NAME)
.createElement(page, HtmlSpan.TAG_NAME, attributes);
node2.replace(node3);
assertEquals("span", page.getHtmlElementById("tag2").getTagName());
}
/**
* @throws Exception if the test fails
*/
@Test
public void getNewNodeById() throws Exception {
final String content = DOCTYPE_HTML
+ "<html><head></head><body>\n"
+ "<br><div id='tag'/></body></html>";
final HtmlPage page = loadPage(content);
final DomNode node = page.getElementById("tag");
final AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute(null, "id", "id", null, "newElt");
final DomNode newNode = page.getWebClient().getPageCreator().getHtmlParser()
.getFactory(HtmlDivision.TAG_NAME)
.createElement(page, HtmlDivision.TAG_NAME, attributes);
try {
page.getHtmlElementById("newElt");
Assertions.fail("Element should not exist yet");
}
catch (final ElementNotFoundException e) {
// nothing to do, it's ok
}
node.replace(newNode);
page.getHtmlElementById("newElt");
try {
page.getHtmlElementById("tag");
Assertions.fail("Element should not exist anymore");
}
catch (final ElementNotFoundException e) {
// nothing to do, it's ok
}
newNode.insertBefore(node);
page.getHtmlElementById("tag");
}
/**
* @throws Exception if the test fails
*/
@Test
public void appendChild() throws Exception {
final String content = DOCTYPE_HTML
+ "<html><head></head><body>\n"
+ "<br><div><div id='tag'></div></div><br></body></html>";
final HtmlPage page = loadPage(content);
final DomNode node = page.getElementById("tag");
final DomNode parent = node.getParentNode();
// position among parent's children
final int position = readPositionAmongParentChildren(node);
final DomNode newNode = new DomText(page, "test");
parent.appendChild(newNode);
assertSame("new node previous sibling", node, newNode.getPreviousSibling());
assertSame("new node next sibling", null, newNode.getNextSibling());
assertSame("next sibling", newNode, node.getNextSibling());
assertSame("parent", parent, newNode.getParentNode());
assertEquals(position + 1, readPositionAmongParentChildren(newNode));
final DomNode newNode2 = new DomText(page, "test2");
parent.appendChild(newNode2);
page.getHtmlElementById("tag");
}
/**
* @throws Exception if the test fails
*/
@Test
public void insertBefore() throws Exception {
final String content = DOCTYPE_HTML
+ "<html><head></head><body>\n"
+ "<br><div id='tag'></div><br></body></html>";
final HtmlPage page = loadPage(content);
final DomNode node = page.getElementById("tag");
final DomNode previousSibling = node.getPreviousSibling();
final DomNode nextSibling = node.getNextSibling();
final DomNode parent = node.getParentNode();
// position among parent's children
final int position = readPositionAmongParentChildren(node);
final DomNode newNode = new DomText(page, "test");
node.insertBefore(newNode);
assertSame("new node previous sibling", previousSibling, newNode.getPreviousSibling());
assertSame("previous sibling", newNode, node.getPreviousSibling());
assertSame("new node next sibling", node, newNode.getNextSibling());
assertSame("next sibling", nextSibling, node.getNextSibling());
assertSame("parent", parent, newNode.getParentNode());
assertSame(newNode, previousSibling.getNextSibling());
assertSame(node, nextSibling.getPreviousSibling());
assertEquals(position, readPositionAmongParentChildren(newNode));
}
/**
* Reads the position of the node among the children of its parent
* @param node the node to look at
* @return the position
*/
private static int readPositionAmongParentChildren(final DomNode node) {
int i = 0;
for (final DomNode child : node.getParentNode().getChildren()) {
if (child == node) {
return i;
}
i++;
}
return -1;
}
/**
* @throws Exception if the test fails
*/
@Test
public void getByXPath() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html>\n"
+ " <head>\n"
+ " <title>my title</title>\n"
+ " </head>"
+ " <body>\n"
+ " <div id='d1'>\n"
+ " <ul>\n"
+ " <li>foo 1</li>\n"
+ " <li>foo 2</li>\n"
+ " </ul>\n"
+ " </div>\n"
+ " <div><span>bla</span></div>\n"
+ "</body>\n"
+ "</html>";
final HtmlPage page = loadPage(htmlContent);
final List<?> results = page.getByXPath("//title");
assertEquals(1, results.size());
final HtmlTitle title = (HtmlTitle) results.get(0);
assertEquals("my title", title.asNormalizedText());
final HtmlHead head = (HtmlHead) title.getParentNode();
assertEquals(results, head.getByXPath("//title"));
assertEquals(results, head.getByXPath("./title"));
assertTrue(head.getByXPath("/title").isEmpty());
assertEquals(results, head.getByXPath("title"));
final HtmlElement div = page.getFirstByXPath("//div");
assertSame(div, page.getHtmlElementById("d1"));
final List<?> lis = div.getByXPath("ul/li");
assertEquals(2, lis.size());
assertEquals(lis, page.getByXPath("//ul/li"));
assertEquals(2, div.<Number>getFirstByXPath("count(//li)").intValue());
}
/**
* @throws Exception if the test fails
*/
@Test
public void getByXPathSelectedNode() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html>\n"
+ " <head>\n"
+ " <title>my title</title>\n"
+ " </head>"
+ " <body>\n"
+ " <h1 id='outer_h1'>HtmlUnit</h1>\n"
+ " <div id='d1'>\n"
+ " <h1 id='h1'>HtmlUnit</h1>\n"
+ " </div>\n"
+ "</body>\n"
+ "</html>";
final HtmlPage page = loadPage(htmlContent);
final HtmlDivision divNode = (HtmlDivision) page.getElementById("d1");
assertEquals(page.getElementById("h1"), divNode.getByXPath(".//h1").get(0));
assertEquals(page.getElementById("outer_h1"), divNode.getByXPath("//h1").get(0));
}
/**
* Regression test for bug #1149: xmlns value has to be trimmed.
* @throws Exception if the test fails
*/
@Test
public void getByXPath_trim_namespace() throws Exception {
final String html = DOCTYPE_HTML
+ "<html xmlns=' http://www.w3.org/1999/xhtml'>\n"
+ "<body>\n"
+ "<div><span>bla</span></div>\n"
+ "</body></html>";
final HtmlPage page = loadPage(html);
final List<?> results = page.getByXPath("//div");
assertEquals(1, results.size());
}
/**
* Make sure display: none has no effect for xpath expressions.
* @throws Exception if the test fails
*/
@Test
public void getFirstByXPathDisplayNone() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html>\n"
+ " <head>\n"
+ " <title>my title</title>\n"
+ " </head>"
+ " <body>\n"
+ " <div id='d1'><span style='display: none;'>bla</span></div>\n"
+ "</body>\n"
+ "</html>";
final HtmlPage page = loadPage(htmlContent);
HtmlElement span = page.getFirstByXPath("//div/span");
assertEquals("<span style=\"display: none;\">bla</span>", span.asXml());
assertFalse(span.isDisplayed());
span = page.getFirstByXPath("//span[text()=\"bla\"]");
assertEquals("<span style=\"display: none;\">bla</span>", span.asXml());
assertFalse(span.isDisplayed());
}
/**
* @throws Exception if the test fails
*/
@Test
public void getFirstByXPath() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>my title</title></head><body>\n"
+ "<div id='d1'><ul><li>foo 1</li><li>foo 2</li></ul></div>\n"
+ "<div><span>bla</span></div>\n"
+ "</body></html>";
final HtmlPage page = loadPage(htmlContent);
final HtmlTitle title = page.getFirstByXPath("//title");
assertEquals("my title", title.asNormalizedText());
final HtmlHead head = (HtmlHead) title.getParentNode();
assertSame(title, head.getFirstByXPath("//title"));
assertSame(title, head.getFirstByXPath("./title"));
assertNull(head.getFirstByXPath("/title"));
assertSame(title, head.getFirstByXPath("title"));
final HtmlElement div = page.getFirstByXPath("//div");
assertSame(div, page.getHtmlElementById("d1"));
final HtmlListItem listItem = div.getFirstByXPath("ul/li");
assertSame(listItem, page.getFirstByXPath("//ul/li"));
assertEquals(2, ((Number) div.getFirstByXPath("count(//li)")).intValue());
}
/**
* Verifies that {@link DomNode#getHtmlElementDescendants()} returns descendant elements in the correct order.
* @throws Exception if an error occurs
*/
@Test
public void getHtmlElementDescendantsOrder() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><body id='0'>\n"
+ "<span id='I'><span id='I.1'><span id='I.1.a'/><span id='I.1.b'/><span id='I.1.c'/></span>\n"
+ "<span id='I.2'><span id='I.2.a'/></span></span>\n"
+ "<span id='II'/>\n"
+ "<span id='III'><span id='III.1'><span id='III.1.a'/></span></span>\n"
+ "</body></html>";
final HtmlPage page = loadPage(html);
final DescendantHtmlElementsIterator iterator = (DescendantHtmlElementsIterator)
page.getDocumentElement().getHtmlElementDescendants().iterator();
assertEquals("", iterator.nextNode().getId());
assertEquals("0", iterator.nextNode().getId());
assertEquals("I", iterator.nextNode().getId());
assertEquals("I.1", iterator.nextNode().getId());
assertEquals("I.1.a", iterator.nextNode().getId());
assertEquals("I.1.b", iterator.nextNode().getId());
assertEquals("I.1.c", iterator.nextNode().getId());
assertEquals("I.2", iterator.nextNode().getId());
assertEquals("I.2.a", iterator.nextNode().getId());
assertEquals("II", iterator.nextNode().getId());
assertEquals("III", iterator.nextNode().getId());
assertEquals("III.1", iterator.nextNode().getId());
assertEquals("III.1.a", iterator.nextNode().getId());
assertFalse(iterator.hasNext());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void getDescendants_remove() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><body id='body'>\n"
+ "<div id='a'>a<div id='b'>b</div>a<div id='c'>c</div>a</div><div id='d'>d</div>\n"
+ "</body></html>";
final HtmlPage page = loadPage(html);
assertEquals("abacad", page.asNormalizedText().replaceAll("\\s", ""));
final DescendantHtmlElementsIterator iterator = (DescendantHtmlElementsIterator)
page.getDocumentElement().getHtmlElementDescendants().iterator();
assertEquals("", iterator.nextNode().getId());
assertEquals("body", iterator.nextNode().getId());
assertEquals("a", iterator.nextNode().getId());
iterator.remove();
assertEquals("d", iterator.nextNode().getId());
assertFalse(iterator.hasNext());
assertEquals("d", page.asNormalizedText().replaceAll("\\s", ""));
}
static class DomChangeListenerTestImpl implements DomChangeListener {
private final List<String> collectedValues_ = new ArrayList<>();
@Override
public void nodeAdded(final DomChangeEvent event) {
collectedValues_.add("nodeAdded: " + event.getParentNode().getNodeName() + ','
+ event.getChangedNode().getNodeName());
}
@Override
public void nodeDeleted(final DomChangeEvent event) {
collectedValues_.add("nodeDeleted: " + event.getParentNode().getNodeName() + ','
+ event.getChangedNode().getNodeName());
}
List<String> getCollectedValues() {
return collectedValues_;
}
}
/**
* @throws Exception if the test fails
*/
@Test
public void domChangeListenerTestImpl_insertBefore() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title>\n"
+ "<script>\n"
+ " function clickMe() {\n"
+ " var p1 = document.getElementById('p1');\n"
+ " var div = document.createElement('DIV');\n"
+ " p1.insertBefore(div, null);\n"
+ " }\n"
+ "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<p id='p1' title='myTitle'></p>\n"
+ "<input id='myButton' type='button' onclick='clickMe()'>\n"
+ "</body></html>";
final String[] expectedValues = {"nodeAdded: p,div", "nodeAdded: p,div"};
final HtmlPage page = loadPage(htmlContent);
final HtmlElement p1 = page.getHtmlElementById("p1");
final DomChangeListenerTestImpl listenerImpl = new DomChangeListenerTestImpl();
p1.addDomChangeListener(listenerImpl);
page.addDomChangeListener(listenerImpl);
final HtmlButtonInput myButton = page.getHtmlElementById("myButton");
myButton.click();
assertEquals(expectedValues, listenerImpl.getCollectedValues());
}
/**
* @throws Exception if the test fails
*/
@Test
public void domChangeListenerTestImpl_appendChild() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title>\n"
+ "<script>\n"
+ " function clickMe() {\n"
+ " var p1 = document.getElementById('p1');\n"
+ " var div = document.createElement('DIV');\n"
+ " p1.appendChild(div);\n"
+ " }\n"
+ "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<p id='p1' title='myTitle'></p>\n"
+ "<input id='myButton' type='button' onclick='clickMe()'>\n"
+ "</body></html>";
final String[] expectedValues = {"nodeAdded: p,div", "nodeAdded: p,div"};
final HtmlPage page = loadPage(htmlContent);
final HtmlElement p1 = page.getHtmlElementById("p1");
final DomChangeListenerTestImpl listenerImpl = new DomChangeListenerTestImpl();
p1.addDomChangeListener(listenerImpl);
page.addDomChangeListener(listenerImpl);
final HtmlButtonInput myButton = page.getHtmlElementById("myButton");
myButton.click();
assertEquals(expectedValues, listenerImpl.getCollectedValues());
}
/**
* @throws Exception if the test fails
*/
@Test
@Alerts({"nodeDeleted: div,p", "nodeDeleted: div,p"})
public void domChangeListenerTestImpl_removeChild() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title>\n"
+ "<script>\n"
+ " function clickMe() {\n"
+ " var p1 = document.getElementById('p1');\n"
+ " var div = document.getElementById('myDiv');\n"
+ " div.removeChild(p1);\n"
+ " }\n"
+ "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<div id='myDiv'><p id='p1' title='myTitle'></p></div>\n"
+ "<input id='myButton' type='button' onclick='clickMe()'>\n"
+ "</body></html>";
final HtmlPage page = loadPage(htmlContent);
final HtmlElement p1 = page.getHtmlElementById("p1");
final DomChangeListenerTestImpl listenerImpl = new DomChangeListenerTestImpl();
p1.addDomChangeListener(listenerImpl);
page.addDomChangeListener(listenerImpl);
final HtmlButtonInput myButton = page.getHtmlElementById("myButton");
myButton.click();
assertEquals(getExpectedAlerts(), listenerImpl.getCollectedValues());
}
/**
* @throws Exception if the test fails
*/
@Test
public void domChangeListenerRegisterNewListener() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title>\n"
+ "<script>\n"
+ " function clickMe() {\n"
+ " var p1 = document.getElementById('p1');\n"
+ " var div = document.createElement('DIV');\n"
+ " p1.appendChild(div);\n"
+ " }\n"
+ "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<p id='p1' title='myTitle'></p>\n"
+ "<input id='myButton' type='button' onclick='clickMe()'>\n"
+ "</body></html>";
final HtmlPage page = loadPage(htmlContent);
final List<String> l = new ArrayList<>();
final DomChangeListener listener2 = new DomChangeListenerTestImpl() {
@Override
public void nodeAdded(final DomChangeEvent event) {
l.add("in listener 2");
}
};
final DomChangeListener listener1 = new DomChangeListenerTestImpl() {
@Override
public void nodeAdded(final DomChangeEvent event) {
l.add("in listener 1");
page.addDomChangeListener(listener2);
}
};
page.addDomChangeListener(listener1);
final HtmlButtonInput myButton = page.getHtmlElementById("myButton");
myButton.click();
final String[] expectedValues = {"in listener 1"};
assertEquals(expectedValues, l);
l.clear();
myButton.click();
final String[] expectedValues2 = {"in listener 1", "in listener 2"};
assertEquals(expectedValues2, l);
}
/**
* @throws Exception if the test fails
*/
@Test
public void getByXPath_XML() throws Exception {
final String xml
= "<books>\n"
+ " <book>\n"
+ " <title>Immortality</title>\n"
+ " <author>John Smith</author>\n"
+ " </book>\n"
+ "</books>";
getMockWebConnection().setResponse(URL_FIRST, xml, MimeType.TEXT_XML);
final WebClient client = getWebClientWithMockWebConnection();
final XmlPage page = client.getPage(URL_FIRST);
final List<?> results = page.getByXPath("//title");
assertEquals(1, results.size());
}
/**
* @throws Exception if the test fails
*/
@Test
public void getCanonicalXPath() throws Exception {
final String content = DOCTYPE_HTML + "<html><head></head><body><div id='div1'/><div id='div2'/></body></html>";
final HtmlPage page = loadPage(content);
for (final HtmlElement element : page.getHtmlElementDescendants()) {
final List<?> foundElements = page.getByXPath(element.getCanonicalXPath());
assertEquals(1, foundElements.size());
assertSame(element, foundElements.get(0));
}
}
/**
* @throws Exception if the test fails
*/
@Test
public void getChildNodes_remove() throws Exception {
final String content = DOCTYPE_HTML + "<html><body id='b'><div id='d1'></div><div id='d2'></div></body></html>";
final HtmlPage page = loadPage(content);
final DomNodeList<DomNode> children = page.getElementById("b").getChildNodes();
assertEquals(2, children.getLength());
page.getElementById("d1").remove();
assertEquals(1, children.getLength());
page.getElementById("d2").remove();
assertEquals(0, children.getLength());
assertNull(children.get(0));
}
/**
* @throws Exception if an error occurs
*/
@Test
public void serialization() throws Exception {
final String html = DOCTYPE_HTML + "<html><head></head><body></body></html>";
final DomChangeListenerTestImpl listener = new DomChangeListenerTestImpl();
HtmlPage page = loadPage(html);
page.addDomChangeListener(listener);
page = clone(page);
page.removeDomChangeListener(listener);
}
/**
* @throws Exception if the test fails
*/
@Test
public void isDisplayed() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<style>\n"
+ "#d2 { display: none; }\n"
+ "#d3 { visibility: hidden; }\n"
+ "#d4 { display: block !important; }\n"
+ "</style>\n"
+ "<div id='d1'>hello</div>\n"
+ "<div id='d2'>world</div>\n"
+ "<div id='d3'>again</div>\n"
+ "<div id='d4' style='display: none' >important</div>\n"
+ "</body></html>";
final HtmlPage page = loadPage(html);
assertTrue(page.getElementById("d1").isDisplayed());
assertFalse(page.getElementById("d2").isDisplayed());
assertFalse(page.getElementById("d3").isDisplayed());
assertTrue(page.getElementById("d4").isDisplayed());
getWebClient().getOptions().setCssEnabled(false);
assertTrue(page.getElementById("d1").isDisplayed());
assertTrue(page.getElementById("d2").isDisplayed());
assertTrue(page.getElementById("d3").isDisplayed());
}
/**
* @throws Exception if the test fails
*/
@Test
public void isDisplayedMouseOver() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<style>\n"
+ "#d2:hover { display: none; }\n"
+ "#d3:hover { visibility: hidden; }\n"
+ "</style>\n"
+ "<div id='d1'>hello</div>\n"
+ "<div id='d2'>world</div>\n"
+ "<div id='d3'>again</div>\n"
+ "<div id='d4' style='display: none' >important</div>\n"
+ "</body></html>";
final HtmlPage page = loadPage(html);
assertTrue(page.getElementById("d1").isDisplayed());
HtmlElement elem = page.getHtmlElementById("d2");
assertTrue(elem.isDisplayed());
elem.mouseOver();
assertFalse(elem.isDisplayed());
elem.mouseOut();
assertTrue(elem.isDisplayed());
elem = page.getHtmlElementById("d3");
assertTrue(elem.isDisplayed());
elem.mouseOver();
assertFalse(elem.isDisplayed());
elem.mouseOut();
assertTrue(elem.isDisplayed());
}
/**
* @throws Exception if the test fails
*/
@Test
public void isDisplayedMouseOverParent() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<style>\n"
+ "#d1:hover { display: none; }\n"
+ "#d2:hover { visibility: hidden; }\n"
+ "</style>\n"
+ "<div id='d1'><div id='d1-1'>hello</div></div>\n"
+ "<div id='d2'><div id='d2-1'>world</div></div>\n"
+ "</body></html>";
final HtmlPage page = loadPage(html);
HtmlElement elem = page.getHtmlElementById("d1");
assertTrue(elem.isDisplayed());
page.getHtmlElementById("d1-1").mouseOver();
assertFalse(elem.isDisplayed());
page.getHtmlElementById("d1-1").mouseOut();
assertTrue(elem.isDisplayed());
elem = page.getHtmlElementById("d2");
assertTrue(elem.isDisplayed());
page.getHtmlElementById("d2-1").mouseOver();
assertFalse(elem.isDisplayed());
page.getHtmlElementById("d2-1").mouseOut();
assertTrue(elem.isDisplayed());
}
/**
* @throws Exception if the test fails
*/
@Test
public void isDisplayedMousePath() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<style>\n"
+ "#d1:hover #d2 { display: none; }\n"
+ "</style>\n"
+ "<div id='d1'>hello<div id='d2'>world</div></div>\n"
+ "</body></html>";
final HtmlPage page = loadPage(html);
final HtmlElement elem1 = page.getHtmlElementById("d1");
final HtmlElement elem2 = page.getHtmlElementById("d2");
assertTrue(elem1.isDisplayed());
assertTrue(elem2.isDisplayed());
elem1.mouseOver();
assertTrue(elem1.isDisplayed());
assertFalse(elem2.isDisplayed());
elem1.mouseOut();
assertTrue(elem1.isDisplayed());
assertTrue(elem2.isDisplayed());
}
}