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.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   * Verify javascript code.
41   *
42   * @author ltheussl
43   * @version $Id: JavascriptVerifier.java 779585 2009-05-28 12:43:33Z ltheussl $
44   */
45  public class JavascriptVerifier
46      extends AbstractVerifier
47  {
48      /**
49       * Verifies a HtmlPage.
50       *
51       * @param file the file to verify.
52       *
53       * @throws Exception if something goes wrong.
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          // HtmlUnit
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 }