Skip to content

Instantly share code, notes, and snippets.

@askamiroff
Forked from tomas-stefano/Capybara.md
Created May 12, 2016 09:50
Show Gist options
  • Select an option

  • Save askamiroff/c1325c7f0e5e2da06d5dd4de76a3d00c to your computer and use it in GitHub Desktop.

Select an option

Save askamiroff/c1325c7f0e5e2da06d5dd4de76a3d00c to your computer and use it in GitHub Desktop.

Revisions

  1. @tomas-stefano tomas-stefano revised this gist Sep 22, 2013. 1 changed file with 127 additions and 120 deletions.
    247 changes: 127 additions & 120 deletions Capybara.md
    Original file line number Diff line number Diff line change
    @@ -1,183 +1,190 @@
    ## Capybara Actions

    ```ruby
    # Anchor
    click_link 'Save'

    click_link 'Save'
    # Button
    click_button 'awesome'

    click_button 'awesome'
    # Both above
    click_link_or_button 'Save'

    click_link_or_button 'Save'
    # Text (area) field
    fill_in 'Name', with: 'Content'

    fill_in 'Name', with: 'Content'
    # Checkbox
    check 'Content'
    uncheck 'Content'

    check 'Content'
    uncheck 'Content'
    # Radio button
    choose 'Content'

    choose 'Content'
    # Select option from select tag
    select 'Option', from: 'Label'

    select 'Option', from: 'Label'

    attach_file Rails.root.join('spec/fixture/some_file.png')
    # File input
    attach_file Rails.root.join('spec/fixture/some_file.png')
    ```

    ## Capybara finders
    ## Capybara Finders

    ```ruby
    page.all(:xpath, '//a')

    page.all(:xpath, '//a')

    page.first(:xpath, '//a')
    page.first(:xpath, '//a')

    page.find('//textarea[@id="additional_newline"]')
    page.find('//textarea[@id="additional_newline"]')

    page.find(:xpath, "//input[@id='form_pets_dog']")['checked']
    # => true
    page.find(:xpath, "//input[@id='form_pets_dog']")['checked']
    # => true

    page.find(:css, '#with_focus_event').trigger(:focus)
    page.find(:css, '#with_focus_event').trigger(:focus)

    page.find(:css,'.wrapper').hover
    page.find(:css,'.wrapper').hover

    page.find_field("test_field").value
    # => 'blah'
    page.find_field("test_field").value
    # => 'blah'

    page.find_by_id('red').tag_name
    # => 'a'
    page.find_by_id('red').tag_name
    # => 'a'

    # finds invisible elements when false
    page.find_by_id("hidden_via_ancestor", visible: false)
    # finds invisible elements when false
    page.find_by_id("hidden_via_ancestor", visible: false)

    page.find_button('What an Awesome')[:value]
    # => 'awesome'
    page.find_button('What an Awesome')[:value]
    # => 'awesome'

    page.find_link('abo').text
    # => 'labore'
    page.find_link('abo').text
    # => 'labore'

    page.find_link('other')[:href]
    # => '/some_uri'
    page.find_link('other')[:href]
    # => '/some_uri'
    ```

    ## Capybara within
    **Note:** `find` will wait for an element to appear on the page, as explained in the Ajax section. If the element does not appear it will raise an error.

    **Note:** In XPath the expression `//` means something very specific, and it might not be what you think. Contrary to common belief, `//` means "anywhere in the document" not "anywhere in the current context".

    ```ruby

    within(search_form) do
    fill_in 'Name', with: 'iOS 7'
    click_button 'Search'
    end
    ## Capybara Scoped Finder `within`

    def search_form
    '.search_form'
    end
    ```ruby
    within(search_form) do
    fill_in 'Name', with: 'iOS 7'
    click_button 'Search'
    end

    def search_form
    '.search_form'
    end

    within_fieldset("villain_fieldset") do
    # ...
    end
    within_fieldset("villain_fieldset") do
    # ...
    end

    within_table("some_table") do
    # ...
    end
    within_table("some_table") do
    # ...
    end

    # Execute the given block within the given iframe using given frame name or index.
    #
    within_frame('some_frame') do
    end
    # Execute the given block within the given iframe using given frame name or index.
    #
    within_frame('some_frame') do
    end

    save_page
    save_page

    # You need to install launchy gem.
    save_and_open_page
    # You need to install launchy gem.
    save_and_open_page
    ```

    ## Capybara common
    ## Capybara Common

    ```ruby

    visit("http:google.com")

    page.current_url

    # Execute the given script, not returning a result. This is useful for scripts that return
    # complex objects, such as jQuery statements. +execute_script+ should be used over
    # +evaluate_script+ whenever possible.
    #
    page.execute_script("$('#change').text('Funky Doodle')")

    # Evaluate the given JavaScript and return the result. Be careful when using this with
    # scripts that return complex objects, such as jQuery statements. +execute_script+ might
    # be a better alternative.
    #
    page.evaluate_script("1+3")
    # => 4

    using_wait_time 6 do
    # ... Changed Capybara.default_wait_time in this block scope.
    end
    visit("http://google.com")

    page.current_url

    # Execute the given script, not returning a result. This is useful for scripts that return
    # complex objects, such as jQuery statements. +execute_script+ should be used over
    # +evaluate_script+ whenever possible.
    #
    page.execute_script("$('#change').text('Funky Doodle')")

    # Evaluate the given JavaScript and return the result. Be careful when using this with
    # scripts that return complex objects, such as jQuery statements. +execute_script+ might
    # be a better alternative.
    #
    page.evaluate_script("1+3")
    # => 4

    using_wait_time 6 do
    # ... Changed Capybara.default_wait_time in this block scope.
    end
    ```

    ## Capybara Matchers

    ```ruby
    expect(page).to have_content("Some Content")
    expect(page).to have_no_content("Some Content")

    expect(page).to have_content("Some Content")
    expect(page).to have_no_content("Some Content")

    # True if there is a anchor tag with text matching regex
    expect(page).to have_xpath("//a")
    expect(page).to have_xpath("//a",:href => "google.com")
    expect(page).to have_xpath("//a[@href => 'google.com']")
    expect(page).to have_xpath("//a[contains(.,'some string')]")
    expect(page).to have_xpath("//p//a", :text => /re[dab]i/i, :count => 1)
    # True if there is a anchor tag with text matching regex
    expect(page).to have_xpath("//a")
    expect(page).to have_xpath("//a",:href => "google.com")
    expect(page).to have_xpath("//a[@href => 'google.com']")
    expect(page).to have_xpath("//a[contains(.,'some string')]")
    expect(page).to 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
    expect(page).to have_selector(:xpath, "//p/h1")
    expect(page).to have_selector(:css, "p a#post_edit_path")
    # can take both xpath and css as input and can take arguments similar to both have_css and have_xpath
    expect(page).to have_selector(:xpath, "//p/h1")
    expect(page).to have_selector(:css, "p a#post_edit_path")

    expect(page).to have_css("input#post_title")
    expect(page).to have_css("input#post_title", :value => "Capybara cheatsheet")
    expect(page).to have_css("input#post_title")
    expect(page).to have_css("input#post_title", :value => "Capybara cheatsheet")

    # True if there are 3 input tags in response
    expect(page).to have_css("input", :count => 3)
    # True if there are 3 input tags in response
    expect(page).to have_css("input", :count => 3)

    # True if there or fewer or equal to 3 input tags
    expect(page).to have_css("input", :maximum => 3)
    # True if there or fewer or equal to 3 input tags
    expect(page).to have_css("input", :maximum => 3)

    # True if there are minimum of 3 input tags
    expect(page).to have_css("input", :minimum => 3)
    # True if there are minimum of 3 input tags
    expect(page).to have_css("input", :minimum => 3)

    # True if there 1 to 3 input tags
    expect(page).to have_css("input", :between => 1..3)
    # True if there 1 to 3 input tags
    expect(page).to have_css("input", :between => 1..3)

    # True if there is a anchor tag with text hello
    expect(page).to have_css("p a", :text => "hello")
    expect(page).to have_css("p a", :text => /[hH]ello(.+)/i)
    # True if there is a anchor tag with text hello
    expect(page).to have_css("p a", :text => "hello")
    expect(page).to have_css("p a", :text => /[hH]ello(.+)/i)

    # For making capybara to take css as default selector
    Capybara.default_selector = :css
    # For making capybara to take css as default selector
    Capybara.default_selector = :css

    # checks for the presence of the input tag
    expect(page).to have_selector("input")
    # checks for the presence of the input tag
    expect(page).to have_selector("input")

    # checks for input tag with value
    expect(page).to have_selector("input", :value =>"Post Title")
    # checks for input tag with value
    expect(page).to have_selector("input", :value =>"Post Title")

    expect(page).to have_no_selector("input")
    expect(page).to have_no_selector("input")

    # For making capybara to take css as default selector
    Capybara.default_selector = :xpath
    # checks for the presence of the input tag
    expect(page).to have_selector("//input")
    # For making capybara to take css as default selector
    Capybara.default_selector = :xpath
    # checks for the presence of the input tag
    expect(page).to have_selector("//input")

    # checks for input tag with value
    expect(page).to have_selector("//input", :value =>"Post Title")
    # checks for input tag with value
    expect(page).to have_selector("//input", :value =>"Post Title")

    # checks for presence of a input field named FirstName in a form
    expect(page).to have_field("FirstName")
    # checks for presence of a input field named FirstName in a form
    expect(page).to have_field("FirstName")

    expect(page).to have_field("FirstName", :value => "Rambo")
    expect(page).to have_field("FirstName", :with => "Rambo")
    expect(page).to have_field("FirstName", :value => "Rambo")
    expect(page).to have_field("FirstName", :with => "Rambo")

    expect(page).to have_link("Foo")
    expect(page).to have_link("Foo", :href=>"googl.com")
    expect(page).to have_no_link("Foo", :href=>"google.com")
    expect(page).to have_link("Foo")
    expect(page).to have_link("Foo", :href=>"googl.com")
    expect(page).to have_no_link("Foo", :href=>"google.com")
    ```
  2. @tomas-stefano tomas-stefano revised this gist Sep 22, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Capybara.md
    Original file line number Diff line number Diff line change
    @@ -24,9 +24,9 @@

    ```ruby

    page.all('//a')
    page.all(:xpath, '//a')

    page.first('//a')
    page.first(:xpath, '//a')

    page.find('//textarea[@id="additional_newline"]')

  3. @tomas-stefano tomas-stefano created this gist Sep 21, 2013.
    183 changes: 183 additions & 0 deletions Capybara.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,183 @@
    ## Capybara Actions

    ```ruby

    click_link 'Save'

    click_button 'awesome'

    click_link_or_button 'Save'

    fill_in 'Name', with: 'Content'

    check 'Content'
    uncheck 'Content'

    choose 'Content'

    select 'Option', from: 'Label'

    attach_file Rails.root.join('spec/fixture/some_file.png')
    ```

    ## Capybara finders

    ```ruby

    page.all('//a')

    page.first('//a')

    page.find('//textarea[@id="additional_newline"]')

    page.find(:xpath, "//input[@id='form_pets_dog']")['checked']
    # => true

    page.find(:css, '#with_focus_event').trigger(:focus)

    page.find(:css,'.wrapper').hover

    page.find_field("test_field").value
    # => 'blah'

    page.find_by_id('red').tag_name
    # => 'a'

    # finds invisible elements when false
    page.find_by_id("hidden_via_ancestor", visible: false)

    page.find_button('What an Awesome')[:value]
    # => 'awesome'

    page.find_link('abo').text
    # => 'labore'

    page.find_link('other')[:href]
    # => '/some_uri'
    ```

    ## Capybara within


    ```ruby

    within(search_form) do
    fill_in 'Name', with: 'iOS 7'
    click_button 'Search'
    end

    def search_form
    '.search_form'
    end

    within_fieldset("villain_fieldset") do
    # ...
    end

    within_table("some_table") do
    # ...
    end

    # Execute the given block within the given iframe using given frame name or index.
    #
    within_frame('some_frame') do
    end

    save_page

    # You need to install launchy gem.
    save_and_open_page
    ```

    ## Capybara common

    ```ruby

    visit("http:google.com")

    page.current_url

    # Execute the given script, not returning a result. This is useful for scripts that return
    # complex objects, such as jQuery statements. +execute_script+ should be used over
    # +evaluate_script+ whenever possible.
    #
    page.execute_script("$('#change').text('Funky Doodle')")

    # Evaluate the given JavaScript and return the result. Be careful when using this with
    # scripts that return complex objects, such as jQuery statements. +execute_script+ might
    # be a better alternative.
    #
    page.evaluate_script("1+3")
    # => 4

    using_wait_time 6 do
    # ... Changed Capybara.default_wait_time in this block scope.
    end
    ```

    ## Capybara Matchers

    ```ruby

    expect(page).to have_content("Some Content")
    expect(page).to have_no_content("Some Content")

    # True if there is a anchor tag with text matching regex
    expect(page).to have_xpath("//a")
    expect(page).to have_xpath("//a",:href => "google.com")
    expect(page).to have_xpath("//a[@href => 'google.com']")
    expect(page).to have_xpath("//a[contains(.,'some string')]")
    expect(page).to 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
    expect(page).to have_selector(:xpath, "//p/h1")
    expect(page).to have_selector(:css, "p a#post_edit_path")

    expect(page).to have_css("input#post_title")
    expect(page).to have_css("input#post_title", :value => "Capybara cheatsheet")

    # True if there are 3 input tags in response
    expect(page).to have_css("input", :count => 3)

    # True if there or fewer or equal to 3 input tags
    expect(page).to have_css("input", :maximum => 3)

    # True if there are minimum of 3 input tags
    expect(page).to have_css("input", :minimum => 3)

    # True if there 1 to 3 input tags
    expect(page).to have_css("input", :between => 1..3)

    # True if there is a anchor tag with text hello
    expect(page).to have_css("p a", :text => "hello")
    expect(page).to have_css("p a", :text => /[hH]ello(.+)/i)

    # For making capybara to take css as default selector
    Capybara.default_selector = :css

    # checks for the presence of the input tag
    expect(page).to have_selector("input")

    # checks for input tag with value
    expect(page).to have_selector("input", :value =>"Post Title")

    expect(page).to have_no_selector("input")

    # For making capybara to take css as default selector
    Capybara.default_selector = :xpath
    # checks for the presence of the input tag
    expect(page).to have_selector("//input")

    # checks for input tag with value
    expect(page).to have_selector("//input", :value =>"Post Title")

    # checks for presence of a input field named FirstName in a form
    expect(page).to have_field("FirstName")

    expect(page).to have_field("FirstName", :value => "Rambo")
    expect(page).to have_field("FirstName", :with => "Rambo")

    expect(page).to have_link("Foo")
    expect(page).to have_link("Foo", :href=>"googl.com")
    expect(page).to have_no_link("Foo", :href=>"google.com")
    ```