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