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.HtmlHeading2;
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 public class JavascriptVerifier
45 extends AbstractVerifier
46 {
47
48
49
50
51
52
53
54 public void verify( String file )
55 throws Exception
56 {
57 File jsTest = getTestFile( "target/output/javascript.html" );
58 assertNotNull( jsTest );
59 assertTrue( jsTest.exists() );
60
61
62 WebClient webClient = new WebClient();
63
64 final List<String> collectedAlerts = new ArrayList<String>( 4 );
65 webClient.setAlertHandler( new CollectingAlertHandler( collectedAlerts ) );
66
67 HtmlPage page = (HtmlPage) webClient.getPage( jsTest.toURI().toURL() );
68 assertNotNull( page );
69
70 HtmlElement element = page.getHtmlElementById( "contentBox" );
71 assertNotNull( element );
72 HtmlDivision division = (HtmlDivision) element;
73 assertNotNull( division );
74
75 Iterator<HtmlElement> elementIterator = division.getHtmlElementDescendants().iterator();
76
77
78
79
80
81 HtmlDivision div = (HtmlDivision) elementIterator.next();
82 assertNotNull( div );
83 assertEquals( "section", div.getAttribute( "class" ) );
84
85 HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
86 assertNotNull( h2 );
87 assertEquals( "Test", h2.asText().trim() );
88
89 HtmlAnchor a = (HtmlAnchor) elementIterator.next();
90 assertNotNull( a );
91 assertEquals( "Test", a.getAttribute( "name" ) );
92
93 HtmlParagraph p = (HtmlParagraph) elementIterator.next();
94 assertNotNull( p );
95 assertEquals( "You should see a JavaScript alert...", p.asText().trim() );
96
97 HtmlScript script = (HtmlScript) elementIterator.next();
98 assertNotNull( script );
99 assertEquals( "text/javascript", script.getAttribute( "type" ) );
100 assertEquals( "", script.asText().trim() );
101 final List<String> expectedAlerts = Collections.singletonList( "Hello!" );
102 assertEquals( expectedAlerts, collectedAlerts );
103 }
104 }