Skip to content

Commit 7facf93

Browse files
committed
Implementing ability to clear file input fields (Firefox only; other browsers need to update atoms)
1 parent 53fb1a0 commit 7facf93

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Diff for: java/client/test/org/openqa/selenium/UploadTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717

1818
package org.openqa.selenium;
1919

20+
import static org.junit.Assert.assertEquals;
2021
import static org.junit.Assume.assumeFalse;
2122
import static org.openqa.selenium.Platform.ANDROID;
2223
import static org.openqa.selenium.WaitingConditions.elementTextToEqual;
2324
import static org.openqa.selenium.support.ui.ExpectedConditions.not;
2425
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf;
26+
import static org.openqa.selenium.testing.Ignore.Driver.CHROME;
27+
import static org.openqa.selenium.testing.Ignore.Driver.HTMLUNIT;
28+
import static org.openqa.selenium.testing.Ignore.Driver.IE;
2529
import static org.openqa.selenium.testing.Ignore.Driver.MARIONETTE;
30+
import static org.openqa.selenium.testing.Ignore.Driver.PHANTOMJS;
2631
import static org.openqa.selenium.testing.Ignore.Driver.SAFARI;
2732

2833
import com.google.common.base.Charsets;
@@ -75,6 +80,16 @@ public void testFileUploading() throws Exception {
7580
wait.until(elementTextToEqual(body, LOREM_IPSUM_TEXT));
7681
}
7782

83+
@Test
84+
@Ignore(value = {CHROME, HTMLUNIT, IE, MARIONETTE, PHANTOMJS, SAFARI})
85+
public void testCleanFileInput() throws Exception {
86+
driver.get(pages.uploadPage);
87+
WebElement element = driver.findElement(By.id("upload"));
88+
element.sendKeys(testFile.getAbsolutePath());
89+
element.clear();
90+
assertEquals("", element.getAttribute("value"));
91+
}
92+
7893
private File createTmpFile(String content) throws IOException {
7994
File f = File.createTempFile("webdriver", "tmp");
8095
f.deleteOnExit();

Diff for: javascript/atoms/dom.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,20 @@ bot.dom.isTextual = function(element) {
409409
};
410410

411411

412+
/**
413+
* @param {!Element} element The element to check.
414+
* @return {boolean} Whether the element is a file input.
415+
*/
416+
bot.dom.isFileInput = function(element) {
417+
if (bot.dom.isElement(element, goog.dom.TagName.INPUT)) {
418+
var type = element.type.toLowerCase();
419+
return type == 'file';
420+
}
421+
422+
return false;
423+
};
424+
425+
412426
/**
413427
* @param {!Element} element The element to check.
414428
* @return {boolean} Whether the element is contentEditable.
@@ -450,7 +464,7 @@ bot.dom.isContentEditable = function(element) {
450464
* @return {boolean} Whether the element accepts user-typed text.
451465
*/
452466
bot.dom.isEditable = function(element) {
453-
return bot.dom.isTextual(element) &&
467+
return (bot.dom.isTextual(element) || bot.dom.isFileInput(element)) &&
454468
!bot.dom.getProperty(element, 'readOnly');
455469
};
456470

Diff for: javascript/atoms/test/action_test.html

+11
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@
173173
assertThrows(goog.partial(bot.action.clear, e));
174174
}
175175

176+
function testClearingAFileInputShouldBeNoOp() {
177+
var e = goog.dom.getElement('fileField');
178+
doNotExpectChangeEvent(e);
179+
bot.action.clear(e);
180+
assertEquals('', e.value);
181+
}
182+
176183
function testSubmittingANonFormElementShouldResultInAnError() {
177184
assertThrows(goog.partial(bot.action.submit, document.body));
178185
}
@@ -255,6 +262,10 @@
255262
<label for="passwordField">Password field</label>
256263
<input type="password" id="passwordField">
257264
</div>
265+
</div>
266+
<label for="fileField">File field</label>
267+
<input type="file" id="fileField">
268+
</div>
258269
</form>
259270

260271
<div id="log">

0 commit comments

Comments
 (0)