Merge commit 'danbarry/master'

This commit is contained in:
Bryan Helmkamp 2008-07-25 18:56:57 -04:00
commit 4fe320aac6
5 changed files with 66 additions and 20 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 matches_alt?(alt) def matches_alt?(alt)
@ -69,25 +69,28 @@ module Webrat
@element["name"] @element["name"]
end end
def label def labels
return nil if label_element.nil? @labels ||= label_elements.map { |element| Label.new(self, element) }
@label ||= Label.new(self, label_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

@ -20,7 +20,8 @@ module Webrat
end end
def matches_text?(link_text) def matches_text?(link_text)
text =~ /#{Regexp.escape(link_text.to_s)}/i matcher = /#{Regexp.escape(link_text.to_s)}/i
text =~ matcher || title =~ matcher
end end
def text def text
@ -33,6 +34,10 @@ module Webrat
authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token} authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}
end end
def title
@element['title']
end
def href def href
@element["href"] @element["href"]
end end

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">

View File

@ -162,6 +162,14 @@ describe "clicks_link" do
@session.clicks_link "Link text" @session.clicks_link "Link text"
end end
it "should work with anchor titles" do
@session.response_body = <<-EOS
<a href="/page" title="Link title">Link text</a>
EOS
@session.expects(:get).with("/page", {})
@session.clicks_link "Link title"
end
it "should match the first matching link" do it "should match the first matching link" do
@session.response_body = <<-EOS @session.response_body = <<-EOS
<a href="/page1">Link text</a> <a href="/page1">Link text</a>

View File

@ -84,6 +84,19 @@ describe "fills_in" do
@session.clicks_button @session.clicks_button
end end
it "should support unlabelled inputs" do # regression test
@session.response_body = <<-EOS
<form method="post" action="/login">
<input id="user_name" name="user[name]" value="test person" type="text" />
<label for="user_email">email</label>
<input id="user_email" name="user[email]" value="test@example.com" type="text" />
<input id="user_address" name="user[address]" value="123 some street" type="text" />
</form>
EOS
lambda { @session.fills_in "email", :with => "value" }.should_not raise_error
end
it "should anchor label matches to start of label" do it "should anchor label matches to start of label" do
@session.response_body = <<-EOS @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">