- Published on
Rails system tests with Cuprite - attach_file
- Author
- Name
- David Wessman
- @davidwessman
Cuprite is a pure Ruby driver for Capybara and a replacement for Selenium.
When using the Capybara method attach_file
in your system tests, you might run into
Ferrum::TimeoutError: Timed out waiting for response. It's possible that this happened because something took a very long time (for example a page load was slow). If so, setting the :timeout option to a higher value might help.
Ferrum::BrowserError: Internal error
This error is thrown when attach_file
is given a relative file path
attach_file("./test/files/test.pdf") do
find("button.file-upload").click
end
Ferrum::TimeoutError: Timed out waiting for response. It's possible that this happened because something took a very long time (for example a page load was slow). If so, setting the :timeout option to a higher value might help.
Ferrum::BrowserError: Internal error
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/ferrum-0.11/lib/ferrum/browser/client.rb:96:in `raise_browser_error'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/ferrum-0.11/lib/ferrum/browser/client.rb:48:in `command'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/ferrum-0.11/lib/ferrum/page.rb:160:in `command'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/ferrum-0.11/lib/ferrum/page/screenshot.rb:178:in `block (2 levels) in capture_screenshot'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/ferrum-0.11/lib/ferrum/page/screenshot.rb:201:in `with_background_color'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/ferrum-0.11/lib/ferrum/page/screenshot.rb:177:in `block in capture_screenshot'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/ferrum-0.11/lib/ferrum/page/screenshot.rb:189:in `maybe_resize_fullscreen'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/ferrum-0.11/lib/ferrum/page/screenshot.rb:176:in `capture_screenshot'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/ferrum-0.11/lib/ferrum/page/screenshot.rb:34:in `screenshot'
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/forwardable.rb:235:in `screenshot'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/cuprite-0.13/lib/capybara/cuprite/driver.rb:169:in `save_screenshot'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/capybara-3.35.3/lib/capybara/session.rb:735:in `block in save_screenshot'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/capybara-3.35.3/lib/capybara/session.rb:735:in `tap'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/capybara-3.35.3/lib/capybara/session.rb:735:in `save_screenshot'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.1.3.1/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb:95:in `save_image'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.1.3.1/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb:33:in `take_screenshot'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.1.3.1/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb:45:in `take_failed_screenshot'
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.1.3.1/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb:16:in `before_teardown'
Solution
Pass all files as absolute file paths using File.expand_path
attach_file(File.expand_path("./test/files/test.pdf")) do
find("button.file-upload").click
end
To read more about using Cuprite please check out the blog post from Evil Martians.
Thank you for reading, hit me up on Twitter if you want to discuss anything!
Twitterdavidwessman