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

View File

@ -39,6 +39,23 @@ describe "chooses" do
@session.clicks_button
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
@session.response_body = <<-EOS
<form method="get" action="/login">