Rails system test with capybara
20 Mar 2022Config capybara download path
require 'capybara/rails'
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium,
using: ENV['HEADLESS'] == 'false' ? :chrome : :headless_chrome,
screen_size: [1400, 1400] do |driver_option|
FileUtils.mkdir_p "#{Capybara.save_path}/downloads"
driver_option.add_preference 'download.default_directory', "#{Capybara.save_path}/downloads"
driver_option.add_preference 'download', {default_directory: "#{Capybara.save_path}/downloads"}
driver_option.add_preference 'download.prompt_for_download', false
end
Change wait time temporarily
using_wait_time 3 do
# ...
end
Wanna know how capybara wait works? Check Capybara::Node::Base#synchronize
Selectors special cases
- select option
select "option_name_here", :from => "organizationSelectId" // or find('#tran_action').find(:xpath, 'option[1]').select_option
- find element without raising exception if it’s not exists.
Find the first element on the page matching the given selector and options. By default
#first
will wait up todefault_max_wait_time
seconds for matching elements to appear and then raise an error if no matching element is found, or nil if the provided count options allow for empty results
modal = first ".modal", between: 0..1
click_on "Close" if modal&.visible?
- skip(ignore) within scope, this is useful when you test inside a
within
, but you still ned to check if a global element(like datepicker) exists.
within 'form' do
click_on "birthday"
# assert_selector '.datepicker-dropdown' # raises error
assert_selector page.document, '.datepicker-dropdown' # pass
end
Scroll
# scroll to el
el = find("#el_id")
scroll_to el
# scroll by distance
scroll_to x, y
Debug
use binding.pry
instead of byebug
, as byebug
cause thread blocking https://github.com/deivid-rodriguez/byebug/issues/193
Run system test in CI
run system test in gitlab/github CI require selenium/standalone-chrome , you can check this gist Gitlab CI Config for Minitest/system tests
references: