Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rspec Rails cheatsheet (include capybara matchers)
#Model
@user.should have(1).error_on(:username) # Checks whether there is an error in username
@user.errors[:username].should include("can't be blank") # check for the error message
#Rendering
response.should render_template(:index)
#Redirecting
response.should redirect_to(movies_path)
#Capybara Matchers
response.body.should have_content("Hello world")
response.body.should have_no_content("Hello world")
response.body.should have_css("input#movie_title")
response.body.should have_css("input#movie_title", :value => "Twelve Angry Men")
response.body.should have_css("input", :count => 3) #True if there are 3 input tags in response
response.body.should have_css("input", :maximum => 3) # True if there or fewer or equal to 3 input tags
response.body.should have_css("input", :minimum => 3) # True if there are minimum of 3 input tags
response.body.should have_css("input", :between => 1..3) # True if there 1 to 3 input tags
response.body.should have_css("p a", :text => "hello") # True if there is a anchor tag with text hello
response.body.should have_css("p a", :text => /[hH]ello(.+)/i)
# True if there is a anchor tag with text matching regex
response.body.should have_xpath("//a")
response.body.should have_xpath("//a",:href => "google.com")
response.body.should have_xpath("//a[@href => 'google.com']")
response.body.should have_xpath("//a[contains(.,'some string')]")
response.body.should 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
response.body.should have_selector(:xpath, "//p/h1")
response.body.should have_selector(:css, "p a#movie_edit_path")
# For making capybara to take css as default selector
Capybara.default_selector = :css
response.body.should have_selector("input") #checks for the presence of the input tag
response.body.should have_selector("input", :value =>"Twelve Angry Men") # checks for input tag with value
response.body.should have_no_selector("input")
# For making capybara to take css as default selector
Capybara.default_selector = :xpath
response.body.should have_selector("//input") #checks for the presence of the input tag
response.body.should have_selector("//input", :value =>"Twelve Angry Men") # checks for input tag with value
# To access elements inside form
response.body.should have_field("FirstName") # checks for presence of a input field named FirstName in a form
response.body.should have_field("FirstName", :value => "Rambo")
response.body.should have_field("FirstName", :with => "Rambo")
response.body.should have_link("Foo")
response.body.should have_link("Foo", :href=>"googl.com")
response.body.should have_no_link("Foo", :href=>"google.com")
@pawelkrol

This comment has been minimized.

Show comment
Hide comment
@pawelkrol

pawelkrol Jul 2, 2013

Hash rocket is not a valid XPath symbol (it leads to "Invalid expression" exception). Please use an "equals" sign: it should be "//a[@href = 'google.com']" instead of "//a[@href => 'google.com']".

pawelkrol commented Jul 2, 2013

Hash rocket is not a valid XPath symbol (it leads to "Invalid expression" exception). Please use an "equals" sign: it should be "//a[@href = 'google.com']" instead of "//a[@href => 'google.com']".

@islador

This comment has been minimized.

Show comment
Hide comment
@islador

islador Sep 6, 2013

This is a life saver, can I give you Karma or something?

islador commented Sep 6, 2013

This is a life saver, can I give you Karma or something?

@gigiJackson

This comment has been minimized.

Show comment
Hide comment
@gigiJackson

gigiJackson Sep 18, 2013

This is useful, thanks!

However, regarding response.body.should have_css("input#movie_title", :value => "Twelve Angry Men"), :value is not actually a valid key for have_css, and :text doesn't actually match against the value attribute of input fields I've found.

You can either do something like have_css("input#movie_title[value=\"Twelve Angry Men\"]"), or user the have_field and :with key.

gigiJackson commented Sep 18, 2013

This is useful, thanks!

However, regarding response.body.should have_css("input#movie_title", :value => "Twelve Angry Men"), :value is not actually a valid key for have_css, and :text doesn't actually match against the value attribute of input fields I've found.

You can either do something like have_css("input#movie_title[value=\"Twelve Angry Men\"]"), or user the have_field and :with key.

@xanhast

This comment has been minimized.

Show comment
Hide comment
@xanhast

xanhast Nov 20, 2013

Is there a difference between should have_no_link("Foo", :href=>"google.com") and should_not have_link("Foo", :href=>"google.com")?

