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 org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  import org.openqa.selenium.By;
21  import org.openqa.selenium.WebDriver;
22  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
23  
24  /**
25   * Tests for {@link HtmlMeta}.
26   *
27   * @author Ahmed Ashour
28   * @author Daniel Gredler
29   * @author Ronald Brill
30   */
31  public class HtmlMetaTest extends WebDriverTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts("[object HTMLMetaElement]")
38      public void simpleScriptable() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html><head>\n"
41              + "<meta id='m' http-equiv='content-type' content='text/html'>\n"
42              + "<script>\n"
43              + LOG_TITLE_FUNCTION
44              + "  function test() {\n"
45              + "    log(document.getElementById('m'));\n"
46              + "  }\n"
47              + "</script>\n"
48              + "</head><body onload='test()'>\n"
49              + "</body></html>";
50  
51          final WebDriver driver = loadPageVerifyTitle2(html);
52          if (driver instanceof HtmlUnitDriver) {
53              final HtmlPage page = (HtmlPage) getEnclosedPage();
54              assertTrue(HtmlMeta.class.isInstance(page.getHtmlElementById("m")));
55          }
56      }
57  
58      /**
59       * @throws Exception if an error occurs
60       */
61      @Test
62      public void getText() throws Exception {
63          final String html = DOCTYPE_HTML
64                  + "<html><head><meta id='m' http-equiv='a' content='b'></head><body></body></html>";
65  
66          final WebDriver driver = loadPage2(html);
67          final String text = driver.findElement(By.id("m")).getText();
68          assertEquals("", text);
69      }
70  
71      /**
72       * @throws Exception if an error occurs
73       */
74      @Test
75      public void isDisplayed() throws Exception {
76          final String html = DOCTYPE_HTML
77                  + "<html><head><meta id='m' http-equiv='a' content='b'></head><body></body></html>";
78  
79          final WebDriver driver = loadPage2(html);
80          final boolean displayed = driver.findElement(By.id("m")).isDisplayed();
81          assertFalse(displayed);
82      }
83  
84  }