View Javadoc
1   package org.apache.maven.doxia.siterenderer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Verify javascript code.
41   *
42   * @author ltheussl
43   */
44  public class JavascriptVerifier
45      extends AbstractVerifier
46  {
47      /**
48       * Verifies a HtmlPage.
49       *
50       * @param file the file to verify.
51       *
52       * @throws Exception if something goes wrong.
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          // HtmlUnit
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 }