Skip to content

Instantly share code, notes, and snippets.

@zhengjia
Created June 7, 2010 01:38
Show Gist options
  • Save zhengjia/428108 to your computer and use it in GitHub Desktop.
Save zhengjia/428108 to your computer and use it in GitHub Desktop.
webrat cheatsheet
== Simulating browser events
# GET a URL, following any redirects, and making sure final page is successful
visit "/some/url"
# In general, elements can be located by their inner text, their 'title'
attribute, their 'name' attribute, and their 'id' attribute.
# They can be selected using a String, which is converted to an escaped Regexp
effectively making it a substring match, or using a Regexp.
# An exception is that using Strings for ids are compared exactly (using ==)
rather than converted to a Regexp
# If the element you are trying to look up does not exist, an error occurs
# Links can be looked up by text, title, or id
# To match... <a href="/signup" title="Sign up" id="signup_link">Click here to
join!</a>
click_link "Click here to join!" # substring text
click_link /join/i # regexp text
click_link "Sign up" # substring title
click_link /sign.*up/i # regexp title
click_link /signup.*link/i # regexp id
click_link "signup_link" # exact id
# All fields can be looked up by ID, name, or label inner text
# text fields, password fields, and text areas can be filled in
# <label for="user[email]">Enter your Email</label><input type="text"
name="user[email]" id="user_name">
fill_in "user_email", :with => "good@example" # exact id
fill_in /user.*email/, :with => "good@example" # regexp id
fill_in "user[email]", :with => "good@example" # substring name
fill_in /user[.*mail.*]/, :with => "good@example" # substring name
fill_in "Email", :with => "[email protected]" # substring label text
fill_in /enter your email/i, :with => "[email protected]" # regexp label text
# Hidden fields can be set
set_hidden_field 'user[l337_hax0r]', :to => 'true'
# Select options can be 'selected' by inner text (an exact String or a Regexp
to match). It can optionally be selected from a particular select field, using
the usual id, name, or label text.
select "Free account"
select "Free account", :from => "Account Types"
select "Free account", :from => "user[account_type]"
select "Free account", :from => "user_account_type"
# Check boxes can be 'checked' and 'unchecked'
check 'Remember me'
uncheck 'Remember me'
# Radio buttons can be choosen, using the usual label text, name, or id.
choose "Yes"
click_button "Register"
== Assertions
# check for text in the body of html tags
# can be a string or regexp
assert_contain("BURNINATOR")
assert_contain(/trogdor/i)
assert_not_contain("peasants")
# check for a css3 selector
assert_have_selector 'div.pagination'
assert_have_no_selector 'form input#name'
== Matchers
# check for text in the body of html tags
# can be a string or regexp
# Matchers are verbs used with auxillary verbs should, should_not, etc.
response.should contain("BURNINATOR")
response.should contain(/trogdor/i)
response.should_not contain("peasants")
response.should have_tag("form[action=?]", xxx_path)
# check for a css3 selector
response.should have_selector('div.pagination')
response.should_not have_selector('form input#name')
== Targetted actions/matchers
# selectors syntax is defined here: http://www.w3.org/TR/css3-selectors/
within 'div.pagination' do |scope|
scope.click_link "1"
end
within '.shows' do |scope|
scope.should contain("NFL")
# unfortunately, assertions don't support this currently
end
within 'form[name="search"] select[name="type"]' do |scope|
scope.should have_selector 'option[value="blah"]'
scope.should have_selector 'option[value="gah"]'
scope.should have_selector 'option[value="eep"]'
scope.should have_selector 'input:only-of-type'
scope.should have_selector 'input[name="query"]'
scope.should have_selector 'input[type="text"]'
end
#modify from http://cheat.errtheblog.com/s/webrat/
@aashish
Copy link

aashish commented Jul 11, 2018

How to trigger an js event such as blur, onclick?

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