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.html;
16  
17  import java.io.File;
18  
19  import org.htmlunit.WebDriverTestCase;
20  import org.htmlunit.junit.BrowserRunner;
21  import org.htmlunit.junit.annotation.Alerts;
22  import org.junit.Test;
23  import org.junit.runner.RunWith;
24  import org.openqa.selenium.By;
25  import org.openqa.selenium.WebDriver;
26  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
27  
28  /**
29   * Tests for {@link HtmlEmbed}.
30   *
31   * @author Ahmed Ashour
32   * @author Frank Danek
33   */
34  @RunWith(BrowserRunner.class)
35  public class HtmlEmbedTest extends WebDriverTestCase {
36  
37      /**
38       * @throws Exception if the test fails
39       */
40      @Test
41      @Alerts({"[object HTMLEmbedElement]", "[object HTMLCollection]", "1", "[object HTMLEmbedElement]"})
42      public void simpleScriptable() throws Exception {
43          final String html = DOCTYPE_HTML
44              + "<html><head>\n"
45              + "<script>\n"
46              + LOG_TITLE_FUNCTION
47              + "  function test() {\n"
48              + "    log(document.getElementById('myId'));\n"
49              + "    log(document.embeds);\n"
50              + "    log(document.embeds.length);\n"
51              + "    log(document.embeds[0]);\n"
52              + "  }\n"
53              + "</script>\n"
54              + "</head><body onload='test()'>\n"
55              + "  <embed id='myId'/>\n"
56              + "</body></html>";
57  
58          final WebDriver driver = loadPageVerifyTitle2(html);
59          if (driver instanceof HtmlUnitDriver) {
60              final HtmlElement element = toHtmlElement(driver.findElement(By.id("myId")));
61              assertTrue(HtmlEmbed.class.isInstance(element));
62          }
63      }
64  
65      /**
66       * @throws Exception if the test fails
67       */
68      @Test
69      @Alerts("[object HTMLEmbedElement]")
70      public void saveAs() throws Exception {
71          final String html = DOCTYPE_HTML
72              + "<html><head>\n"
73              + "<script>\n"
74              + LOG_TITLE_FUNCTION
75              + "  function test() {\n"
76              + "    log(document.getElementById('myId'));\n"
77              + "  }\n"
78              + "</script>\n"
79              + "</head><body onload='test()'>\n"
80              + "  <embed id='myId' src='helloworld.bin'/>\n"
81              + "</body></html>";
82  
83          getMockWebConnection().setDefaultResponse("something");
84          final WebDriver driver = loadPageVerifyTitle2(html);
85          if (driver instanceof HtmlUnitDriver) {
86              final HtmlEmbed element = (HtmlEmbed) toHtmlElement(driver.findElement(By.id("myId")));
87              final File file = new File(System.getProperty("user.home"), "htmlunit-embed.bin");
88              element.saveAs(file);
89              final long length = file.length();
90              assertTrue(file.delete());
91              assertTrue(length > 0);
92          }
93      }
94  }