Capybara Cheat Sheet

From WikiOD

Here is the cheatsheat of Capybara and basic guide on how to use Capybara

Navigating[edit | edit source]

visit articles_path

Clicking links and buttons[edit | edit source]

click_on 'Link Text'
click_button
click_link

Interacting with forms[edit | edit source]

attach_file 'Image', '/path/to/image.jpg'
fill_in 'First Name', with: 'John'
check 'A checkbox'
uncheck 'A checkbox'
choose 'A radio button'
select 'Option', from: 'Select box'
unselect

Limiting[edit | edit source]

within '.classname' do
  click '...'
end
within_fieldset :id do
  ...
end

Querying[edit | edit source]

Predicates[edit | edit source]

page.has_css?('.button')
expect(page).to have_css('.button')
page.should have_css('.button')

{: .-setup}

Positive Negative
has_content? has_no_content?
has_css? (selector) has_no_css?
has_xpath? (path) has_no_xpath?
has_link? (selector) has_no_link?
has_button? (selector) has_no_button?
has_field? (selector) has_no_field?
has_checked_field? (selector) has_unchecked_field?
has_table? (selector) has_no_table?
has_select? (selector) has_no_select?

{: .-headers.-left-align}

In Rspec, these also map to matchers like page.should have_content.

Selectors[edit | edit source]

expect(page).to have_button('Save')
expect(page).to have_button('#submit')
expect(page).to have_button('//[@id="submit"]')

The selector arguments can be text, CSS selector, or XPath expression.

RSpec assertions[edit | edit source]

page.has_button?('Save')
expect(page).to have_no_button('Save')

In RSpec, you can use page.should assertions.

About negatives[edit | edit source]

expect(page).to have_no_button('Save')   # OK
expect(page).not_to have_button('Save')  # Bad

Use should have_no_* versions with RSpec matchers because should_not have_* doesn’t wait for a timeout from the driver.

RSpec[edit | edit source]

Matchers[edit | edit source]

expect(page).to \

{: .-setup}

  have_selector '.blank-state'
  have_selector 'h1#hola', text: 'Welcome'
  have_button 'Save'
  have_checked_field '#field'
  have_unchecked_field
  have_css '.class'
  have_field '#field'
  have_table '#table'
  have_xpath '//div'
  have_link 'Logout', href: logout_path
  have_select 'Language',
    selected: 'German'
    options: ['Engish', 'German']
    with_options: ['Engish', 'German'] # partial match
  have_text 'Hello',
    type: :visible # or :all
    # alias: have_content

Common options[edit | edit source]

All matchers have these options: {: .-setup}

  text: 'welcome'
  text: /Hello/
  visible: true
  count: 4
  between: 2..5
  minimum: 2
  maximum: 5
  wait: 10

Other features[edit | edit source]

Finding[edit | edit source]

find(selector)
find_button(selector)
find_by_id(id)
find_field(selector)
find_link(selector)
locate

Scoping[edit | edit source]

within '#delivery' do
  fill_in 'Street', with: 'Hello'
end
within :xpath, '//article'
within_fieldset
within_table
within_frame
scope_to
find('#x').fill_in('Street', with: 'Hello')
# same as within

Scripting[edit | edit source]

execute_script('$("input").trigger("change")')
evaluate_script('window.ga')

Executes JavaScript.

Debugging[edit | edit source]

save_and_open_page

Opens the webpage in your browser.

Page[edit | edit source]

page
  .all('h3')
  .body
  .html
  .source
  .current_host
  .current_path
  .current_url

AJAX[edit | edit source]

using_wait_time 10 do
  ...
end

Misc[edit | edit source]

drag
field_labeled

Page object[edit | edit source]

page.status_code == 200
page.response_headers

See: http://www.rubydoc.info/github/jnicklas/capybara/master/Capybara/Session

Poltergeist[edit | edit source]

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, :inspector => true)
end
Capybara.javascript_driver = :poltergeist

Use poltergeist to integrate PhantomJS.

Blacklist[edit | edit source]

config.before :each, :js do
  page.driver.browser.url_blacklist = [
    'fonts.googleapis.com',
    'use.typekit.net',
    'f.vimeocdn.com',
    'player.vimeo.com',
    'www.googletagmanager.com'
  ].flat_map { |domain| [ "http://#{domain}", "https://#{domain}" ] }
end

Debugging[edit | edit source]

Enable inspector: true and then: {: .-setup}

page.driver.debug

To pause execution for a while:

page.driver.pause

Selenium[edit | edit source]

Accepting confirm() and alert()[edit | edit source]

accept_alert { ... }
dismiss_confirm { ... }
accept_prompt(with: 'hi') { ... }

Alternatively:

page.driver.browser.switch_to.alert.accept

Updating session[edit | edit source]

page.set_rack_session(foo: 'bar')

See also[edit | edit source]

{: .-one-column}

Credit:rstacruz