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