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