Sara
Sara

Reputation: 19

Capybara not attaching file with input tag

I've tried attaching a file with different a option but its not working.

1) attach_file('file-input', Rails.root + 'spec/files/books.jpg',:visible => false)

Error: Unable to find file field "file-input"

If I use page.has_css? ('.file-input') its able to find CSS.

I tried using other locators also:

2) attach_file('#library_resource_files', Rails.root + 'spec/files/books.jpg',:visible => false)

Error: Unable to find file field "file-input"

Scenario: Clicks on Upload button click_link 'Upload' which is working fine. and pop up opens , where do I need to click on button to attach the file.

Here is the HTML:

<input multiple="multiple" accept="image/*,
     audio/*,
     video/*,
     application/x-shockwave-flash,
     application/vnd.adobe.flash.movie,
     application/pdf,
     application/msword,
     application/vnd.google-apps.*,
     application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
     application/vnd.openxmlformats-officedocument.presentationml.presentation,
     application/vnd.openxmlformats-officedocument.wordprocessingml.document,
     application/octet-stream,
     text/plain"
 class="file-input" type="file"
 name="library_resource[files][]" id="library_resource_files">

Upvotes: 0

Views: 411

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

From the docs for attach_file - http://www.rubydoc.info/gems/capybara/Capybara/Node/Actions#attach_file-instance_method - it matches elements based on there id, name, or associated labels text (it does not match on a class name or with a CSS selector string). Therefore you would want something like

attach_file('library_resource_files', Rails.root + 'spec/files/books.jpg')

For styling reasons the actual file input is often not visible on the page, If that is true for you then you may (depending on the driver being used) be able to do

attach_file('library_resource_files', Rails.root + 'spec/files/books.jpg', visible: false)

if that doesn't work then you will need to modify the inputs CSS usingexecute_script to make it visible before calling attach_file

Upvotes: 1

Related Questions