1 package org.apache.maven.doxia.siterenderer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
23 import com.gargoylesoftware.htmlunit.WebClient;
24 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
25 import com.gargoylesoftware.htmlunit.html.HtmlDivision;
26 import com.gargoylesoftware.htmlunit.html.HtmlElement;
27 import com.gargoylesoftware.htmlunit.html.HtmlHeader2;
28 import com.gargoylesoftware.htmlunit.html.HtmlPage;
29 import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
30 import com.gargoylesoftware.htmlunit.html.HtmlScript;
31
32 import java.io.File;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.Iterator;
36 import java.util.List;
37
38
39
40
41
42
43
44
45 public class JavascriptVerifier
46 extends AbstractVerifier
47 {
48
49
50
51
52
53
54
55 public void verify( String file )
56 throws Exception
57 {
58 File jsTest = getTestFile( "target/output/javascript.html" );
59 assertNotNull( jsTest );
60 assertTrue( jsTest.exists() );
61
62
63 WebClient webClient = new WebClient();
64
65 final List collectedAlerts = new ArrayList();
66 webClient.setAlertHandler( new CollectingAlertHandler( collectedAlerts ) );
67
68 HtmlPage page = (HtmlPage) webClient.getPage( jsTest.toURI().toURL() );
69 assertNotNull( page );
70
71 HtmlElement element = page.getHtmlElementById( "contentBox" );
72 assertNotNull( element );
73 HtmlDivision division = (HtmlDivision) element;
74 assertNotNull( division );
75
76 Iterator elementIterator = division.getAllHtmlChildElements();
77
78
79
80
81
82 HtmlDivision div = (HtmlDivision) elementIterator.next();
83 assertNotNull( div );
84 assertEquals( div.getAttributeValue( "class" ), "section" );
85
86 HtmlHeader2 h2 = (HtmlHeader2) elementIterator.next();
87 assertNotNull( h2 );
88 assertEquals( h2.asText().trim(), "Test" );
89
90 HtmlAnchor a = (HtmlAnchor) elementIterator.next();
91 assertNotNull( a );
92 assertEquals( a.getAttributeValue( "name" ), "Test" );
93
94 HtmlParagraph p = (HtmlParagraph) elementIterator.next();
95 assertNotNull( p );
96 assertEquals( p.asText().trim(), "You should see a JavaScript alert..." );
97
98 HtmlScript script = (HtmlScript) elementIterator.next();
99 assertNotNull( script );
100 assertEquals( script.getAttributeValue( "type" ), "text/javascript" );
101 assertEquals( script.asText().trim(), "" );
102 final List expectedAlerts = Collections.singletonList( "Hello!" );
103 assertEquals( expectedAlerts, collectedAlerts );
104 }
105 }