1

I'm trying to test file upload in Capybara, Rails 5.2 feature spec with selenium.

I have been searching for this and found a couple of suggestions. One of them is How do you test uploading a file with Capybara and Dropzone.js?

However, these solutions are for jQuery and Dropzone.js. Does anyone have a solution to this?

TrongBang
  • 923
  • 1
  • 12
  • 23

3 Answers3

0

99% of the time passing visible: false as an option to find indicates you're doing something wrong when testing applications with Capybara.

If all you were trying to do is upload a file when the file input is hidden for styling reasons then the Capybara docs provide all the needed info - https://www.rubydoc.info/gems/capybara/Capybara/Node/Actions#attach_file-instance_method - and an option specifically for dealing with this situation

attach_file(Rails.root.join('spec', 'resources', 'test.png'), make_visible: true)
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • If the 99% was correct, I might be in the 1%. I have tested with react-dropzone and it worked. Also the implementation of react-dropzone makes it hidden. – TrongBang Jul 12 '18 at 22:47
  • @TrongBang You're not, and just because it "works" doesn't make it the right way to do it - you're just lucky that you chose to use selenium with chromedriver as your driver, which happens to currently not follow the webdriver spec and lets you send keys at non-visible file field elements (which a user could never do) – Thomas Walpole Jul 12 '18 at 23:20
  • so do you have a solution to this? with the current implementation of react-dropzone, probably you would need to use javascript to make it visible and then use attach_file() method. Any other guess? – TrongBang Jul 13 '18 at 03:34
  • I confirmed the code works as expected. Can you please explain the point why visible: false mostly is wrong? the attach_file() also uses make_visible: true. What is the difference? Thanks – TrongBang Jul 13 '18 at 09:24
  • @TrongBang It's wrong because using `find(..., visible: false).send_keys ...` will fail with most drivers since they (correctly) wont let you interact with non-visible elements (user wouldn't be able to). The general use of `visible: false` is wrong because a user can't see/interact with the elements and thus they are irrelevant to testing an app from a users perspective. The `make_visible` option to `attach_file` handles, in a multi-driver compatible way, the one common case where it isn't currently possible to do things the way a user would/could. – Thomas Walpole Jul 13 '18 at 16:17
  • I hope you dont get this in a wrong way. From the documentation: make_visible (true, Hash) — A Hash of CSS styles to change before attempting to attach the file, if `true` { opacity: 1, display: 'block', visibility: 'visible' } is used (may not be supported by all drivers) I guess it's a trick to make it visible right? – TrongBang Jul 14 '18 at 09:26
  • 1
    @TrongBang It's not a "trick", it is a supported feature of Capybara that temporarily makes the element visible (hence the name `make_visible`) – Thomas Walpole Jul 14 '18 at 16:37
0

Something similar to below may well solve your problem

find(".dropzone").drop(Rails.root.join("spec/fixtures/file.txt"))
Gino
  • 1,043
  • 16
  • 27
-1

the magic code:

        find('input[type="file"]', visible: false).send_keys \
            Rails.root.join('spec', 'resources', 'test.png')
TrongBang
  • 923
  • 1
  • 12
  • 23