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
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 matches_alt?(alt)
@ -69,25 +69,28 @@ module Webrat
@element["name"]
end
def label
return nil if label_element.nil?
@label ||= Label.new(self, label_element)
def labels
@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

@ -20,7 +20,8 @@ module Webrat
end
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
def text
@ -33,6 +34,10 @@ module Webrat
authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}
end
def title
@element['title']
end
def href
@element["href"]
end

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

View File

@ -162,6 +162,14 @@ describe "clicks_link" do
@session.clicks_link "Link text"
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
@session.response_body = <<-EOS
<a href="/page1">Link text</a>

View File

@ -84,6 +84,19 @@ describe "fills_in" do
@session.clicks_button
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
@session.response_body = <<-EOS
<form method="post" action="/login">