Raise error when trying to interact with a disabled form element

This commit is contained in:
Luke Melia 2008-10-01 20:22:23 -04:00
parent 7c46a6fed0
commit 33fdf33c89
9 changed files with 84 additions and 2 deletions

View File

@ -8,6 +8,7 @@
* Support relative links, including href="?foo=bar" (Patch from Kyle Hargraves)
* Separated Rails-specific code from the Webrat core to make it easier to use Webrat with other environments
* Alias visits as visit, clicks_link as click_link, etc. for better readability
* Raise error when trying to interact with a disabled form element (Luke Melia)
* Minor enhancements

View File

@ -45,7 +45,15 @@ module Webrat
def matches_alt?(alt)
@element["alt"] =~ /^\W*#{Regexp.escape(alt.to_s)}/i
end
def disabled?
!@element["disabled"].nil? && @element["disabled"] != 'false'
end
def raise_error_if_disabled
raise "Cannot interact with disabled form element (#{self})" if disabled?
end
def to_param
value = @value.to_s.gsub('&', '%26')
param_parser.parse_query_parameters("#{name}=#{value}")
@ -144,6 +152,7 @@ module Webrat
end
def click
raise_error_if_disabled
set(@element["value"]) unless @element["name"].blank?
@form.submit
end
@ -182,10 +191,12 @@ module Webrat
end
def check
raise_error_if_disabled
set(@element["value"] || "on")
end
def uncheck
raise_error_if_disabled
set(nil)
end
@ -212,6 +223,7 @@ module Webrat
end
def choose
raise_error_if_disabled
other_options.each do |option|
option.set(nil)
end

View File

@ -22,7 +22,9 @@ module Webrat
# <tt>field</tt> can be either the value of a name attribute (i.e. <tt>user[email]</tt>)
# or the text inside a <tt><label></tt> element that points at the <tt><input></tt> field.
def fills_in(id_or_name_or_label, options = {})
find_field(id_or_name_or_label, TextField, TextareaField, PasswordField).set(options[:with])
field = find_field(id_or_name_or_label, TextField, TextareaField, PasswordField)
field.raise_error_if_disabled
field.set(options[:with])
end
alias_method :fill_in, :fills_in

View File

@ -15,6 +15,7 @@ module Webrat
end
def choose
@select.raise_error_if_disabled
@select.set(value)
end

View File

@ -50,6 +50,16 @@ describe "checks" do
@session.clicks_button
end
it "should fail if the checkbox is disabled" do
@session.response_body = <<-EOS
<form method="post" action="/login">
<input type="checkbox" name="remember_me" disabled="disabled" />
<input type="submit" />
</form>
EOS
lambda { @session.checks "remember_me" }.should raise_error
end
it "should result in a custom value being posted" do
@session.response_body = <<-EOS
<form method="post" action="/login">
@ -87,6 +97,16 @@ describe "unchecks" do
lambda { @session.unchecks "remember_me" }.should raise_error
end
it "should fail if the checkbox is disabled" do
@session.response_body = <<-EOS
<form method="post" action="/login">
<input type="checkbox" name="remember_me" checked="checked" disabled="disabled" />
<input type="submit" />
</form>
EOS
lambda { @session.unchecks "remember_me" }.should raise_error
end
it "should uncheck rails style checkboxes" do
@session.response_body = <<-EOS
<form method="get" action="/login">

View File

@ -53,7 +53,18 @@ describe "chooses" do
@session.chooses "Female"
@session.chooses "Male"
@session.clicks_button
end
end
it "should fail if the radio button is disabled" do
@session.response_body = <<-EOS
<form method="post" action="/login">
<input type="radio" name="first_option" disabled="disabled" />
<input type="submit" />
</form>
EOS
lambda { @session.chooses "first_option" }.should raise_error
end
it "should result in the value on being posted if not specified" do
@session.response_body = <<-EOS

View File

@ -22,6 +22,17 @@ describe "clicks_button" do
lambda { @session.clicks_button }.should raise_error
end
it "should fail if button is disabled" do
@session.response_body = <<-EOS
<form method="get" action="/login">
<input type="submit" disabled="disabled" />
</form>
EOS
lambda { @session.clicks_button }.should raise_error
end
it "should default to get method" do
@session.response_body = <<-EOS

View File

@ -39,6 +39,18 @@ describe "fills_in" do
lambda { @session.fills_in "Email", :with => "foo@example.com" }.should raise_error
end
it "should fail if input is disabled" do
@session.response_body = <<-EOS
<form method="get" action="/login">
<label for="user_email">Email</label>
<input id="user_email" name="user[email]" type="text" disabled="disabled" />
<input type="submit" />
</form>
EOS
lambda { @session.fills_in "Email", :with => "foo@example.com" }.should raise_error
end
it "should allow overriding default form values" do
@session.response_body = <<-EOS
<form method="post" action="/login">

View File

@ -35,6 +35,18 @@ describe "selects" do
lambda { @session.selects "February", :from => "year" }.should raise_error
end
it "should fail if the select is disabled" do
@session.response_body = <<-EOS
<form method="post" action="/login">
<select name="month" disabled="disabled"><option value="1">January</option></select>
<input type="submit" />
</form>
EOS
lambda { @session.selects "January", :from => "month" }.should raise_error
end
it "should send value from option" do
@session.response_body = <<-EOS