0

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
  • Hi Juhi, have you tried mounting your current working directory to the hub container? Refer to [this](https://stackoverflow.com/questions/41485217/mount-current-directory-as-a-volume-in-docker-on-windows-10) answer for more details. – Techrookie89 Jul 07 '21 at 10:27
  • I have used the docker cp command to add the required folder from my local system in the docker container. But when I run my testcase in container to upload a file, I click on upload button and a folder popup opens, I am not sure how to give the path or navigate through the folder using selenium – Juhi Sehgal Jul 07 '21 at 10:30
  • For uploading a file in selenium, you don't actually need to click on the upload button to open the OS level file selector. You basically need to send the absolute path of the file via sendkeys to its corresponding input tag. – Techrookie89 Jul 07 '21 at 10:44
  • Can you also, share an html snippet of the element in which you're trying to upload the file? – Techrookie89 Jul 07 '21 at 10:48
  • The element wherein I want to upload the file is not under the input tag. It is in a span tag – Juhi Sehgal Jul 07 '21 at 10:51

2 Answers2

0
  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());

0

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);