1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.util.List;
18
19 import org.htmlunit.WebDriverTestCase;
20 import org.htmlunit.junit.BrowserRunner;
21 import org.htmlunit.junit.annotation.Alerts;
22 import org.htmlunit.junit.annotation.BuggyWebDriver;
23 import org.htmlunit.junit.annotation.HtmlUnitNYI;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.openqa.selenium.By;
27 import org.openqa.selenium.WebDriver;
28 import org.openqa.selenium.WebElement;
29 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
30
31
32
33
34
35
36
37 @RunWith(BrowserRunner.class)
38 public class HtmlOption2Test extends WebDriverTestCase {
39
40
41
42
43
44 @Test
45 @Alerts(DEFAULT = {"option1", "", "Number Three", "Number 4",
46 "option1\nNumber Three\nNumber 4"},
47 CHROME = {"option1", "", "Number Three", "Number 4",
48 " option1\n Number Three\n Number 4\n "},
49 EDGE = {"option1", "", "Number Three", "Number 4",
50 " option1\n Number Three\n Number 4\n "})
51 @HtmlUnitNYI(CHROME = {"option1", "", "Number Three", "Number 4",
52 "option1\nNumber Three\nNumber 4"},
53 EDGE = {"option1", "", "Number Three", "Number 4",
54 "option1\nNumber Three\nNumber 4"})
55 public void getVisibleText() throws Exception {
56 final String htmlContent = DOCTYPE_HTML
57 + "<html>\n"
58 + "<head></head>\n"
59 + "<body id='tester'>\n"
60 + " <form>\n"
61 + " <select>\n"
62 + " <option id='option1'>option1</option>\n"
63 + " <option id='option2' label='Number Two'/>\n"
64 + " <option id='option3' label='overridden'>Number Three</option>\n"
65 + " <option id='option4'>Number 4</option>\n"
66 + " </select>\n"
67 + " </form>\n"
68 + "</body></html>";
69
70 final WebDriver driver = loadPage2(htmlContent);
71 String text = driver.findElement(By.id("option1")).getText();
72 assertEquals(getExpectedAlerts()[0], text);
73 text = driver.findElement(By.id("option2")).getText();
74 assertEquals(getExpectedAlerts()[1], text);
75 text = driver.findElement(By.id("option3")).getText();
76 assertEquals(getExpectedAlerts()[2], text);
77 text = driver.findElement(By.id("option4")).getText();
78 assertEquals(getExpectedAlerts()[3], text);
79 text = driver.findElement(By.id("tester")).getText();
80 assertEquals(getExpectedAlerts()[4], text);
81
82 if (driver instanceof HtmlUnitDriver) {
83 final HtmlPage page = (HtmlPage) getEnclosedPage();
84 assertEquals(getExpectedAlerts()[4], page.getElementById("tester").getVisibleText());
85 }
86 }
87
88
89
90
91 @Test
92 @Alerts("[object HTMLOptionElement]")
93 public void simpleScriptable() throws Exception {
94 final String html = DOCTYPE_HTML
95 + "<html><head>\n"
96 + "<script>\n"
97 + LOG_TITLE_FUNCTION
98 + " function test() {\n"
99 + " log(document.getElementById('myId'));\n"
100 + " }\n"
101 + "</script>\n"
102 + "</head><body onload='test()'>\n"
103 + "<select>\n"
104 + " <option id='myId'>test1</option>\n"
105 + " <option id='myId2'>test2</option>\n"
106 + "</select>\n"
107 + "</body></html>";
108
109 final WebDriver driver = loadPageVerifyTitle2(html);
110 if (driver instanceof HtmlUnitDriver) {
111 final HtmlPage page = (HtmlPage) getEnclosedPage();
112 assertTrue(HtmlOption.class.isInstance(page.getHtmlElementById("myId")));
113 }
114 }
115
116
117
118
119 @Test
120 @Alerts({"oDown", "sDown", "dDown", "oUp", "sUp", "dUp"})
121
122
123
124
125
126 @BuggyWebDriver(CHROME = {"sUp", "dUp"},
127 EDGE = {"sUp", "dUp"},
128 FF = {"sDown", "dDown", "sUp", "dUp"},
129 FF_ESR = {"sDown", "dDown", "sUp", "dUp"})
130 public void onMouse() throws Exception {
131 final String html = DOCTYPE_HTML
132 + "<html><head><title>foo</title>\n"
133 + "<script>\n"
134 + LOG_TEXTAREA_FUNCTION
135 + "</script>\n"
136 + "</head><body>\n"
137 + " <form>\n"
138 + " <div"
139 + " onMouseDown='log(\"dDown\");'"
140 + " onMouseUp='log(\"dUp\");'>\n"
141 + " <select name='select1' size='4'"
142 + " onMouseDown='log(\"sDown\");'"
143 + " onMouseUp='log(\"sUp\");'>\n"
144 + " <option id='opt1' value='option1'>Option1</option>\n"
145 + " <option id='opt2' value='option2' selected='selected'>Option2</option>\n"
146 + " <option id='opt3' value='option3'"
147 + " onMouseDown='log(\"oDown\");'"
148 + " onMouseUp='log(\"oUp\");'>Option3</option>\n"
149 + " </select>\n"
150 + " </div>\n"
151 + " </form>\n"
152 + LOG_TEXTAREA
153 + "</body></html>";
154
155 final WebDriver driver = loadPage2(html);
156 driver.findElement(By.id("opt3")).click();
157
158 verifyTextArea2(driver, getExpectedAlerts());
159 }
160
161
162
163
164
165
166 @Test
167 public void isSelected() throws Exception {
168 final String html = DOCTYPE_HTML
169 + "<html><body>"
170 + " <select multiple><option value='a'>a</option><option value='b'>b</option></select>\n"
171 + "</body></html>";
172
173 final WebDriver driver = loadPage2(html);
174
175 final WebElement select = driver.findElement(By.tagName("select"));
176
177 final List<WebElement> options = select.findElements(By.tagName("option"));
178 for (final WebElement option : options) {
179 option.click();
180 }
181
182 for (final WebElement option : options) {
183 assertTrue(option.isSelected());
184 }
185 }
186
187
188
189
190 @Test
191 public void isSelectedJavaScript() throws Exception {
192 final String html = DOCTYPE_HTML
193 + "<html><head>\n"
194 + "<script>\n"
195 + " function test() {\n"
196 + " var s = document.getElementsByTagName('select').item(0);\n"
197 + " var options = s.options;\n"
198 + " for (var i = 0; i < options.length; i++) {\n"
199 + " options[i].selected = true;\n"
200 + " }\n"
201 + " }\n"
202 + "</script>\n"
203 + "</head><body onload='test()'>\n"
204 + " <select multiple><option value='a'>a</option><option value='b'>b</option></select>\n"
205 + "</body></html>";
206
207 final WebDriver driver = loadPage2(html);
208
209 final WebElement select = driver.findElement(By.tagName("select"));
210
211 final List<WebElement> options = select.findElements(By.tagName("option"));
212
213 for (final WebElement option : options) {
214 assertTrue(option.isSelected());
215 }
216 }
217 }