Show Menu

Capybara with RSpec Cheat Sheet by

Capybara with RSpec. Content largely taken from ddovii's great cheat sheet and zhengjia's gist. I hope they don't mind!
rspec     selenium     capybara

Navigation & Windows

visit(­'u­rl')
page.d­riv­er.b­ro­wse­r.s­wit­ch_­to.w­in­dow­(w­ind­ow_­han­dle)
my_ori­gin­al_­win­_handle = page.d­riv­er.b­ro­wse­r.w­ind­ow_­han­dle­s.first
my_las­t_w­in_­handle = page.d­riv­er.b­ro­wse­r.w­ind­ow_­han­dle­s.last
page.d­riv­er.b­ro­wse­r.s­wit­ch_­to.a­le­rt.a­ccept
page.d­riv­er.g­o_back

Clicking Links and Buttons

click_­­bu­tto­n('­text, id, title, or value')
click_­lin­k('­link­ id, link title or *link text')
click_­on(­'l­ink, button value, or button text')
page.d­riv­er.b­ro­wse­r.a­cti­on.m­ov­e_t­o(p­age.al­l('p', text: 'Text'­).f­irs­t.n­ati­ve).cl­ick.pe­rform
*also see Querying

Forms, Data and Typing

fill_i­n('­name, id or text', with: 'value')
find('­css­_se­lec­tor­').s­et­('v­alue')
find(:­xpath, 'xpath­_se­lec­tor­').s­et­('v­alue')
select­('t­ext', from: 'select name, id or text ')
check(­'name, id or text of checkbox')
unchec­k('­name, id or text of checkb­ox')
choose­('­radio button name, id or text')
listbo­x-e­x.n­ati­ve.s­en­dke­ys(­:ar­row­_down)
attach­_fi­le(­'name, id or text', 'file/­nam­e.png')

Scoping

within­­('­­c­ss­­_lo­­ca­t­or')
within­­(:­x­path, 'xpat­­h_­l­o­ca­­tor')
within­­_f­r­a­me­­('­name or id')
within­­_t­abl­e('­name or id')
within­­_f­iel­dse­t('­name or id')
 

Querying

page.h­as_­xpa­th?­('/­/ta­ble­/tr')
page.h­as_­css­?('­table tr.foo')
page.h­as_­con­ten­t?(­'Text')
page.s­hould have_x­pat­h('­//t­abl­e/tr')
page.s­hould have_c­ss(­'table tr.foo')
page.s­hould have_c­ont­ent­('T­ext')
page.s­hould have_n­o_c­ont­ent­('T­ext')
find_f­iel­d('­Tex­t').value
find_l­ink­('T­ext­').v­is­ible?
find_b­utt­on(­'Se­nd'­).click
find('­//t­abl­e/t­r').click
find_b­y_i­d(id)
locate­("//­*[@­id=­'ov­erl­ay'­"­).f­ind­("//­h1").click
all('a­').each { |a| a[:href] }
expect­­(page or locati­on).to have_c­­on­t­e­nt­­('T­ext')
expect­­(p­a­g­e).n­ot_to have_c­­on­t­e­nt­­('T­ext')
expect­(pa­ge).to have_c­ss('a', text: 'Text', visible: true)

Movement, Scrolling and Window Size

page.e­xec­ute­_sc­rip­t('­win­dow.sc­rol­lBy­(0,­-200)')
page.d­riv­er.b­ro­wse­r.a­cti­on.m­ov­e_t­o(f­ind­_bu­tto­n(t­ext­).n­ati­ve).pe­rform
page.d­riv­er.b­ro­wse­r.m­ana­ge.w­in­dow.ma­ximize
page.d­riv­er.b­ro­wse­r.m­ana­ge.w­in­dow.re­siz­e_t­o(x,y)

Page Source

expect page.h­tml.do­wnc­ase.in­clude? 'text'
page.s­tat­us_­cod­e.s­hould be 200 (not supported by Selenium driver)
page.d­riv­er.b­ro­wse­r.m­ana­ge.a­ll­_co­oki­es.each {|cookie|}

Scripting

result = page.e­val­uat­e_s­cri­pt('4 + 4');

Debugging

save_a­nd_­ope­n_page
also, use bindi­ng.pry

Download the Capybara with RSpec Cheat Sheet

1 Page
//media.cheatography.com/storage/thumb/corey_capybara-with-rspec.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          rSpec Core API and Mock API Cheat Sheet
          Selenium WebDriver Cheat Sheet Cheat Sheet