Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
capybara cheat sheet
=Clicking links and buttons=
click_link('Link Text')
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')
within("//li[@id='employee']") do
fill_in 'Name', :with => 'Jimmy'
within(:css, "li#employee") do
fill_in 'Name', :with => 'Jimmy'
within_fieldset('Employee') do
fill_in 'Name', :with => 'Jimmy'
within_table('Employee') do
fill_in 'Name', :with => 'Jimmy'
page.should have_xpath('//table/tr')
page.should have_css('table')
page.should have_content('foo')
page.should have_no_content('foo')
find_field('First Name').value
all('a').each { |a| a[:href] }
result = page.evaluate_script('4 + 4');
=Asynchronous JavaScript=
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
Copy link

pdxmike commented Jan 22, 2017

This is great. Thank you!

Copy link

smoyte commented Feb 17, 2017

Copy link

ckib16 commented Feb 24, 2017

Nice @hooverlunch, thanks!

Copy link

jepjep70 commented Feb 25, 2017

Guys good day. Can you help me on how can i create a test report when i am using capybara-cucumber in my automation. What gem should i need to install and how should i invoke it. Thank you in advance.

Copy link

cooljl31 commented Mar 20, 2017


Copy link

pmaderamitlasf1 commented May 12, 2017

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

Copy link

gustavo-freitas commented May 30, 2017

This is simple and great. Thanks!

Copy link

jumbosushi commented Jul 12, 2017

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

Copy link

ElizabethCobian commented Aug 24, 2017

Excellent! Thanks :)

Copy link

itumoraes commented Sep 20, 2017


Copy link

ghost commented Oct 23, 2017

Thanks for this great gist 👍

Copy link

Pomeha commented Nov 27, 2017

Great! 👍

Copy link

AndrewFReda commented Dec 18, 2017

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

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?

Copy link

leylaKapi commented Mar 1, 2018

@vishakhapanat Did you find a solution for your error?

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)

Copy link

Pomeha commented May 23, 2018

👍 👍 👍

Copy link

singh08avinash commented Jun 4, 2018

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

Copy link

aashishbq commented Jul 11, 2018

How do you wait for page to load in webrat?

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

Copy link

philihp commented Aug 18, 2018


Copy link

glabrego commented Feb 27, 2019


Copy link

arknamal commented Apr 24, 2019

Thanks a lot. Helpful

Copy link

FrozenIce0617 commented May 10, 2019

Amazing article!
Very helpful!

Copy link

davidlegare commented Jul 26, 2019

Is this up to date in July 2019?

Copy link

Azzawie commented Nov 22, 2019


Copy link

maryline-saladin commented Mar 14, 2020

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

Copy link

renanregis commented Apr 20, 2020


Copy link

vidurpunj commented May 21, 2020


Copy link

NathenGD22 commented Mar 3, 2022

mmmm capy

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