public
Description: Webrat - Ruby Acceptance Testing for Web applications
Homepage: http://gitrdoc.com/brynary/webrat/tree/master/
Clone URL: git://github.com/brynary/webrat.git
Raise error when trying to interact with a disabled form element
lukemelia (author)
Wed Oct 01 17:22:23 -0700 2008
commit  33fdf33c899c731cbae885547264c3966f9a4b82
tree    cff7855ad76ea5dabcde8b840c0bdf68af4314b1
parent  7c46a6fed0fe1b5ff4319515b6eeb60464133ce5
...
8
9
10
 
11
12
13
...
8
9
10
11
12
13
14
0
@@ -8,6 +8,7 @@
0
   * Support relative links, including href="?foo=bar" (Patch from Kyle Hargraves)
0
   * Separated Rails-specific code from the Webrat core to make it easier to use Webrat with other environments
0
   * Alias visits as visit, clicks_link as click_link, etc. for better readability
0
+  * Raise error when trying to interact with a disabled form element (Luke Melia)
0
   
0
 * Minor enhancements
0
 
...
45
46
47
 
 
 
 
48
 
 
 
 
49
50
51
...
144
145
146
 
147
148
149
...
182
183
184
 
185
186
187
188
 
189
190
191
...
212
213
214
 
215
216
217
...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
...
152
153
154
155
156
157
158
...
191
192
193
194
195
196
197
198
199
200
201
202
...
223
224
225
226
227
228
229
0
@@ -45,7 +45,15 @@ module Webrat
0
     def matches_alt?(alt)
0
       @element["alt"] =~ /^\W*#{Regexp.escape(alt.to_s)}/i
0
     end
0
+
0
+    def disabled?
0
+      !@element["disabled"].nil? && @element["disabled"] != 'false'
0
+    end
0
     
0
+    def raise_error_if_disabled
0
+      raise "Cannot interact with disabled form element (#{self})" if disabled?
0
+    end
0
+        
0
     def to_param
0
       value = @value.to_s.gsub('&', '%26')
0
       param_parser.parse_query_parameters("#{name}=#{value}")
0
@@ -144,6 +152,7 @@ module Webrat
0
     end
0
 
0
     def click
0
+      raise_error_if_disabled
0
       set(@element["value"]) unless @element["name"].blank?
0
       @form.submit
0
     end
0
@@ -182,10 +191,12 @@ module Webrat
0
     end
0
 
0
     def check
0
+      raise_error_if_disabled
0
       set(@element["value"] || "on")
0
     end
0
 
0
     def uncheck
0
+      raise_error_if_disabled
0
       set(nil)
0
     end
0
 
0
@@ -212,6 +223,7 @@ module Webrat
0
     end
0
     
0
     def choose
0
+      raise_error_if_disabled
0
       other_options.each do |option|
0
         option.set(nil)
0
       end
...
22
23
24
25
 
 
 
26
27
28
...
22
23
24
 
25
26
27
28
29
30
0
@@ -22,7 +22,9 @@ module Webrat
0
     # <tt>field</tt> can be either the value of a name attribute (i.e. <tt>user[email]</tt>)
0
     # or the text inside a <tt><label></tt> element that points at the <tt><input></tt> field.
0
     def fills_in(id_or_name_or_label, options = {})
0
-      find_field(id_or_name_or_label, TextField, TextareaField, PasswordField).set(options[:with])
0
+      field = find_field(id_or_name_or_label, TextField, TextareaField, PasswordField)
0
+      field.raise_error_if_disabled
0
+      field.set(options[:with])
0
     end
0
 
0
     alias_method :fill_in, :fills_in
...
15
16
17
 
18
19
20
...
15
16
17
18
19
20
21
0
@@ -15,6 +15,7 @@ module Webrat
0
     end
0
     
0
     def choose
0
+      @select.raise_error_if_disabled
0
       @select.set(value)
0
     end
0
     
...
50
51
52
 
 
 
 
 
 
 
 
 
 
53
54
55
...
87
88
89
 
 
 
 
 
 
 
 
 
 
90
91
92
...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
0
@@ -50,6 +50,16 @@ describe "checks" do
0
     @session.clicks_button
0
   end
0
   