xanhast commented Nov 20, 2013

Is there a difference between should have_no_link("Foo", :href=>"google.com") and should_not have_link("Foo", :href=>"google.com")?

@nerdinand

This comment has been minimized.

Show comment
Hide comment
@nerdinand

nerdinand Dec 4, 2013

Thanks a lot for this! I have corrected a few typos in my fork of this gist. Maybe you want to incorporate them: https://gist.github.com/nerdinand/7787825

nerdinand commented Dec 4, 2013

Thanks a lot for this! I have corrected a few typos in my fork of this gist. Maybe you want to incorporate them: https://gist.github.com/nerdinand/7787825

@Vadorequest

This comment has been minimized.

Show comment
Hide comment
@Vadorequest

Vadorequest Jan 29, 2014

Really useful, thanks a lot!

Vadorequest commented Jan 29, 2014

Really useful, thanks a lot!

@JasonY1

This comment has been minimized.

Show comment
Hide comment
@JasonY1

JasonY1 Apr 16, 2014

Thank you for making this!

JasonY1 commented Apr 16, 2014

Thank you for making this!

@rmswimkktt

This comment has been minimized.

Show comment
Hide comment
@rmswimkktt

rmswimkktt Aug 4, 2014

Nice cheetsheet. I user this sheet.thx.

rmswimkktt commented Aug 4, 2014

Nice cheetsheet. I user this sheet.thx.

@plucb

This comment has been minimized.

Show comment
Hide comment
@plucb

plucb Feb 15, 2015

Is it still up-to-date with current capybara version?

plucb commented Feb 15, 2015

Is it still up-to-date with current capybara version?

@danieljohnmorris

This comment has been minimized.

Show comment
Hide comment
@danieljohnmorris

danieljohnmorris Mar 9, 2015

This works but using page.should instead of response.body.should

danieljohnmorris commented Mar 9, 2015

This works but using page.should instead of response.body.should

@AlexanderFisenko

This comment has been minimized.

Show comment
Hide comment
@AlexanderFisenko

AlexanderFisenko Mar 25, 2015

You'd better use the new :expect syntax like expect(page).to have_

AlexanderFisenko commented Mar 25, 2015

You'd better use the new :expect syntax like expect(page).to have_

@bunnymatic

This comment has been minimized.

Show comment
Hide comment
@bunnymatic

bunnymatic Jul 31, 2015

With capybara 2.4.4, if you want to find a field with a value, use the following

  expect(page).to have_field("My Field", with: "the value in the field")

bunnymatic commented Jul 31, 2015

With capybara 2.4.4, if you want to find a field with a value, use the following

  expect(page).to have_field("My Field", with: "the value in the field")
@kirbygit

This comment has been minimized.

Show comment
Hide comment
@kirbygit

kirbygit Feb 21, 2017

Hey I just started learning RSpec is there a tutorial while using these awesome cheatsheets?

kirbygit commented Feb 21, 2017

Hey I just started learning RSpec is there a tutorial while using these awesome cheatsheets?

@togiberlin

This comment has been minimized.

Show comment
Hide comment
@togiberlin

togiberlin Mar 22, 2017

Hi, this Gist is outdated - it still uses the "should" syntax.
In newer versions of Capybara, it was replaced by the "expect" syntax.

togiberlin commented Mar 22, 2017

Hi, this Gist is outdated - it still uses the "should" syntax.
In newer versions of Capybara, it was replaced by the "expect" syntax.

@redrick

This comment has been minimized.

Show comment
Hide comment
@redrick

redrick Aug 14, 2017

Hey, so with expect syntax and couple things that work different, hopefully helpful :)

https://gist.github.com/redrick/01a0ca575a99f264f1d8ab6a4c93cff0

(I was working from nerdinand fork)

redrick commented Aug 14, 2017

Hey, so with expect syntax and couple things that work different, hopefully helpful :)

https://gist.github.com/redrick/01a0ca575a99f264f1d8ab6a4c93cff0

(I was working from nerdinand fork)

@brookjordan

This comment has been minimized.

Show comment
Hide comment
@brookjordan

brookjordan May 24, 2018

Line 44 should be:
# To passxpathas the default selector type

brookjordan commented May 24, 2018

Line 44 should be:
# To passxpathas the default selector type

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