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 :)

Get Adobe Flash playerPlugin by wpburn.com wordpress themes