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.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
37
38
39
40 @RunWith(BrowserRunner.class)
41 public class Html2CanvasTest extends WebDriverTestCase {
42
43
44 protected static Server SERVER_;
45
46
47
48
49 @BeforeClass
50 public static void startSesrver() throws Exception {
51 SERVER_ = WebServerTestCase.createWebServer("src/test/resources/libraries/html2canvas/", null);
52 }
53
54
55
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
68
69 protected URL getBaseUrl() {
70 return URL_FIRST;
71 }
72
73
74
75
76 @Test
77 @Alerts("data:image/png;base64")
78 @NotYetImplemented
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 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 }