1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.file;
16
17 import static java.nio.charset.StandardCharsets.ISO_8859_1;
18
19 import java.io.File;
20
21 import org.apache.commons.io.FileUtils;
22 import org.htmlunit.WebDriverTestCase;
23 import org.htmlunit.junit.annotation.Alerts;
24 import org.junit.jupiter.api.Test;
25 import org.openqa.selenium.By;
26 import org.openqa.selenium.WebDriver;
27
28
29
30
31
32
33 public class FileListTest extends WebDriverTestCase {
34
35
36
37
38 @Test
39 @Alerts({"1", "true"})
40 public void in() throws Exception {
41 final String html = DOCTYPE_HTML
42 + "<html>\n"
43 + "<head>\n"
44 + "<script>\n"
45 + LOG_TITLE_FUNCTION
46 + "function test() {\n"
47 + " if (document.testForm.fileupload.files) {\n"
48 + " var files = document.testForm.fileupload.files;\n"
49 + " log(files.length);\n"
50
51 + " log(0 in files);\n"
52 + " }\n"
53 + "}\n"
54 + "</script>\n"
55 + "</head>\n"
56 + "<body>\n"
57 + " <form name='testForm'>\n"
58 + " <input type='file' id='fileupload' name='fileupload'>\n"
59 + " </form>\n"
60 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
61 + "</body>\n"
62 + "</html>";
63
64 final WebDriver driver = loadPage2(html);
65
66 final File tstFile = File.createTempFile("HtmlUnitUploadTest", ".txt");
67 try {
68 FileUtils.writeStringToFile(tstFile, "Hello HtmlUnit", ISO_8859_1);
69
70 final String path = tstFile.getCanonicalPath();
71 driver.findElement(By.name("fileupload")).sendKeys(path);
72
73 driver.findElement(By.id("testBtn")).click();
74 verifyTitle2(driver, getExpectedAlerts());
75 }
76 finally {
77 FileUtils.deleteQuietly(tstFile);
78 }
79 }
80
81
82
83
84 @Test
85 @Alerts({"1", "[object File]"})
86 public void item() throws Exception {
87 final String html = DOCTYPE_HTML
88 + "<html>\n"
89 + "<head>\n"
90 + "<script>\n"
91 + LOG_TITLE_FUNCTION
92 + "function test() {\n"
93 + " if (document.testForm.fileupload.files) {\n"
94 + " var files = document.testForm.fileupload.files;\n"
95 + " log(files.length);\n"
96
97 + " log(files.item(0));\n"
98 + " }\n"
99 + "}\n"
100 + "</script>\n"
101 + "</head>\n"
102 + "<body>\n"
103 + " <form name='testForm'>\n"
104 + " <input type='file' id='fileupload' name='fileupload'>\n"
105 + " </form>\n"
106 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
107 + "</body>\n"
108 + "</html>";
109
110 final WebDriver driver = loadPage2(html);
111
112 final File tstFile = File.createTempFile("HtmlUnitUploadTest", ".txt");
113 try {
114 FileUtils.writeStringToFile(tstFile, "Hello HtmlUnit", ISO_8859_1);
115
116 final String path = tstFile.getCanonicalPath();
117 driver.findElement(By.name("fileupload")).sendKeys(path);
118
119 driver.findElement(By.id("testBtn")).click();
120 verifyTitle2(driver, getExpectedAlerts());
121 }
122 finally {
123 FileUtils.deleteQuietly(tstFile);
124 }
125 }
126
127
128
129
130 @Test
131 @Alerts({"1", "null", "null"})
132 public void itemWrong() throws Exception {
133 final String html = DOCTYPE_HTML
134 + "<html>\n"
135 + "<head>\n"
136 + "<script>\n"
137 + LOG_TITLE_FUNCTION
138 + "function test() {\n"
139 + " if (document.testForm.fileupload.files) {\n"
140 + " var files = document.testForm.fileupload.files;\n"
141 + " log(files.length);\n"
142
143 + " log(files.item(-1));\n"
144 + " log(files.item(1));\n"
145 + " }\n"
146 + "}\n"
147 + "</script>\n"
148 + "</head>\n"
149 + "<body>\n"
150 + " <form name='testForm'>\n"
151 + " <input type='file' id='fileupload' name='fileupload'>\n"
152 + " </form>\n"
153 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
154 + "</body>\n"
155 + "</html>";
156
157 final WebDriver driver = loadPage2(html);
158
159 final File tstFile = File.createTempFile("HtmlUnitUploadTest", ".txt");
160 try {
161 FileUtils.writeStringToFile(tstFile, "Hello HtmlUnit", ISO_8859_1);
162
163 final String path = tstFile.getCanonicalPath();
164 driver.findElement(By.name("fileupload")).sendKeys(path);
165
166 driver.findElement(By.id("testBtn")).click();
167 verifyTitle2(driver, getExpectedAlerts());
168 }
169 finally {
170 FileUtils.deleteQuietly(tstFile);
171 }
172 }
173
174
175
176
177 @Test
178 @Alerts({"1", "[object File]"})
179 public void indexed() throws Exception {
180 final String html = DOCTYPE_HTML
181 + "<html>\n"
182 + "<head>\n"
183 + "<script>\n"
184 + LOG_TITLE_FUNCTION
185 + "function test() {\n"
186 + " if (document.testForm.fileupload.files) {\n"
187 + " var files = document.testForm.fileupload.files;\n"
188 + " log(files.length);\n"
189
190 + " log(files[0]);\n"
191 + " }\n"
192 + "}\n"
193 + "</script>\n"
194 + "</head>\n"
195 + "<body>\n"
196 + " <form name='testForm'>\n"
197 + " <input type='file' id='fileupload' name='fileupload'>\n"
198 + " </form>\n"
199 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
200 + "</body>\n"
201 + "</html>";
202
203 final WebDriver driver = loadPage2(html);
204
205 final File tstFile = File.createTempFile("HtmlUnitUploadTest", ".txt");
206 try {
207 FileUtils.writeStringToFile(tstFile, "Hello HtmlUnit", ISO_8859_1);
208
209 final String path = tstFile.getCanonicalPath();
210 driver.findElement(By.name("fileupload")).sendKeys(path);
211
212 driver.findElement(By.id("testBtn")).click();
213 verifyTitle2(driver, getExpectedAlerts());
214 }
215 finally {
216 FileUtils.deleteQuietly(tstFile);
217 }
218 }
219
220
221
222
223 @Test
224 @Alerts({"1", "undefined", "undefined"})
225 public void indexedWrong() throws Exception {
226 final String html = DOCTYPE_HTML
227 + "<html>\n"
228 + "<head>\n"
229 + "<script>\n"
230 + LOG_TITLE_FUNCTION
231 + "function test() {\n"
232 + " if (document.testForm.fileupload.files) {\n"
233 + " var files = document.testForm.fileupload.files;\n"
234 + " log(files.length);\n"
235
236 + " log(files[-1]);\n"
237 + " log(files[1]);\n"
238 + " }\n"
239 + "}\n"
240 + "</script>\n"
241 + "</head>\n"
242 + "<body>\n"
243 + " <form name='testForm'>\n"
244 + " <input type='file' id='fileupload' name='fileupload'>\n"
245 + " </form>\n"
246 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
247 + "</body>\n"
248 + "</html>";
249
250 final WebDriver driver = loadPage2(html);
251
252 final File tstFile = File.createTempFile("HtmlUnitUploadTest", ".txt");
253 try {
254 FileUtils.writeStringToFile(tstFile, "Hello HtmlUnit", ISO_8859_1);
255
256 final String path = tstFile.getCanonicalPath();
257 driver.findElement(By.name("fileupload")).sendKeys(path);
258
259 driver.findElement(By.id("testBtn")).click();
260 verifyTitle2(driver, getExpectedAlerts());
261 }
262 finally {
263 FileUtils.deleteQuietly(tstFile);
264 }
265 }
266
267
268
269
270 @Test
271 @Alerts({"1", "[object File]"})
272 public void iterator() throws Exception {
273 final String html = DOCTYPE_HTML
274 + "<html>\n"
275 + "<head>\n"
276 + "<script>\n"
277 + LOG_TITLE_FUNCTION
278 + "function test() {\n"
279 + " if (document.testForm.fileupload.files) {\n"
280 + " var files = document.testForm.fileupload.files;\n"
281 + " log(files.length);\n"
282
283 + " for (var i of files) {\n"
284 + " log(i);\n"
285 + " }\n"
286 + " }\n"
287 + "}\n"
288 + "</script>\n"
289 + "</head>\n"
290 + "<body>\n"
291 + " <form name='testForm'>\n"
292 + " <input type='file' id='fileupload' name='fileupload'>\n"
293 + " </form>\n"
294 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
295 + "</body>\n"
296 + "</html>";
297
298 final WebDriver driver = loadPage2(html);
299
300 final File tstFile = File.createTempFile("HtmlUnitUploadTest", ".txt");
301 try {
302 FileUtils.writeStringToFile(tstFile, "Hello HtmlUnit", ISO_8859_1);
303
304 final String path = tstFile.getCanonicalPath();
305 driver.findElement(By.name("fileupload")).sendKeys(path);
306
307 driver.findElement(By.id("testBtn")).click();
308 verifyTitle2(driver, getExpectedAlerts());
309 }
310 finally {
311 FileUtils.deleteQuietly(tstFile);
312 }
313 }
314 }