Skip to content

Instantly share code, notes, and snippets.

@eriktrom
Forked from elfassy/capybara_cheat_sheet.rb
Last active August 29, 2015 14:00
Show Gist options
  • Save eriktrom/a9af6dfe8eea799d940a to your computer and use it in GitHub Desktop.
Save eriktrom/a9af6dfe8eea799d940a to your computer and use it in GitHub Desktop.

Revisions

  1. eriktrom revised this gist Apr 30, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions capybara_cheat_sheet.rb
    Original file line number Diff line number Diff line change
    @@ -81,6 +81,7 @@
    #=Debugging=
    save_and_open_page
    screenshot_and_open_image # with the capybara-screenshot gem
    screenshot_and_save_page

    #=Asynchronous JavaScript=
    click_link('foo')
  2. @elfassy elfassy revised this gist Apr 29, 2014. 1 changed file with 23 additions and 9 deletions.
    32 changes: 23 additions & 9 deletions capybara_cheat_sheet.rb
    Original file line number Diff line number Diff line change
    @@ -52,6 +52,28 @@
    find('//table/tr').click
    all('a').each { |a| a[:href] }

    #=Find actions=
    find("input.file").attach_file
    find("input.checkbox").check
    find("input.select").choose
    find(".button").click_button
    find(".link").click_link
    find(".link").click_link_or_button
    find(".link").click_on
    find(".link").click
    find("input.text").fill_in(:with => 'Jimmy')
    find("input.select").select
    find("input.checkbox").uncheck
    find("input.select").unselect
    find("input.select").unselect_option(option: "Option 5")
    find("input.checkbox").checked?
    find(".button").disabled?
    find(".link").hover
    find("input.select").selected?
    find("input.text").value
    find(".text").text
    find(".link").visible?

    #=Scripting=
    result = page.evaluate_script('4 + 4')
    periods = page.evaluate_script("$('#MainContent_dd').map(function() { return $(this).text() }).get()")
    @@ -69,12 +91,4 @@
    using_wait_time 5 do
    # assertions
    end

    #=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

  3. @elfassy elfassy revised this gist Apr 29, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion capybara_cheat_sheet.rb
    Original file line number Diff line number Diff line change
    @@ -53,7 +53,8 @@
    all('a').each { |a| a[:href] }

    #=Scripting=
    result = page.evaluate_script('4 + 4');
    result = page.evaluate_script('4 + 4')
    periods = page.evaluate_script("$('#MainContent_dd').map(function() { return $(this).text() }).get()")

    #=Debugging=
    save_and_open_page
    @@ -68,6 +69,7 @@
    using_wait_time 5 do
    # assertions
    end

    #=XPath and CSS=
    within(:css, 'ul li') { ... }
    find(:css, 'ul li').text
  4. @elfassy elfassy renamed this gist Apr 29, 2014. 1 changed file with 13 additions and 8 deletions.
    21 changes: 13 additions & 8 deletions capybara cheat sheet → capybara_cheat_sheet.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    ```ruby
    #=Navigating=
    visit('/projects')
    visit(post_comments_path(post))
    @@ -8,23 +7,27 @@
    click_link('Link Text')
    click_button('Save')
    click('Link Text') # Click either a link or a button
    click('Button Value')
    click_on('Button Value')
    find('form.foo .btn').click

    #=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')
    choose("radio_group_selector"), option: "Option 5"
    check('A Checkbox')
    uncheck('A Checkbox')
    attach_file('Image', '/path/to/image.jpg')
    select('Option', :from => 'Select Box')
    unselect('Option', from: select_box)
    find("#select_id").select("value")

    #=scoping=
    within("//li[@id='employee']") do
    within(:xpath, "//li[@id='employee']") do
    fill_in 'Name', :with => 'Jimmy'
    end
    within(:css, "li#employee") do
    within("li#employee") do
    fill_in 'Name', :with => 'Jimmy'
    end
    within_fieldset('Employee') do
    @@ -42,26 +45,29 @@
    page.should have_css('table tr.foo')
    page.should have_content('foo')
    page.should have_no_content('foo')
    expect(page).to have_selector 'foobar'
    find_field('First Name').value
    find_link('Hello').visible?
    find_link('Hello').visible? #false, finds only 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
    screenshot_and_open_image # with the capybara-screenshot gem

    #=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')

    using_wait_time 5 do
    # assertions
    end
    #=XPath and CSS=
    within(:css, 'ul li') { ... }
    find(:css, 'ul li').text
    @@ -70,4 +76,3 @@
    within('ul li') { ... }
    find('ul li').text
    locate('input#name').value
    ```
  5. @elfassy elfassy revised this gist Apr 29, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions capybara cheat sheet
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    ```ruby
    #=Navigating=
    visit('/projects')
    visit(post_comments_path(post))
    @@ -69,3 +70,4 @@
    within('ul li') { ... }
    find('ul li').text
    locate('input#name').value
    ```
  6. @elfassy elfassy revised this gist Apr 29, 2014. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions capybara cheat sheet
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,15 @@
    =Navigating=
    #=Navigating=
    visit('/projects')
    visit(post_comments_path(post))

    =Clicking links and buttons=
    #=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=
    #=Interacting with forms=
    fill_in('First Name', :with => 'John')
    fill_in('Password', :with => 'Seekrit')
    fill_in('Description', :with => 'Really Long Text…')
    @@ -19,7 +19,7 @@
    attach_file('Image', '/path/to/image.jpg')
    select('Option', :from => 'Select Box')

    =scoping=
    #=scoping=
    within("//li[@id='employee']") do
    fill_in 'Name', :with => 'Jimmy'
    end
    @@ -33,7 +33,7 @@
    fill_in 'Name', :with => 'Jimmy'
    end

    =Querying=
    #=Querying=
    page.has_xpath?('//table/tr')
    page.has_css?('table tr.foo')
    page.has_content?('foo')
    @@ -48,20 +48,20 @@
    locate("//*[@id='overlay'").find("//h1").click
    all('a').each { |a| a[:href] }

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

    =Debugging=
    #=Debugging=
    save_and_open_page

    =Asynchronous JavaScript=
    #=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=
    #=XPath and CSS=
    within(:css, 'ul li') { ... }
    find(:css, 'ul li').text
    locate(:css, 'input#name').value
  7. @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