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 HtmlStyle}.
26   *
27   * @author Marc Guillemot
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   */
31  public class HtmlStyle2Test extends WebDriverTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts("[object HTMLStyleElement]")
38      public void simpleScriptable() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html><head>\n"
41              + "<style type='text/css' id='myId'>\n"
42              + "img { border: 0px }\n"
43              + "</style>\n"
44              + "<script>\n"
45              + LOG_TITLE_FUNCTION
46              + "  function test() {\n"
47              + "    log(document.getElementById('myId'));\n"
48              + "  }\n"
49              + "</script>\n"
50              + "</head><body onload='test()'>\n"
51              + "</body></html>";
52  
53          final WebDriver driver = loadPageVerifyTitle2(html);
54          if (driver instanceof HtmlUnitDriver) {
55              final HtmlPage page = (HtmlPage) getEnclosedPage();
56              assertTrue(HtmlStyle.class.isInstance(page.getHtmlElementById("myId")));
57          }
58      }
59  
60      /**
61       * @throws Exception if an error occurs
62       */
63      @Test
64      public void getText() throws Exception {
65          final String html = DOCTYPE_HTML
66                  + "<html>\n"
67                  + "<head>\n"
68                  + "  <title>foo</title>\n"
69                  + "  <style type='text/css' id='s'>\n"
70                  + "    img { border: 0px }\n"
71                  + "  </style>\n"
72                  + "</head>\n"
73                  + "<body>\n"
74                  + "</body>\n"
75                  + "</html>";
76  
77          final WebDriver driver = loadPage2(html);
78          final String text = driver.findElement(By.id("s")).getText();
79          assertEquals("", text);
80      }
81  
82      /**
83       * @throws Exception if an error occurs
84       */
85      @Test
86      public void isDisplayed() throws Exception {
87          final String html = DOCTYPE_HTML
88                  + "<html>\n"
89                  + "<head>\n"
90                  + "  <title>foo</title>\n"
91                  + "  <style type='text/css' id='s'>\n"
92                  + "    img { border: 0px }\n"
93                  + "  </style>\n"
94                  + "</head>\n"
95                  + "<body>\n"
96                  + "</body>\n"
97                  + "</html>";
98  
99          final WebDriver driver = loadPage2(html);
100         final boolean displayed = driver.findElement(By.id("s")).isDisplayed();
101         assertFalse(displayed);
102     }
103 }