0
+  it "should fail if the checkbox is disabled" do
0
+    @session.response_body = <<-EOS
0
+      <form method="post" action="/login">
0
+        <input type="checkbox" name="remember_me" disabled="disabled" />
0
+        <input type="submit" />
0
+      </form>
0
+    EOS
0
+    lambda { @session.checks "remember_me" }.should raise_error
0
+  end
0
+  
0
   it "should result in a custom value being posted" do
0
     @session.response_body = <<-EOS
0
       <form method="post" action="/login">
0
@@ -87,6 +97,16 @@ describe "unchecks" do
0
     lambda { @session.unchecks "remember_me" }.should raise_error
0
   end
0
   
0
+  it "should fail if the checkbox is disabled" do
0
+    @session.response_body = <<-EOS
0
+      <form method="post" action="/login">
0
+        <input type="checkbox" name="remember_me" checked="checked" disabled="disabled" />
0
+        <input type="submit" />
0
+      </form>
0
+    EOS
0
+    lambda { @session.unchecks "remember_me" }.should raise_error
0
+  end
0
+  
0
   it "should uncheck rails style checkboxes" do
0
     @session.response_body = <<-EOS
0
       <form method="get" action="/login">
...
53
54
55
56
 
 
 
 
 
 
 
 
 
 
 
 
57
58
59
...
53
54
55
 
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
0
@@ -53,7 +53,18 @@ describe "chooses" do
0
     @session.chooses "Female"
0
     @session.chooses "Male"
0
     @session.clicks_button
0
-  end  
0
+  end
0
+  
0
+  it "should fail if the radio button is disabled" do
0
+    @session.response_body = <<-EOS
0
+      <form method="post" action="/login">
0
+        <input type="radio" name="first_option" disabled="disabled" />
0
+        <input type="submit" />
0
+      </form>
0
+    EOS
0
+    
0
+    lambda { @session.chooses "first_option" }.should raise_error
0
+  end
0
   
0
   it "should result in the value on being posted if not specified" do
0
     @session.response_body = <<-EOS
...
22
23
24
 
 
 
 
 
 
 
 
 
 
 
25
26
27
...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
0
@@ -22,6 +22,17 @@ describe "clicks_button" do
0
 
0
     lambda { @session.clicks_button }.should raise_error
0
   end
0
+
0
+  
0
+  it "should fail if button is disabled" do
0
+    @session.response_body = <<-EOS
0
+      <form method="get" action="/login">
0
+        <input type="submit" disabled="disabled" />
0
+      </form>
0
+    EOS
0
+
0
+    lambda { @session.clicks_button }.should raise_error
0
+  end
0
   
0
   it "should default to get method" do
0
     @session.response_body = <<-EOS
...
39
40
41
 
 
 
 
 
 
 
 
 
 
 
 
42
43
44
...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
0
@@ -39,6 +39,18 @@ describe "fills_in" do
0
     lambda { @session.fills_in "Email", :with => "foo@example.com" }.should raise_error
0
   end
0
   
0
+  it "should fail if input is disabled" do
0
+    @session.response_body = <<-EOS
0
+      <form method="get" action="/login">
0
+        <label for="user_email">Email</label>
0
+        <input id="user_email" name="user[email]" type="text" disabled="disabled" />
0
+        <input type="submit" />
0
+      </form>
0
+    EOS
0
+    
0
+    lambda { @session.fills_in "Email", :with => "foo@example.com" }.should raise_error
0
+  end
0
+  
0
   it "should allow overriding default form values" do
0
     @session.response_body = <<-EOS
0
       <form method="post" action="/login">
...
35
36
37
 
 
 
 
 
 
 
 
 
 
 
 
38
39
40
...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
0
@@ -35,6 +35,18 @@ describe "selects" do
0
 
0
     lambda { @session.selects "February", :from => "year" }.should raise_error
0
   end
0
+
0
+  
0
+  it "should fail if the select is disabled" do
0
+    @session.response_body = <<-EOS
0
+      <form method="post" action="/login">
0
+        <select name="month" disabled="disabled"><option value="1">January</option></select>
0
+        <input type="submit" />
0
+      </form>
0
+    EOS
0
+
0
+    lambda { @session.selects "January", :from => "month" }.should raise_error
0
+  end
0
   
0
   it "should send value from option" do
0
     @session.response_body = <<-EOS

Comments