1
2
3
4
5
6
7
8
9
10
11
12
13
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
28
29
30
31
32
33 @RunWith(BrowserRunner.class)
34 public class HtmlPreformattedTextTest extends WebDriverTestCase {
35
36
37
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
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
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
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 }