LocalFileDetector in combination with uploading multiple files not working as expected.
When elements are of the "input[type=file]" kind, Selenium supports sending paths to files with the sendKeys() method. This also works when you need to upload multiple files. This is done by sending multiple paths on different lines (in a single string with line breaks), i.e.(in Java 8):
element.sendKeys("
path/to/first/file
path/to/second/file
");
However, when using a LocalFileDetector, this functionality is not working anymore. The error message is saying that one of the files is not found, whilst when I upload the files separately from the same folder, they can both be found.
Detailed steps to reproduce the behavior:
WebDriver code:
private static WebDriver driver;
public static WebDriver getRemoteChromeDriver() {
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("start-maximized");
options.addArguments("--incognito");
driver = null;
RemoteWebDriver remoteWebDriver = null;
System.out.println("Connecting to Hub URL = " + hubUrl);
try {
remoteWebDriver = new RemoteWebDriver(new URL(hubUrl), options);
} catch (MalformedURLException e) {
e.printStackTrace();
}
remoteWebDriver.setFileDetector(new LocalFileDetector());
driver = remoteWebDriver;
return driver;
}
Test code:
//Elements
@FindBy(css = "input[type=file]")
private WebElement uploadElement;
public void uploadMultipleFiles(List<File> files) {
String multiPath = "";
for (File file : files) {
multiPath += file.getPath() + "\n";
}
//Remove latest \n character
multiPath = multiPath.substring(0, multiPath.length() - 1);
System.out.println("These are all the files being uploaded:");
System.out.println(multiPath);
uploadElement.sendKeys(multiPath);
}
OS: Host machine: MacOSX. Remote WebDriver machine: Windows 10
(The rest of the environment is all regarding Remote Machine)
Browser: Chrome
Browser version: 75.0.3770.142
Browser Driver version: ChromeDriver 75.0.3770.140
Language Bindings version: Java 1.8.0_191
Selenium Grid version (if applicable): 3.141.59
If you need anymore information, please let me know.
Thanks.
To be a little more complete, the actual test using the upload files method is structured as follows:
@Test
void uploadMultipleFilesTest() {
WebDriver driver = WebDriverManager.getRemoteChromeDriver();
List<File> fileList = new ArrayList<>();
fileList.add(new File("path/to/first/file"));
fileList.add(new File("path/to/second/file"));
homePage.uploadMultipleFiles(fileList);
}
I found a workaround for my System Under Test. Apparently: sending the files consecutively with a for-loop is quick enough for my SUT to add the uploads together.
public void uploadAllFiles(List<File> files) {
for (File file : files) uploadElement.sendKeys(file.getPath());
}
However, this workaround might not be applicable to other systems. Therefore not closing issue...
I was sending files consecutively because I was unaware of the ability to send multiple files in one go by sending them as one string separated by newlines.
However since Chrome updated to version 75 I'm having problems with sending files consecutively, somehow files I uploaded before get uploaded again. For example if I upload 3 files by doing it consecutively instead of the expected 3 files total i'm getting file one 3 times, file two 2 times and file three 1 time.
So i'm curious if this issue with a setup of Filedetector, Remotewebdriver and trying to send multiple files in one go can be resolved somehow. It would provide a workaround for the issues of uploading consecutively i'm facing with chrome v75 (and up), plus it's neater and faster to do several files in one go.
Fixed by fc12197e879bbea25e33058444efad46650d2b5b
@barancev Thanks for taking the time to fix this!
I already tried testing your fix by building a "custom" release of selenium with the help of this page. However I did not succeed yet via the bazel or go commands...
Offtopic question but is it possible to create a standalone JAR of the selenium project using the regular java compile commands instead of bazel or go?
You can raise an issue on the instruction too :)
The simplest way to build the standalone JAR is running go all_zip, the result will be in build/dist directory.
@barancev I do not know if you have tested this fix yourself already but I just did using a custom local selenium build and it works as expected.
Thanks again!
Sure we have it tested, I've added a test before fixing the issue: 8264a3a985d0e606015ded0bab1b7455004d7eba
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.