radio buttons can have multiple labels

This commit is contained in:
Dan Barry 2008-07-08 12:31:44 -05:00
parent 17f4dba965
commit a9430aa2ce
2 changed files with 40 additions and 19 deletions

View File

@ -25,8 +25,8 @@ module Webrat
end end
def label_text def label_text
return nil unless label return nil if labels.empty?
label.text labels.first.text
end end
def matches_id?(id) def matches_id?(id)
@ -38,8 +38,8 @@ module Webrat
end end
def matches_label?(label_text) def matches_label?(label_text)
return false unless label return false if labels.empty?
label.matches_text?(label_text) labels.any? { |label| label.matches_text?(label_text) }
end end
def to_param def to_param
@ -65,25 +65,29 @@ module Webrat
@element["name"] @element["name"]
end end
def label def labels
return nil if label_element.nil? return nil if label_elements.empty?
@label ||= Label.new(self, label_element) @labels ||= label_elements.map { |element| Label.new(self, element) }
end end
def label_element def label_elements
@label_element ||= begin return @label_elements unless @label_elements.nil?
parent = @element.parent @label_elements = []
while parent.respond_to?(:parent)
return parent if parent.name == "label" parent = @element.parent
parent = parent.parent while parent.respond_to?(:parent)
end if parent.name == 'label'
@label_elements.push parent
if id.blank? break
nil
else
@form.element.at("label[@for=#{id}]")
end end
parent = parent.parent
end end
unless id.blank?
@label_elements += @form.element / "label[@for=#{id}]"
end
@label_elements
end end
def default_value def default_value

View File

@ -39,6 +39,23 @@ describe "chooses" do
@session.clicks_button @session.clicks_button
end end
it "should use any of the button's labels" do
@session.response_body = <<-EOS
<form method="get" action="/login">
<label for="user_gender_male"><img src="male.jpg" /></label>
<input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
<label for="user_gender_female"><img src="female.jpg" /></label>
<input id="user_gender_female" name="user[gender]" type="radio" value="F" />
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
EOS
@session.expects(:get).with("/login", "user" => {"gender" => "M"})
@session.chooses "Male"
@session.clicks_button
end
it "should only submit last chosen value" do it "should only submit last chosen value" do
@session.response_body = <<-EOS @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">