Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use capybara upload file?

Tags:

capybara

the html like this <input type='file' id='ok' class='lalalalala'>

my code is attach_file("ok","./fileset/publisher/upload_pic.jpg") but I got fail: Failures:

 Capybara::ElementNotFound:
   Unable to find file field "ok"

so what is file field? how can I upload file?

like image 941
user3203342 Avatar asked Jun 17 '14 15:06

user3203342


1 Answers

From the Capybara docs:

The file field can be found via its name, id or label text.

And you clearly have this already, which is a bit puzzling. Is there a modal window blocking this and/or is the page.driver switched to something else by chance? I ask because it's actually not completely necessary to interact with a form button and cause a modal or file/Explorer/Finder window to appear, and sometimes this can confuse the page.driver (ex: switches focus to the window instead of the page).

Try removing any click_button actions just prior to this step, and then try this:

attach_file('ok', File.absolute_path('./fileset/publisher/upload_pic.jpg'))

This has worked for me previously, in a super deeply parented div which ended up with a button class.

like image 93
etusm Avatar answered Oct 10 '22 16:10

etusm