Skip to content

Instantly share code, notes, and snippets.

@zakelfassi
Forked from zhengjia/capybara cheat sheet
Last active August 29, 2015 14:04
Show Gist options
  • Save zakelfassi/b543c6389f7293a03525 to your computer and use it in GitHub Desktop.
Save zakelfassi/b543c6389f7293a03525 to your computer and use it in GitHub Desktop.

Revisions

  1. zakelfassi revised this gist Aug 6, 2014. 1 changed file with 68 additions and 60 deletions.
    128 changes: 68 additions & 60 deletions capybaraSheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -7,77 +7,85 @@ visit(post_comments_path(post))
    ```

    ## Clicking links and buttons
    ```ruby```
    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')
    ```ruby
    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')
    ```ruby
    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
    ```ruby
    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] }
    ```ruby
    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');
    ```ruby
    result = page.evaluate_script('4 + 4');
    ```

    ## Debugging

    save_and_open_page
    ```ruby
    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')
    ```ruby
    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
    ```ruby
    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
    ```
  2. zakelfassi revised this gist Aug 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion capybaraSheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ visit(post_comments_path(post))
    ```

    ## Clicking links and buttons

    ```ruby```
    click_link('id-of-link')
    click_link('Link Text')
    click_button('Save')
  3. zakelfassi revised this gist Aug 6, 2014. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions capybaraSheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,10 @@
    # Capybara Cheat Sheet

    ## Navigating

    visit('/projects')
    visit(post_comments_path(post))
    ```ruby
    visit('/projects')
    visit(post_comments_path(post))
    ```

    ## Clicking links and buttons

  4. zakelfassi revised this gist Aug 6, 2014. 1 changed file with 20 additions and 9 deletions.
    29 changes: 20 additions & 9 deletions capybaraSheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,20 @@
    =Navigating=
    # Capybara Cheat Sheet

    ## 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 +24,8 @@
    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 +39,8 @@
    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 +55,24 @@
    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
  5. zakelfassi renamed this gist Aug 6, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. zakelfassi revised this gist Aug 6, 2014. No changes.
  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