fotanus
fotanus

Reputation: 20116

How to test file fields when the file selection is done through a button click?

In my software, I have a dynamically generated file input that is actually triggered by a button click event, like this: $('#my-file-input').focus().click().

While I could use capybara attach_file directly on the file input, I would rather use the button.

I'm already using a web driver to the test. So after click in the button, I can even see the file browser opening, however I can't find a way to automatically select a file. Is there a way I can achieve that?

Upvotes: 2

Views: 404

Answers (2)

Srikanth Venugopalan
Srikanth Venugopalan

Reputation: 9049

I am afraid that the file select window cannot be seen by webdriver, as it is outside the DOM.

The same goes for the other browser controls outside the DOM.

On a side note, I remember facing some permission issues (on IE specifically) when the file select is triggered by JavaScript.

Upvotes: 2

Chloe
Chloe

Reputation: 26264

You will probably have to use a different driver, like Webkit, to execute the Javascript when clicking the button:

https://github.com/jnicklas/capybara#capybara-webkit

The capybara-webkit driver is for true headless testing. It uses QtWebKit to start a rendering engine process. It can execute JavaScript as well. It is significantly faster than drivers like Selenium since it does not load an entire browser.

After the file upload field is created in the DOM, then you use attach_file to select the file.

http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions#attach_file-instance_method

Upvotes: 0

Related Questions