Category: BDD

Dec32009

Webrat Steps extension (Be more DRY)

Comments Off

Recently when I was working with Cucumber and Webrat, I found default webrat definition very interesting. Even in some case I didn’t even had to write a single step definition. But while proceeding I found that something is missing from webrat definition. So I decided to extend it. And come up with some methods defined as below.

To use them just download this file and put it under features/step_definitions/

You can copy below code clipboard

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#Created By Bagwan Pankaj (RailsJaipur) “http://railsjaipur.in/”
#This file is an extension of default webrat steps. some of the webrat steps have been extended
#in this file so that one need not to write unrequired steps.
#Extends default
#USE: When I follow //
When /^I follow \/([^\/]*)\/$/ do |regexp_link|
  click_link(regexp_link)
end
 
#USE: When I follow // within ""
When /^I follow \/([^\/]*)\/ within "([^\"]*)"$/ do |regexp_link, parent|
  click_link_within(parent,regexp_link)
end
 
#USE: Then It should not include tag ""
Then /^It should not include tag "([^\"]*)"$/ do |tag|
  assert_have_no_selector(tag, :count => 0)
end
 
#USE: Then It should not include tag "" within ""
Then /^It should not include tag "([^\"]*)" within "([^\"]*)"$/ do |tag_match, parent|
  within parent do |scope|
    scope.should_not have_selector(tag_match, :count => 0)
  end
end
 
#USE: Then It should not include selector ""
Then /^It should not include selector "([^\"]*)"$/ do |tag|
  Then %{It should not include tag \"#{tag}\"}
end
 
#USE: Then It should not include selector "" within ""
Then /^It should not include selector "([^\"]*)" within "([^\"]*)"$/ do |tag_match, parent|
  Then %{It should not include tag \"#{tag_match}\" within \"#{parent.to_s}\"}
end
 
#USE: Then It should include tag ""
Then /^It should include tag "([^\"]*)"$/ do |tag|
  assert_have_selector(tag)
end
 
#USE: Then It should include tag "" within ""
Then /^It should include tag "([^\"]*)" within "([^\"]*)"$/ do |tag_match, parent|
  within parent do |scope|
    scope.should have_selector(tag_match)
  end
end
 
#USE: Then It should include selector ""
Then /^It should include selector "([^\"]*)"$/ do |tag|
  Then %{It should include tag \"#{tag}\"}
end
 
#USE: Then It should include selector "" within ""
Then /^It should include selector "([^\"]*)" within "([^\"]*)"$/ do |tag_match, parent|
  Then %{It should include tag \"#{tag_match}\" within \"#{parent.to_s}\"}
end
 
#USE: When I check // within “”
When /^I check \/([^\/]*)\/ within "([^\"]*)"$/ do |field, parent|
  within parent do |scope|
    regexp = Regexp.new(field)
    scope.check(regexp)
  end
end
 
#USE: When /^I check “([^\"]*)” within “”
When /^I check "([^\"]*)" within "([^\"]*)"$/ do |field, parent|
  within parent do |scope|
    scope.check(field)
  end
end
 
#USE: When /^(?:|I )check //
When /^(?:|I )check \/([^\/]*)\/$/ do |field|
  regexp = Regexp.new(field)
  check(regexp)
end
 
#USE: I should be able to see  "
"
Then /^I should be able to see (\d+) "([^\"]*)"$/ do |count,selector|
  assert_have_selector(selector, :count => count)
end
 
#USE: I should be able to see  "
" within "
"
Then /^I should be able to see (\d+) "([^\"]*)" within "([^\"]*)"$/ do |count,selector, parent|
  within parent do  |scope|
    scope.should have_selector(selector, :count => count)
  end
end
 
When /^(?:|I )choose "([^\"]*)" within "([^\"]*)"$/ do |field,parent|
  within parent do |scope|
    scope.choose(field)
  end
end
 
When /^(?:|I )choose \/([^\/]*)\/ within "([^\"]*)"$/ do |field,parent|
  within parent do |scope|
    regexp = Regexp.new(field)
    scope.choose(regexp)
  end
end

Now you can use above listed methods easily and I assure you that if you use them properly you do not have to write a single step definition.

Their uses can be found against USE in the downloaded file.

“Use the Rails the way Rails is”

Happy Boarding :)

Nov232009

Cucumber + Webrat CheatSheet

Comments Off

Webrat – Ruby Acceptance Testing for Web applications

Download PDF format

Before starting here are some useful links

	http://gitrdoc.com/brynary/webrat
	http://groups.google.com/group/webrat
	http://webrat.lighthouseapp.com/
	http://github.com/brynary/webrat
	#webrat on Freenode

Simulating browser events

1. Visiting a URL

GET a URL, following any redirects, and making sure final page is successful

1
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

2. Visiting Link

Links can be looked up by text, title, or id

for example To match… Click here to join!

we can write down like

1
2
3
4
5
6
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

3. Filling Up a form

Note:

  • All fields can be looked up by ID, name, or label inner text
  • Text fields, password fields, and text areas can be filled in using fill_in

1
2
3
4
5
6
fill_in "user_email", :with => "test@example.com"                     # exact id
fill_in /user.*email/, :with => "test@example.com"                   # regexp id
fill_in "user[email]", :with => "test@example.com"               # substring name
fill_in /user[.*mail.*]/, :with => "test@example.com"          # substring name
fill_in "<label for="user[email]">Enter your Email</label>", :with =&gt; "test@example.com"    # substring label text
fill_in /enter your email/i, :with =&gt; "test@example.com"    # regexp label text

Note: Hidden fields can also be set using

1
set_hidden_field 'user[l337_test]', :to =&gt; 'true'

4. Selecting from Drop Down

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.

1
2
3
4
select "Free account"
select "Free account", :from =&gt; "Account Types"
select "Free account", :from =&gt; "user[account_type]"
select "Free account", :from =&gt; "user_account_type"

5. Check Boxes

Check boxes can be ‘checked’ and ‘unchecked’

1
2
check 'Remember me'
uncheck 'Remember me'

6. Radio Buttons

Radio buttons can be also choosen, using the usual label text, name, or id.

1
choose "Yes"

7. Buttons

1
click_button "Register"

Assertions

check for text in the body of html tags
can be a string or regexp

1
2
3
assert_contain("Successfully created")
assert_contain(/trogdor/i)
assert_not_contain("peasants")

check for a css3 selector

1
2
assert_have_selector 'div.pagination'
assert_have_no_selector 'form input#name'

Note: Assertion does work not inside block

Matchers

check for text in the body of html tags
an be a string or regexp

1
2
3
response.should contain("BURNINATOR")
response.should contain(/trogdor/i)
response.should_not contain("peasants")

check for a css3 selector

1
2
response.should have_selector('div.pagination')
response.should_not have_selector('form input#name')

Targetted actions/matchers

1
2
3
within 'div.pagination' do |scope|
	scope.click_link "1"
end
1
2
3
4
within '.shows' do |scope|
	scope.should contain("NFL")
	# unfortunately, assertions don't support this currently
end
Get Adobe Flash playerPlugin by wpburn.com wordpress themes