Today I Learned

hashrocket A Hashrocket project

Attach file to non-visible file input in Capybara

This applies to feature and system RSpecs that use Capybara.

Sometimes you may need to attach a file to a non-visible file input - especially in some UI designs. This means your standard attach_file won't do the trick, since it relies on the element being visible.

 # won't work when input is hidden by CSS
attach_file(
  "document_attachment",
  file_fixture("dummy.pdf")
)

Luckily, the attach_file method has a super easy way to deal with this - just pass make_visible: true and your file input will be made visible just to attach the file. Alternatively, you could pass a hash of css styles to explicitly tell it how to toggle the visibility.

attach_file(
  "document_attachment",
  file_fixture("dummy.pdf"),
  make_visible: true
)

https://www.rubydoc.info/gems/capybara/Capybara%2FNode%2FActions:attach_file

See More #testing TILs