View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.libraries;
16  
17  import java.net.URL;
18  
19  import org.eclipse.jetty.server.Server;
20  import org.htmlunit.WebDriverTestCase;
21  import org.htmlunit.WebServerTestCase;
22  import org.htmlunit.junit.BrowserRunner;
23  import org.junit.AfterClass;
24  import org.junit.BeforeClass;
25  import org.junit.Test;
26  import org.junit.runner.RunWith;
27  import org.openqa.selenium.JavascriptExecutor;
28  import org.openqa.selenium.WebDriver;
29  
30  /**
31   * Tests for compatibility with <a href="https://www.chartjs.org/">Chart.js</a>.
32   *
33   * @author Ronald Brill
34   */
35  @RunWith(BrowserRunner.class)
36  public class ChartJs2x9x4Test extends WebDriverTestCase {
37  
38      /** The server. */
39      protected static Server SERVER_;
40  
41      /**
42       * @throws Exception if an error occurs
43       */
44      @BeforeClass
45      public static void startSesrver() throws Exception {
46          SERVER_ = WebServerTestCase.createWebServer("src/test/resources/libraries/chartjs/2.9.4/", null);
47      }
48  
49      /**
50       * @throws Exception if an error occurs
51       */
52      @AfterClass
53      public static void stopServer() throws Exception {
54          if (SERVER_ != null) {
55              SERVER_.stop();
56              SERVER_.destroy();
57              SERVER_ = null;
58          }
59      }
60  
61      /**
62       * @return the resource base URL
63       */
64      protected URL getBaseUrl() {
65          return URL_FIRST;
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      public void simpleBarChart() throws Exception {
73          doTest("simple_bar_chart");
74      }
75  
76      private void doTest(final String filename) throws Exception {
77          final WebDriver driver = getWebDriver();
78          driver.get(getBaseUrl() + filename + ".html");
79  
80          // final WebElement chart = driver.findElement(By.id("myChart"));
81          ((JavascriptExecutor) driver).executeScript("return document.getElementById('myLog').value = "
82                          + "document.getElementById('myChart').toDataURL('image/png') + '§'");
83          final String expected = loadExpectation("/libraries/chartjs/2.9.4/expectations/" + filename, ".expected");
84          verifyTextArea2(driver, expected);
85      }
86  }