Docker & Selenium-Java :- Unable to upload Image/file in chrome browser running on a docker container

885 Views Asked by At

I running selenium testcases with a remote driver on a docker container.

I want to upload files to a chrome browser running on a conatiner.

I have tried the following:-

  1. Copied file from my local system(MyDocuments folder) to the docker container. When I click on the upload button, I am not sure how to navigate through the folders and upload the file.

I tried this but at "input.sendKeys(imagePath);" line I get the message "element not interactable":- https://stackoverflow.com/a/54810763

  1. I am running testcases on a container but the screenshots are saved on my local machine. Is it possible that I can also upload the files from my local machine and not from container
2

There are 2 best solutions below

0
On

If you need WebCam mock file for Chrome Docker node

I do run JavaScriptExecutor to execute JS file(s) on remote host. Adding dummy input at about:blank

var input=document.createElement('input');
    input.type="file";
    input.accept="video/*";

document.body.appendChild(input);

With enabled LocalFileDetector for RemoveWebDriver do

By locator = By.xpath("//body//input[@type='file']");
this.type(locator, f.getAbsolutePath(), "file");  (webElement.sendKeys(value))

Then run another JavaScriptExecutor for file

var file = document.getElementsByTagName('input')[0].files[0];

var a = document.createElement("a");
a.href = window.URL.createObjectURL(file, {type: "text/plain"});
a.download = "demo.y4m";
a.click();

As a result, we have downloaded demo.y4m to /tmp folder on remote machine. ( Keep in mind /tmp directory on remote machine will keep uploded file untill recreation of container is done)

For specifying upload path

    Map<String, Object> prefs = new HashMap<>();
    prefs.put("download.default_directory", "/tmp");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--use-fake-ui-for-media-stream");
    options.addArguments("--allow-file-access-from-files");
    options.addArguments("--use-fake-device-for-media-stream");
    options.addArguments("--use-file-for-fake-video-capture=/tmp/demo.y4m");
options.setExperimentalOption("prefs", prefs);
0
On
  1. Identify the input element and upload the file
  2. Use a file detector to upload files from your local machine

String FileName = "Test.jpeg";

driver.setFileDetector(new LocalFileDetector());

WebElement element = driver.findElement(By.xpath("//input[text()='Upload File']"));

File file = new File("I want to upload image present in this location");

element.sendKeys(file.getAbsolutePath());