1
2
3
4
5
6
7
8
9
10
11
12
13
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
35
36
37
38 public class Html2CanvasTest extends WebDriverTestCase {
39
40
41 protected static Server SERVER_;
42
43
44
45
46 @BeforeAll
47 public static void startSesrver() throws Exception {
48 SERVER_ = WebServerTestCase.createWebServer("src/test/resources/libraries/html2canvas/", null);
49 }
50
51
52
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
65
66 protected URL getBaseUrl() {
67 return URL_FIRST;
68 }
69
70
71
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
81
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
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 }