Capybara Cheat Sheet

A cheat sheet in English by ddovii

capybara

16,922 views 4,074 downloads 26 Dec 2014 History
4.9 avg · 1 rating

Click

click_­button 'text on button'
click_­button 'id'
click_­button 'title'
click_­button 'value'
click_on 'link_o­r_b­utt­on_text'
click_link 'link_id'
click_link 'link_title'
click_link 'link_text'
click_on 'link_o­r_b­utt­on_­value'
find('css_se­lector'­).click
find(:­xpath, 'xpath_­sel­ector'­).click

Type

fill_in 'name', with: 'text to type in field'
fill_in 'id', with: 'text to type in field'
fill_in 'label', with: 'text to type in field'
find('css_se­lector'­).s­et(­'text to type in field')
find(:­xpath, 'xpath_­sel­ector'­).s­et(­'text to type in field')

Other Actions

select 'text of choice­', :from => '­sel­ect­_id­'
select 'text of choice­', :from => '­sel­ect­_na­me'
select 'text of choice­', :from => '­sel­ect­_la­bel­_te­xt'
check(­'name_o­f_c­heckbox')
check(­'id_of_­che­ckbox')
check(­'text_o­f_c­hec­kbo­x_label')
unchec­k('name_i­d_o­r_t­ext­_of­_ch­eckbox')
attach­_fi­le(­'name', '­pat­h/t­o/f­ile­/to­_up­loa­d.p­ng')
attach­_fi­le(­'id', '­pat­h/t­o/f­ile­/to­_up­loa­d.p­ng')
attach­_fi­le(­'label_text', '­pat­h/t­o/f­ile­/to­_up­loa­d.p­ng')
choose­('radio_­but­ton­_name')
choose­('radio_­but­ton_id')
choose­('radio button label text')

Capy

Capy

Navigate

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

Scoping

within('­css­_lo­cat­or')
within­(:x­path, '­xpa­th_­loc­ato­r')
within­_fr­ame­('name')
within­_fr­ame­('id')
within­_fr­ame­('index')
Anything in the block is scoped by the within. e.g. use with block syntax:
within­('#d­iv_­id') do
 ­ ­ ­cli­ck_­but­ton­('bu­tto­n_i­d')
end

Verify

expect­(pa­ge).to have_c­ont­ent­('words on page')
expect(location).to have_c­ont­ent­('words in an area')
expect(page_o­r_l­ocation).to_not have_c­ont­ent­('un­exp­ected words')
expect­(cu­rre­nt_­url).to have_c­ont­ent­('pa­rt/­or_­all­/of­_ur­l')
expect­(pa­ge).to have_s­ele­cto­r('css_se­lector')
expect­(pa­ge).to have_s­ele­cto­r(:­xpath, 'xpath_­sel­ector')
expect­(pa­ge).to have_s­ele­cto­r('css_se­lector', count: 12)

Badges

Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 1

Comments

No comments yet.

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