Edge cases for links and buttons

Links with anchors, without hrefs, forms without
action.
This commit is contained in:
Jonas Nicklas 2010-01-11 21:59:22 +01:00
parent a34178859b
commit f874b176da
6 changed files with 75 additions and 16 deletions

View File

@ -38,7 +38,7 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
def click
if tag_name == 'a'
driver.visit(self[:href])
driver.visit(self[:href].to_s)
elsif (tag_name == 'input' or tag_name == 'button') and %w(submit image).include?(type)
Form.new(driver, form).submit(self)
end
@ -100,23 +100,19 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
end
def submit(button)
if post?
driver.submit(node['action'].to_s, params(button))
else
driver.visit(node['action'].to_s, params(button))
end
driver.submit(method, node['action'].to_s, params(button))
end
def multipart?
self[:enctype] == "multipart/form-data"
end
def post?
self[:method] =~ /post/i
private
def method
self[:method] =~ /post/i ? :post : :get
end
private
def merge_param!(params, key, value)
collection = key.sub!(/\[\]$/, '')
if collection
@ -143,21 +139,23 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
end
def visit(path, attributes = {})
return if path.gsub(/^#{current_path}/, '') =~ /^#/
get(path, attributes)
follow_redirects!
cache_body
end
def current_url
request.url
request.url rescue ""
end
def response_headers
response.headers
end
def submit(path, attributes)
post(path, attributes)
def submit(method, path, attributes)
path = current_path if not path or path.empty?
send(method, path, attributes)
follow_redirects!
cache_body
end
@ -166,7 +164,11 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
html.xpath(selector).map { |node| Node.new(self, node) }
end
private
private
def current_path
request.path rescue ""
end
def follow_redirects!
Capybara::WaitUntil.timeout(4) do

View File

@ -56,8 +56,8 @@ module Capybara
end
def link(locator)
xpath = append("//a[@id=#{s(locator)} or contains(.,#{s(locator)}) or contains(@title,#{s(locator)})]")
xpath.prepend("//a[text()=#{s(locator)} or @title=#{s(locator)}]")
xpath = append("//a[@href][@id=#{s(locator)} or contains(.,#{s(locator)}) or contains(@title,#{s(locator)})]")
xpath.prepend("//a[@href][text()=#{s(locator)} or @title=#{s(locator)}]")
end
def button(locator)

View File

@ -166,6 +166,18 @@ module ClickButtonSpec
@session.click_button('Go FAR')
@session.body.should include('You landed')
end
it "should post pack to the same URL when no action given" do
@session.visit('/postback')
@session.click_button('With no action')
@session.body.should include('Postback')
end
it "should post pack to the same URL when blank action given" do
@session.visit('/postback')
@session.click_button('With blank action')
@session.body.should include('Postback')
end
end
end
end

View File

@ -58,6 +58,31 @@ module ClickLinkSpec
@session.click_link('Redirect')
@session.body.should include('You landed')
end
it "should do nothing on anchor links" do
@session.fill_in("test_field", :with => 'blah')
@session.click_link('Anchor')
@session.find_field("test_field").value.should == 'blah'
@session.click_link('Blank Anchor')
@session.find_field("test_field").value.should == 'blah'
end
it "should do nothing on URL+anchor links for the same page" do
@session.fill_in("test_field", :with => 'blah')
@session.click_link('Anchor on same page')
@session.find_field("test_field").value.should == 'blah'
end
it "should follow link on URL+anchor links for a different page" do
@session.click_link('Anchor on different page')
@session.body.should include('Bar')
end
it "raise an error with links with no href" do
running do
@session.click_link('No Href')
end.should raise_error(Capybara::ElementNotFound)
end
end
end
end

13
spec/views/postback.erb Normal file
View File

@ -0,0 +1,13 @@
<h1>Postback</h1>
<form method="get">
<p>
<input type="submit" value="With no action">
</p>
</form>
<form action="" method="get">
<p>
<input type="submit" value="With blank action">
</p>
</form>

View File

@ -18,6 +18,13 @@
<input type="text" id="test_field" value="monkey"/>
<a title="twas a fine link" href="/redirect">A link came first</a>
<a title="a fine link" href="/with_simple_html">A link</a>
<a>No Href</a>
<a href="">Blank Href</a>
<a href="#">Blank Anchor</a>
<a href="#anchor">Anchor</a>
<a href="/with_simple_html#anchor">Anchor on different page</a>
<a href="/with_html#anchor">Anchor on same page</a>
<input type="text" value="" id="test_field">
</p>
<div id="hidden" style="display:none;">