Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium upload file: file not found [docker]

I have following method that uploads image using selenium.

public static void uploadSampleImage(StaticSeleniumDriver driver)
{
    File file = new File(System.getProperty("user.dir") + "/resources/images/" + SAMPLE_DOCUMENT_FILE_NAME);
    Utils.Log("file exists: " + file.exists());

    String imagePath = file.getAbsolutePath();
    WebElement input = driver.findElement(By.name("file"));
    input.sendKeys(imagePath);
}

That's a standard way of feeding file path (like explained in Guru99 tutorial) to upload file.

  1. It works fine when testing locally on windows
  2. It is NOT working when run inside docker container (linux), getting this error:

org.openqa.selenium.InvalidArgumentException: invalid argument: File not found : /usr/src/app/resources/images/image2.png (Session info: chrome=72.0.3626.81) (Driver info: chromedriver=2.46.628388 (4a34a70827ac54148e092aafb70504c4ea7ae926),platform=Linux 4.9.125-linuxkit x86_64) (WARNING: The server did not provide any stacktrace information)

Which is weird because I am sure file exist in given directory (in my method above, I am checking if file exists and log clearly confirms that)

enter image description here

Any suggestions would be welcome, thank you

like image 724
Matthewek Avatar asked Feb 21 '19 15:02

Matthewek


1 Answers

For RemoteWebDriver you have to set file detector driver.setFileDetector(new LocalFileDetector());. Your code:

public static void uploadSampleImage(StaticSeleniumDriver driver)
{
    driver.setFileDetector(new LocalFileDetector());
    File file = new File(System.getProperty("user.dir") + "/resources/images/" + SAMPLE_DOCUMENT_FILE_NAME);
    Utils.Log("file exists: " + file.exists());

    String imagePath = file.getAbsolutePath();
    WebElement input = driver.findElement(By.name("file"));
    input.sendKeys(imagePath);
}
like image 83
Sers Avatar answered Sep 19 '22 11:09

Sers