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.annotation.Alerts;
24  import org.htmlunit.junit.annotation.HtmlUnitNYI;
25  import org.junit.jupiter.api.AfterAll;
26  import org.junit.jupiter.api.Assertions;
27  import org.junit.jupiter.api.BeforeAll;
28  import org.junit.jupiter.api.Test;
29  import org.openqa.selenium.By;
30  import org.openqa.selenium.WebDriver;
31  import org.openqa.selenium.WebElement;
32  
33  /**
34   * Tests for html2canvas.
35   *
36   * @author Ronald Brill
37   */
38  public class Html2CanvasTest extends WebDriverTestCase {
39  
40      /** The server. */
41      protected static Server SERVER_;
42  
43      /**
44       * @throws Exception if an error occurs
45       */
46      @BeforeAll
47      public static void startSesrver() throws Exception {
48          SERVER_ = WebServerTestCase.createWebServer("src/test/resources/libraries/html2canvas/", null);
49      }
50  
51      /**
52       * @throws Exception if an error occurs
53       */
54      @AfterAll
55      public static void stopServer() throws Exception {
56          if (SERVER_ != null) {
57              SERVER_.stop();
58              SERVER_.destroy();
59              SERVER_ = null;
60          }
61      }
62  
63      /**
64       * @return the resource base URL
65       */
66      protected URL getBaseUrl() {
67          return URL_FIRST;
68      }
69  
70      /**
71       * @throws Exception if the test fails
72       */
73      @Test
74      @Alerts("data:image/png;base64")
75      @HtmlUnitNYI(CHROME = "nyi",
76              EDGE = "nyi",
77              FF = "nyi",
78              FF_ESR = "nyi")
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 void verify(final Duration maxWaitTime, final WebElement textArea) throws Exception {
98          final long maxWait = System.currentTimeMillis() + maxWaitTime.toMillis();
99  
100         String result = "nyi";
101         while (System.currentTimeMillis() < maxWait) {
102             final String value = textArea.getDomProperty("value");
103             if (value != null && value.startsWith("data:image/png;base64,")) {
104                 result = value;
105                 break;
106             }
107 
108             Thread.sleep(100);
109         }
110 
111         Assertions.assertTrue(result.startsWith(getExpectedAlerts()[0]),
112                 "'" + result + "' does not start with '" + getExpectedAlerts()[0] + "'");
113     }
114 }