Capybara with RSpec Cheat Sheet

A cheat sheet in English by Corey

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

16,876 views 3,223 downloads 25 Apr 2016 History
3.7 avg · 0 ratings

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­tto­n('text, id, title, or value')
click_­lin­k('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: '­Tex­t').f­ir­st.n­at­ive­).c­lic­k.p­erform
*also see Querying

Forms, Data and Typing

fill_i­n('name, id or text', with: '­val­ue')
find('c­ss_­sel­ect­or').se­t('v­alu­e')
find(:­xpath, '­xpa­th_­sel­ect­or').se­t('v­alu­e')
select­('te­xt', from: 'select name, id or text ')
check(­'name, id or text of checkb­ox')
unchec­k('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­_fi­le(­'name, id or text', '­fil­e/n­ame.pn­g')

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­abl­e('name or id')
within­­_f­iel­dse­t('name or id')

Querying

page.h­as_­xpa­th?­('//­tab­le/­tr')
page.h­as_­css­?('table tr.foo­')
page.h­as_­con­ten­t?(­'­Tex­t')
page.s­hould have_x­pat­h('/­/ta­ble­/tr­')
page.s­hould have_c­ss(­'­table tr.foo­')
page.s­hould have_c­ont­ent­('Te­xt')
page.s­hould have_n­o_c­ont­ent­('Te­xt')
find_f­iel­d('T­ext­'­).value
find_l­ink­('Te­xt').vi­sible?
find_b­utt­on(­'­Sen­d').c­lick
find('/­/ta­ble­/tr­'­).click
find_b­y_id(id)
locate­("//­*[@­id=­'­ove­rla­y'").f­in­d("/­/h1­"­).click
all('a').each { |a| a[:href] }
expect­(page or location).to have_c­­on­t­e­nt­­('Te­xt')
expect­­(p­a­g­e).n­ot_to have_c­­on­t­e­nt­­('Te­xt')
expect­(pa­ge).to have_c­ss(­'­a', text: '­Tex­t', visible: true)

Movement, Scrolling and Window Size

page.e­xec­ute­_sc­rip­t('w­ind­ow.s­cr­oll­By(­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? '­tex­t'
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 bindin­g.pry

Badges

Nice Cheat Sheet Good Cheat Sheet

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.