Skip to content

Instantly share code, notes, and snippets.

@kitwalker12
Forked from zhengjia/capybara cheat sheet
Last active December 28, 2015 11:19
Show Gist options
  • Select an option

  • Save kitwalker12/7492448 to your computer and use it in GitHub Desktop.

Select an option

Save kitwalker12/7492448 to your computer and use it in GitHub Desktop.

Revisions

  1. kitwalker12 revised this gist Nov 15, 2013. 2 changed files with 133 additions and 71 deletions.
    71 changes: 0 additions & 71 deletions capybara cheat sheet.rb
    Original file line number Diff line number Diff line change
    @@ -1,71 +0,0 @@
    =Navigating=
    visit('/projects')
    visit(post_comments_path(post))

    =Clicking links and buttons=
    click_link('id-of-link')
    click_link('Link Text')
    click_button('Save')
    click('Link Text') # Click either a link or a button
    click('Button Value')

    =Interacting with forms=
    fill_in('First Name', :with => 'John')
    fill_in('Password', :with => 'Seekrit')
    fill_in('Description', :with => 'Really Long Text…')
    choose('A Radio Button')
    check('A Checkbox')
    uncheck('A Checkbox')
    attach_file('Image', '/path/to/image.jpg')
    select('Option', :from => 'Select Box')

    =scoping=
    within("//li[@id='employee']") do
    fill_in 'Name', :with => 'Jimmy'
    end
    within(:css, "li#employee") do
    fill_in 'Name', :with => 'Jimmy'
    end
    within_fieldset('Employee') do
    fill_in 'Name', :with => 'Jimmy'
    end
    within_table('Employee') do
    fill_in 'Name', :with => 'Jimmy'
    end

    =Querying=
    page.has_xpath?('//table/tr')
    page.has_css?('table tr.foo')
    page.has_content?('foo')
    page.should have_xpath('//table/tr')
    page.should have_css('table tr.foo')
    page.should have_content('foo')
    page.should have_no_content('foo')
    find_field('First Name').value
    find_link('Hello').visible?
    find_button('Send').click
    find('//table/tr').click
    locate("//*[@id='overlay'").find("//h1").click
    all('a').each { |a| a[:href] }

    =Scripting=
    result = page.evaluate_script('4 + 4');

    =Debugging=
    save_and_open_page

    =Asynchronous JavaScript=
    click_link('foo')
    click_link('bar')
    page.should have_content('baz')
    page.should_not have_xpath('//a')
    page.should have_no_xpath('//a')

    =XPath and CSS=
    within(:css, 'ul li') { ... }
    find(:css, 'ul li').text
    locate(:css, 'input#name').value
    Capybara.default_selector = :css
    within('ul li') { ... }
    find('ul li').text
    locate('input#name').value
    133 changes: 133 additions & 0 deletions rspec capybara cheat sheet.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,133 @@
    # Capybara

    =Navigating=
    visit('/projects')
    visit(post_comments_path(post))

    =Clicking links and buttons=
    click_link('id-of-link')
    click_link('Link Text')
    click_button('Save')
    click('Link Text') # Click either a link or a button
    click('Button Value')

    =Interacting with forms=
    fill_in('First Name', :with => 'John')
    fill_in('Password', :with => 'Seekrit')
    fill_in('Description', :with => 'Really Long Text…')
    choose('A Radio Button')
    check('A Checkbox')
    uncheck('A Checkbox')
    attach_file('Image', '/path/to/image.jpg')
    select('Option', :from => 'Select Box')

    =scoping=
    within("//li[@id='employee']") do
    fill_in 'Name', :with => 'Jimmy'
    end
    within(:css, "li#employee") do
    fill_in 'Name', :with => 'Jimmy'
    end
    within_fieldset('Employee') do
    fill_in 'Name', :with => 'Jimmy'
    end
    within_table('Employee') do
    fill_in 'Name', :with => 'Jimmy'
    end

    =Querying=
    page.has_xpath?('//table/tr')
    page.has_css?('table tr.foo')
    page.has_content?('foo')
    page.should have_xpath('//table/tr')
    page.should have_css('table tr.foo')
    page.should have_content('foo')
    page.should have_no_content('foo')
    find_field('First Name').value
    find_link('Hello').visible?
    find_button('Send').click
    find('//table/tr').click
    locate("//*[@id='overlay'").find("//h1").click
    all('a').each { |a| a[:href] }

    =Scripting=
    result = page.evaluate_script('4 + 4');

    =Debugging=
    save_and_open_page

    =Asynchronous JavaScript=
    click_link('foo')
    click_link('bar')
    page.should have_content('baz')
    page.should_not have_xpath('//a')
    page.should have_no_xpath('//a')

    =XPath and CSS=
    within(:css, 'ul li') { ... }
    find(:css, 'ul li').text
    locate(:css, 'input#name').value
    Capybara.default_selector = :css
    within('ul li') { ... }
    find('ul li').text
    locate('input#name').value

    #Rspec

    #Model

    @user.should have(1).error_on(:username) # Checks whether there is an error in username
    @user.errors[:username].should include("can't be blank") # check for the error message

    #Rendering
    response.should render_template(:index)

    #Redirecting
    response.should redirect_to(movies_path)


    #Capybara Matchers

    response.body.should have_content("Hello world")
    response.body.should have_no_content("Hello world")

    response.body.should have_css("input#movie_title")
    response.body.should have_css("input#movie_title", :value => "Twelve Angry Men")
    response.body.should have_css("input", :count => 3) #True if there are 3 input tags in response
    response.body.should have_css("input", :maximum => 3) # True if there or fewer or equal to 3 input tags
    response.body.should have_css("input", :minimum => 3) # True if there are minimum of 3 input tags
    response.body.should have_css("input", :between => 1..3) # True if there 1 to 3 input tags
    response.body.should have_css("p a", :text => "hello") # True if there is a anchor tag with text hello
    response.body.should have_css("p a", :text => /[hH]ello(.+)/i) # True if there is a anchor tag with text matching regex

    response.body.should have_xpath("//a")
    response.body.should have_xpath("//a",:href => "google.com")
    response.body.should have_xpath("//a[@href => 'google.com']")
    response.body.should have_xpath("//a[contains(.,'some string')]")
    response.body.should have_xpath("//p//a", :text => /re[dab]i/i, :count => 1)


    # can take both xpath and css as input and can take arguments similar to both have_css and have_xpath
    response.body.should have_selector(:xpath, "//p/h1")
    response.body.should have_selector(:css, "p a#movie_edit_path")

    # For making capybara to take css as default selector
    Capybara.default_selector = :css
    response.body.should have_selector("input") #checks for the presence of the input tag
    response.body.should have_selector("input", :value =>"Twelve Angry Men") # checks for input tag with value
    response.body.should have_no_selector("input")

    # For making capybara to take css as default selector
    Capybara.default_selector = :xpath
    response.body.should have_selector("//input") #checks for the presence of the input tag
    response.body.should have_selector("//input", :value =>"Twelve Angry Men") # checks for input tag with value


    # To access elements inside form
    response.body.should have_field("FirstName") # checks for presence of a input field named FirstName in a form
    response.body.should have_field("FirstName", :value => "Rambo")
    response.body.should have_field("FirstName", :with => "Rambo")

    response.body.should have_link("Foo")
    response.body.should have_link("Foo", :href=>"googl.com")
    response.body.should have_no_link("Foo", :href=>"google.com")
  2. kitwalker12 renamed this gist Nov 15, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. kitwalker12 revised this gist Nov 15, 2013. No changes.
  4. @zhengjia zhengjia created this gist Jun 7, 2010.
    71 changes: 71 additions & 0 deletions capybara cheat sheet
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    =Navigating=
    visit('/projects')
    visit(post_comments_path(post))

    =Clicking links and buttons=
    click_link('id-of-link')
    click_link('Link Text')
    click_button('Save')
    click('Link Text') # Click either a link or a button
    click('Button Value')

    =Interacting with forms=
    fill_in('First Name', :with => 'John')
    fill_in('Password', :with => 'Seekrit')
    fill_in('Description', :with => 'Really Long Text…')
    choose('A Radio Button')
    check('A Checkbox')
    uncheck('A Checkbox')
    attach_file('Image', '/path/to/image.jpg')
    select('Option', :from => 'Select Box')

    =scoping=
    within("//li[@id='employee']") do
    fill_in 'Name', :with => 'Jimmy'
    end
    within(:css, "li#employee") do
    fill_in 'Name', :with => 'Jimmy'
    end
    within_fieldset('Employee') do
    fill_in 'Name', :with => 'Jimmy'
    end
    within_table('Employee') do
    fill_in 'Name', :with => 'Jimmy'
    end

    =Querying=
    page.has_xpath?('//table/tr')
    page.has_css?('table tr.foo')
    page.has_content?('foo')
    page.should have_xpath('//table/tr')
    page.should have_css('table tr.foo')
    page.should have_content('foo')
    page.should have_no_content('foo')
    find_field('First Name').value
    find_link('Hello').visible?
    find_button('Send').click
    find('//table/tr').click
    locate("//*[@id='overlay'").find("//h1").click
    all('a').each { |a| a[:href] }

    =Scripting=
    result = page.evaluate_script('4 + 4');

    =Debugging=
    save_and_open_page

    =Asynchronous JavaScript=
    click_link('foo')
    click_link('bar')
    page.should have_content('baz')
    page.should_not have_xpath('//a')
    page.should have_no_xpath('//a')

    =XPath and CSS=
    within(:css, 'ul li') { ... }
    find(:css, 'ul li').text
    locate(:css, 'input#name').value
    Capybara.default_selector = :css
    within('ul li') { ... }
    find('ul li').text
    locate('input#name').value