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