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 com.gargoylesoftware.htmlunit.CollectingAlertHandler;
28  import com.gargoylesoftware.htmlunit.WebClient;
29  import com.gargoylesoftware.htmlunit.html.HtmlElement;
30  import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
31  import com.gargoylesoftware.htmlunit.html.HtmlMain;
32  import com.gargoylesoftware.htmlunit.html.HtmlPage;
33  import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
34  import com.gargoylesoftware.htmlunit.html.HtmlScript;
35  import com.gargoylesoftware.htmlunit.html.HtmlSection;
36  
37  import static org.codehaus.plexus.testing.PlexusExtension.getTestFile;
38  import static org.junit.jupiter.api.Assertions.assertEquals;
39  import static org.junit.jupiter.api.Assertions.assertNotNull;
40  import static org.junit.jupiter.api.Assertions.assertTrue;
41  
42  /**
43   * Verify javascript code.
44   *
45   * @author ltheussl
46   */
47  public class JavascriptVerifier extends AbstractVerifier {
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) throws Exception {
56          File jsTest = getTestFile("target/output/javascript.html");
57          assertNotNull(jsTest);
58          assertTrue(jsTest.exists());
59  
60          // HtmlUnit
61          try (WebClient webClient = new WebClient()) {
62              webClient.getOptions().setCssEnabled(false);
63  
64              final List<String> collectedAlerts = new ArrayList<>(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              HtmlMain main = (HtmlMain) element;
73              assertNotNull(main);
74  
75              Iterator<HtmlElement> elementIterator =
76                      main.getHtmlElementDescendants().iterator();
77  
78              // ----------------------------------------------------------------------
79              //
80              // ----------------------------------------------------------------------
81  
82              HtmlSection section = (HtmlSection) elementIterator.next();
83              assertNotNull(section);
84  
85              HtmlHeading1 h1 = (HtmlHeading1) elementIterator.next();
86              assertNotNull(h1);
87              assertEquals("Test", h1.asNormalizedText().trim());
88  
89              HtmlParagraph p = (HtmlParagraph) elementIterator.next();
90              assertNotNull(p);
91              assertEquals(
92                      "You should see a JavaScript alert...", p.asNormalizedText().trim());
93  
94              HtmlScript script = (HtmlScript) elementIterator.next();
95              assertNotNull(script);
96              assertEquals("text/javascript", script.getAttribute("type"));
97              assertEquals("", script.asNormalizedText().trim());
98              List<String> expectedAlerts = Collections.singletonList("Hello!");
99              assertEquals(expectedAlerts, collectedAlerts);
100         }
101     }
102 }