Refer to: https://github.com/jnicklas/capybara
Note: Capybara does not show exceptions created by app. It will instead only tell you that the element you are searching for is not found.
- Gemfile setup
gem "capybara"
gem 'selenium-webdriver' # requires for live browser demostration
gem "launchy" # requires for save_and_open_page
# Checks for CSS tag and content
# Note that View Deal can appear in any nested tags in div.deal
expect(page).to have_css "div.deal", {text: "View Deal"}
page.should have_css ".deal", between: 10..99
# Possible options:
# :text, :visible, :between, :count, :maximum, :minimum, :exact, :match, :wait
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click_on('Link Text') # clicks on either links or buttons
click_on('Button Value')
fill_in('First Name', :with => 'John')
fill_in('Password', :with => 'Seekrit')
fill_in('Description', :with => 'Really Long Text...')
choose('A Radio Button') # choose by label, id or name
check('A Checkbox')
uncheck('A Checkbox')
attach_file('Image', '/path/to/image.jpg')
select('Option', :from => 'Select Box')
find_field('First Name').value
find_link('Hello').visible?
find_button('Send').click
find(:xpath, "//table/tr").click
find("#overlay").find("h1").click
all('a').each { |a| a[:href] }
# Useful to find the first tag out of multiple tags
all('a', text: "View Full Cruise Details").first.click
- Add
js: true
to your describe
describe 'some stuff which requires js', js: true do
it 'will use the default js driver'
it 'will switch to one specific driver', :driver => :webkit
end
ap current_url
- Sometimes Capybara timeouts too fast and fails to find the elements
- Make the waiting time longer by putting it anywhere in
spec_helpers.rb
Capybara.default_wait_time = 5
- Selenium driver runs in a different thread from Capybara
- So they don't share your test data
- You need to configure rspec with
database_cleaner
in order to make it work
# /spec/spec_helper.rb
# Set this to false as it does not work with selenium
config.use_transactional_fixtures = false
# Use database_cleaner for cleaning our test database
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
- Capybara loads page without running the JS. Hence, simple JS like 'Confirm delete?' will be ignored in testing.
- To run JS, you need to run it with selenium driver via js: true.
To confirm a dialog,
page.driver.browser.switch_to.alert.accept
- The conflict comes when you want to see js: true sometimes, and sometimes you don't want to see it. You can resolve it via
page.driver.browser.switch_to.alert.accept if example.metadata[:js]
- If you need to run JS heavily, you need to look into a headless JS at https://github.com/thoughtbot/capybara-webkit All your test cases which requires JS will need to set js: true but a browser will not pop up (because you will set Capybara.javascript_driver = :webkit in the spec_helper.rb which makes it headless) If you want to see the js in action, simply comment out Capybara.javascript_driver = :webkit and run your test case.