View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.doxia.siterenderer;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.htmlunit.CollectingAlertHandler;
28  import org.htmlunit.WebClient;
29  import org.htmlunit.html.HtmlAnchor;
30  import org.htmlunit.html.HtmlElement;
31  import org.htmlunit.html.HtmlHeading1;
32  import org.htmlunit.html.HtmlMain;
33  import org.htmlunit.html.HtmlPage;
34  import org.htmlunit.html.HtmlParagraph;
35  import org.htmlunit.html.HtmlScript;
36  import org.htmlunit.html.HtmlSection;
37  
38  import static org.codehaus.plexus.testing.PlexusExtension.getTestFile;
39  import static org.junit.jupiter.api.Assertions.assertEquals;
40  import static org.junit.jupiter.api.Assertions.assertNotNull;
41  import static org.junit.jupiter.api.Assertions.assertTrue;
42  
43  /**
44   * Verify javascript code.
45   *
46   * @author ltheussl
47   */
48  public class JavascriptVerifier extends AbstractVerifier {
49      /**
50       * Verifies a HtmlPage.
51       *
52       * @param file the file to verify.
53       *
54       * @throws Exception if something goes wrong.
55       */
56      public void verify(String file) throws Exception {
57          File jsTest = getTestFile("target/output/javascript.html");
58          assertNotNull(jsTest);
59          assertTrue(jsTest.exists());
60  
61          // HtmlUnit
62          try (WebClient webClient = new WebClient()) {
63              webClient.getOptions().setCssEnabled(false);
64  
65              final List<String> collectedAlerts = new ArrayList<>(4);
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              HtmlMain main = (HtmlMain) element;
74              assertNotNull(main);
75  
76              Iterator<HtmlElement> elementIterator =
77                      main.getHtmlElementDescendants().iterator();
78  
79              // ----------------------------------------------------------------------
80              //
81              // ----------------------------------------------------------------------
82  
83              HtmlSection section = (HtmlSection) elementIterator.next();
84              assertNotNull(section);
85  
86              HtmlAnchor anchor = (HtmlAnchor) elementIterator.next();
87              assertNotNull(anchor);
88              assertEquals("Test", anchor.getAttribute("id"));
89              HtmlHeading1 h1 = (HtmlHeading1) elementIterator.next();
90              assertNotNull(h1);
91              assertEquals("Test", h1.asNormalizedText().trim());
92  
93              HtmlParagraph p = (HtmlParagraph) elementIterator.next();
94              assertNotNull(p);
95              assertEquals(
96                      "You should see a JavaScript alert...", p.asNormalizedText().trim());
97  
98              HtmlScript script = (HtmlScript) elementIterator.next();
99              assertNotNull(script);
100             assertEquals("text/javascript", script.getAttribute("type"));
101             assertEquals("", script.asNormalizedText().trim());
102             List<String> expectedAlerts = Collections.singletonList("Hello!");
103             assertEquals(expectedAlerts, collectedAlerts);
104         }
105     }
106 }