Show Menu
Cheatography

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!

Navigation & Windows

visit('url')
page.d­riv­er.b­ro­wse­r.s­wit­ch_­to.w­indow(window­_handle)
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­tton('text, id, title, or value')
click_­link('link­ id, link title or *link text')
click_on('link, 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_in('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')
uncheck('name, id or text of checkbox')
choose('radio button name, id or text')
listbo­x-e­x.n­ati­ve.s­en­dke­ys(­:ar­row­_down)
attach­_file('name, id or text', 'file/­nam­e.png')

Scoping

within­('­css­_l­o­c­at­or')
within­­(:­x­path, 'xpat­h­_l­­oca­tor')
within­­_f­r­a­me­('name or id')
within­­_t­able('name or id')
within­­_f­iel­dset('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_id(id)
locate­("//­*[@­id=­'ov­erl­ay'­"­).f­ind­("//­h1").click
all('a­').each { |a| a[:href] }
expect­(page or location).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­cript('
4 + 4
');

Debugging

save_a­nd_­ope­n_page
also, use
bindin­g.pry
       
 

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

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Cypressio Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet