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  import java.time.Duration;
19  
20  import org.eclipse.jetty.server.Server;
21  import org.htmlunit.WebDriverTestCase;
22  import org.htmlunit.WebServerTestCase;
23  import org.htmlunit.junit.BrowserRunner;
24  import org.htmlunit.junit.annotation.Alerts;
25  import org.htmlunit.junit.annotation.NotYetImplemented;
26  import org.junit.AfterClass;
27  import org.junit.Assert;
28  import org.junit.BeforeClass;
29  import org.junit.Test;
30  import org.junit.runner.RunWith;
31  import org.openqa.selenium.By;
32  import org.openqa.selenium.WebDriver;
33  import org.openqa.selenium.WebElement;
34  
35  /**
36   * Tests for html2canvas.
37   *
38   * @author Ronald Brill
39   */
40  @RunWith(BrowserRunner.class)
41  public class Html2CanvasTest extends WebDriverTestCase {
42  
43      /** The server. */
44      protected static Server SERVER_;
45  
46      /**
47       * @throws Exception if an error occurs
48       */
49      @BeforeClass
50      public static void startSesrver() throws Exception {
51          SERVER_ = WebServerTestCase.createWebServer("src/test/resources/libraries/html2canvas/", null);
52      }
53  
54      /**
55       * @throws Exception if an error occurs
56       */
57      @AfterClass
58      public static void stopServer() throws Exception {
59          if (SERVER_ != null) {
60              SERVER_.stop();
61              SERVER_.destroy();
62              SERVER_ = null;
63          }
64      }
65  
66      /**
67       * @return the resource base URL
68       */
69      protected URL getBaseUrl() {
70          return URL_FIRST;
71      }
72  
73      /**
74       * @throws Exception if the test fails
75       */
76      @Test
77      @Alerts("data:image/png;base64")
78      @NotYetImplemented
79      public void helloWorld() throws Exception {
80          // this does not produce an image in html unit so far
81          // have added this test to not forget it
82          doTest("html2canvas.html");
83      }
84  
85      private void doTest(final String filename) throws Exception {
86          final WebDriver driver = getWebDriver();
87  
88          driver.get(getBaseUrl() + filename);
89          driver.findElement(By.id("printButtonId")).click();
90  
91          // verifyTextArea2(driver, getExpectedAlerts());
92  
93          final WebElement textArea = driver.findElement(By.id("myLog"));
94          verify(DEFAULT_WAIT_TIME, textArea);
95      }
96  
97      private static void verify(final Duration maxWaitTime, final WebElement textArea) throws Exception {
98          final long maxWait = System.currentTimeMillis() + maxWaitTime.toMillis();
99  
100         String value = "";
101         while (System.currentTimeMillis() < maxWait) {
102             value = textArea.getDomProperty("value");
103             if (value != null && value.startsWith("data:image/png;base64,")) {
104                 break;
105             }
106 
107             Thread.sleep(50);
108         }
109 
110         Assert.assertTrue("'" + value + "' does not start with 'data:image/png;base64'",
111                             value.startsWith("data:image/png;base64,"));
112     }
113 }