My scenario is the clicking icon on the site opens file browser. Is it possible to attach image to that opened file browser window in spec. My config is Docker, Capybara, Selenium driver. I'm testing on both headless and non-headless browser (Chrome)
3 Answers
The issue with File inputs is the actual <input type="file" ...>
element is often hidden and then a button is added to the page to trigger the file selection instead. Capybara doesn't interact with non-visible elements since a user couldn't, and unfortunately once the file selection dialog has been shown (system dialog box) the browser no longer has any control over it so it can't be automated. The workaround for this is to not click the button that opens the file selection, and instead temporarily make the <input type="file" ...>
element visible on the page so it can be interacted with. To do that Capybara provides a make_visible
option as shown in the docs - https://www.rubydoc.info/github/teamcapybara/capybara/Capybara/Node/Actions#attach_file-instance_method .
attach_file('name, id, or label text of field', file_to_attach, make_visible: true)

- 48,548
- 5
- 64
- 78
Note that filesystems are different for Capybara and remote Chrome as they run in different containers. You could attach volume with files on the same path to both containers. Then filepath specified by Capybara would be found by remote Chrome.

- 1,485
- 14
- 13
Normally, the file inputs are hidden as mentioned by Thomas Walpole. In the code, you can see the attribute "hidden" when you see in developer tools.
You can try removing hidden attribute by JS script and upload the file.
Capybara.current_session.execute_script(
"document.querySelector('element_locator').removeAttribute('hidden')"
)
And then attach file
page.attach_file(
element_locator,
Rails.root.join("features", "support", "upload_files", "file_name")
)
I placed the file to upload in path features->support->upload_files->file_name
This should work.

- 120
- 1
- 6