-1

I am using Selenium WebDriver for automation. Uploading files in WebDriver is done by simply using the sendKeys() method on the file input field.

Code snippet:

WebElement uploadElement = driver.findElement(By.id("uploadfile"));

// enter the absolute file path into the file input field
uploadElement.sendKeys("C:\\test.txt");

Above code snippet works as expected when script execution is running on Local machine.. But it is not working when script execution is running on Zalenium docker container.

Sagar Bhavsar
  • 278
  • 3
  • 8

2 Answers2

0

File upload is relatively very simple but slightly different when you are using Docker concept. You need to ensure that you set the file detector for the file (using LocalFileDetector class) which you want to upload.

Refer below code snippet:

WebElement uploadElement = driver.findElement(By.id("uploadfile"));

LocalFileDetector detector = new LocalFileDetector();
File localFile = detector.getLocalFile("C:\\test.txt");

uploadElement.setFileDetector(detector);

// enter the absolute file path into the file input field
uploadElement.sendKeys(localFile.getAbsolutePath());

Above code snippet will upload the file when script execution on Local/Remote/Zalenium docker container.

Sagar Bhavsar
  • 278
  • 3
  • 8
0

This worked for me. There is no need to mount volume to get this done

File file = new File(filePath); //my local filepath where the file will be created
        File tempDir = new File(System.getProperty("java.io.tmpdir", null), "uploadFile");
        if (!tempDir.exists()) {
            tempDir.mkdir();
        }
        File fileToCreate = new File(tempDir, file.getName());
        byte[] bytes = Base64.getDecoder().decode(value.getBytes());
        FileUtils.writeByteArrayToFile(fileToCreate, bytes);
        Thread.sleep(3000);
        RemoteWebDriver remoteDriver = new RemoteWebDriver(
                        new URL("http://localhost:4444/wd/hub"), capabilities);
        remoteDriver.setFileDetector(new LocalFileDetector());
        remoteDriver.findElement(locator).sendKeys(fileToCreate.toString());
chinnu Nish
  • 119
  • 1
  • 13