Skip to content

Instantly share code, notes, and snippets.

@zhengjia
Created June 7, 2010 01:35
Show Gist options
  • Save zhengjia/428105 to your computer and use it in GitHub Desktop.
Save zhengjia/428105 to your computer and use it in GitHub Desktop.
capybara cheat sheet
=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
@pmaderamitlasf1
Copy link

I am unable to hit "enter" key , any solution?

@gustavo-freitas
Copy link

This is simple and great. Thanks!

@jumbosushi
Copy link

I'm always coming back to this. Thanks for a great list!

@ElizabethCobian
Copy link

Excellent! Thanks :)

@itumoraes
Copy link

Awesome!

Copy link

ghost commented Oct 23, 2017

Thanks for this great gist 👍

@Pomeha
Copy link

Pomeha commented Nov 27, 2017

Great! 👍

@AndrewFReda
Copy link

Awesome, thanks! (FYI, this gist was used by LaunchAcademy here)

@vishakhapanat
Copy link

vishakhapanat commented Jan 7, 2018

When i use find(<name_of_object>).click, I get following error
Unable to find css "" (Capybara::ElementNotFound)
Can someone please help?

@leylaKapi
Copy link

@vishakhapanat Did you find a solution for your error?

@igbanam
Copy link

igbanam commented Mar 14, 2018

To note, things like have_css, and have_xpath also have some optional arguments which help better querying. For instance have_css(".widget", maximum: 2)

@Pomeha
Copy link

Pomeha commented May 23, 2018

👍 👍 👍

@singh08avinash
Copy link

singh08avinash commented Jun 4, 2018

Hiii
I want to use xpath with select method in cucumber rails so how can i use xpath with select method?

@aashishbq
Copy link

aashishbq commented Jul 11, 2018

How do you wait for page to load in webrat?

@srghma
Copy link

srghma commented Jul 13, 2018

To match that select with label 'Country' have an option with text 'France' selected

expect(page).to have_select('Country', selected: 'France')

@philihp
Copy link

philihp commented Aug 18, 2018

👍

@glabrego
Copy link

TY

@arknamal
Copy link

Thanks a lot. Helpful

@FrozenIce0617
Copy link

FrozenIce0617 commented May 10, 2019

Amazing article!
Very helpful!

@davidlegare
Copy link

Is this up to date in July 2019?

@Azzawie
Copy link

Azzawie commented Nov 22, 2019

👍

@maryline-saladin
Copy link

Useful thanks !
Could someone give a little bit more info about tab handling ? opening new tab, switching, closing please ?
Thanks a lot !

@renanregis
Copy link

thanks!

@vidurpunj
Copy link

thanks

@NathenGD22
Copy link

mmmm capy

@recurator
Copy link

save_and_open_screenshot

@vineethvijay7
Copy link

https://devhints.io/capybara pretty much covers all of the available capybara actions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment