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 HtmlPreformattedText}.
28   *
29   * @author Ahmed Ashour
30   * @author Frank Danek
31   * @author Ronald Brill
32   */
33  @RunWith(BrowserRunner.class)
34  public class HtmlPreformattedTextTest extends WebDriverTestCase {
35  
36      /**
37       * @throws Exception if the test fails
38       */
39      @Test
40      @Alerts("[object HTMLPreElement]")
41      public void simpleScriptable() throws Exception {
42          final String html = DOCTYPE_HTML
43              + "<html><head>\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              + "  <pre id='myId'>Some Text</pre>\n"
52              + "</body></html>";
53  
54          final WebDriver driver = loadPageVerifyTitle2(html);
55          if (driver instanceof HtmlUnitDriver) {
56              final HtmlPage page = (HtmlPage) getEnclosedPage();
57              assertTrue(HtmlPreformattedText.class.isInstance(page.getHtmlElementById("myId")));
58          }
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      @Alerts("  hello   abc")
66      public void getText() throws Exception {
67          final String html = DOCTYPE_HTML
68              + "<html><head></head><body>\n"
69              + "<pre id='foo'>  hello \t abc</pre>"
70              + "</body></html>";
71  
72          final WebDriver driver = loadPage2(html);
73          assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("foo")).getText());
74      }
75  
76      /**
77       * @throws Exception if the test fails
78       */
79      @Test
80      @Alerts("1\n2\n3\n4")
81      public void asTextDifferentLineBreaks() throws Exception {
82          final String html = DOCTYPE_HTML
83              + "<html><head></head><body>\n"
84              + "<pre id='foo'>1\n2\r\n3\r4</pre>"
85              + "</body></html>";
86  
87          final WebDriver driver = loadPage2(html);
88          assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("foo")).getText());
89      }
90  
91      /**
92       * @throws Exception if the test fails
93       */
94      @Test
95      @Alerts("start\n  hello   abc \nend")
96      public void asTextInsideDiv() throws Exception {
97          final String html = DOCTYPE_HTML
98              + "<html><head></head><body>\n"
99              + "<div id='foo'>start<pre>  hello \t abc </pre>end</div>"
100             + "</body></html>";
101 
102         final WebDriver driver = loadPage2(html);
103         assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("foo")).getText());
104     }
105 }