1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Merge branch 'master' of git://github.com/jnicklas/capybara into interact_with_visible_only

Conflicts:
	lib/capybara.rb
This commit is contained in:
Andrea Fazzi 2010-01-11 21:56:56 +01:00
commit 0af58c61d3
9 changed files with 140 additions and 28 deletions

View file

@ -337,6 +337,7 @@ The following people have dedicated their time and effort to Capybara:
* Aaron Patterson
* Dan Dofter
* Thorbjørn Hermansen
* Louis T.
== License:

View file

@ -10,6 +10,7 @@ module Capybara
class NotSupportedByDriverError < CapybaraError; end
class TimeoutError < CapybaraError; end
class LocateHiddenElementError < CapybaraError; end
class InfiniteRedirectError < TimeoutError; end
class << self
attr_accessor :debug, :asset_root, :app_host

View file

@ -144,7 +144,7 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
def visit(path, attributes = {})
get(path, attributes)
follow_redirect! while response.redirect?
follow_redirects!
cache_body
end
@ -158,7 +158,7 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
def submit(path, attributes)
post(path, attributes)
follow_redirect! while response.redirect?
follow_redirects!
cache_body
end
@ -168,6 +168,16 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
private
def follow_redirects!
Capybara::WaitUntil.timeout(4) do
redirect = response.redirect?
follow_redirect! if redirect
not redirect
end
rescue Capybara::TimeoutError
raise Capybara::InfiniteRedirectError, "infinite redirect detected!"
end
def cache_body
@body = response.body
@html = Nokogiri::HTML(body)

View file

@ -34,11 +34,13 @@ module Capybara
end
def field(locator)
fillable_field(locator).file_field(locator).checkbox(locator).radio_button(locator).select(locator)
fillable_field(locator).input_field(:file, locator).checkbox(locator).radio_button(locator).select(locator)
end
def fillable_field(locator)
text_field(locator).password_field(locator).text_area(locator)
[:text, :password, :email, :url, :search, :tel, :color].inject(text_area(locator)) do |all, type|
all.input_field(type, locator)
end
end
def content(locator)
@ -54,40 +56,27 @@ module Capybara
end
def link(locator)
append("//a[@id=#{s(locator)} or contains(.,#{s(locator)}) or @title=#{s(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)}]")
end
def button(locator)
xpath = append("//input[@type='submit' or @type='image'][@id=#{s(locator)} or contains(@value,#{s(locator)})]")
xpath.append("//button[@id=#{s(locator)} or contains(@value,#{s(locator)}) or contains(.,#{s(locator)})]")
end
def text_field(locator)
add_field(locator, "//input[@type='text']")
end
def password_field(locator)
add_field(locator, "//input[@type='password']")
xpath = xpath.append("//button[@id=#{s(locator)} or contains(@value,#{s(locator)}) or contains(.,#{s(locator)})]")
xpath = xpath.prepend("//input[@type='submit' or @type='image'][@value=#{s(locator)}]")
xpath = xpath.prepend("//button[@value=#{s(locator)} or text()=#{s(locator)}]")
end
def text_area(locator)
add_field(locator, "//textarea")
end
def radio_button(locator)
add_field(locator, "//input[@type='radio']")
end
def checkbox(locator)
add_field(locator, "//input[@type='checkbox']")
end
def select(locator)
add_field(locator, "//select")
end
def file_field(locator)
add_field(locator, "//input[@type='file']")
def input_field(type, locator)
add_field(locator, "//input[@type='#{type}']")
end
def scope(scope)
@ -106,6 +95,22 @@ module Capybara
XPath.new(*[XPath.wrap(path).paths, @paths].flatten)
end
def checkbox(locator)
input_field(:checkbox, locator)
end
def radio_button(locator)
input_field(:radio, locator)
end
[:text, :password, :email, :url, :search, :tel, :color, :file].each do |type|
class_eval <<-RUBY, __FILE__, __LINE__+1
def #{type}_field(locator)
input_field(:#{type}, locator)
end
RUBY
end
protected
def add_field(locator, field)

View file

@ -115,6 +115,11 @@ module ClickButtonSpec
@session.click_button('Click')
extract_results(@session)['first_name'].should == 'John'
end
it "should prefer exact matches over partial matches" do
@session.click_button('Just an input')
extract_results(@session)['button'].should == 'button_second'
end
end
context "with id given on a button defined by <button> tag" do
@ -134,6 +139,11 @@ module ClickButtonSpec
@session.click_button('ck_me')
extract_results(@session)['first_name'].should == 'John'
end
it "should prefer exact matches over partial matches" do
@session.click_button('Just a button')
extract_results(@session)['button'].should == 'Just a button'
end
end
context "with a locator that doesn't exist" do

View file

@ -17,6 +17,16 @@ module ClickLinkSpec
@session.click_link('labore')
@session.body.should include('Bar')
end
it "should accept partial matches" do
@session.click_link('abo')
@session.body.should include('Bar')
end
it "should prefer exact matches over partial matches" do
@session.click_link('A link')
@session.body.should include('Bar')
end
end
context "with title given" do
@ -24,6 +34,16 @@ module ClickLinkSpec
@session.click_link('awesome title')
@session.body.should include('Bar')
end
it "should accept partial matches" do
@session.click_link('some tit')
@session.body.should include('Bar')
end
it "should prefer exact matches over partial matches" do
@session.click_link('a fine link')
@session.body.should include('Bar')
end
end
context "with a locator that doesn't exist" do

View file

@ -165,3 +165,35 @@
<input type="submit" value="Go FAR"/>
</p>
</form>
<form action="/form" method="post">
<p>
<label for="html5_email">Html5 Email</label>
<input type="email" name="form[html5_email]" value="person@email.com" id="html5_email"/>
</p>
<p>
<label for="html5_url">Html5 Url</label>
<input type="url" name="form[html5_url]" value="http://www.example.com" id="html5_url"/>
</p>
<p>
<label for="html5_search">Html5 Search</label>
<input type="search" name="form[html5_search]" value="what are you looking for" id="html5_search"/>
</p>
<p>
<label for="html5_tel">Html5 Tel</label>
<input type="tel" name="form[html5_tel]" value="911" id="html5_tel"/>
</p>
<p>
<label for="html5_color">Html5 Color</label>
<input type="color" name="form[html5_color]" value="#FFF" id="html5_color"/>
</p>
</form>
<form action="/form" method="post">
<p>
<button type="submit" name="form[button]" value="button_first">Just an input that came first</button>
<button type="submit" name="form[button]" value="button_second">Just an input</button>
<input type="submit" name="form[button]" value="Just a button that came first"/>
<input type="submit" name="form[button]" value="Just a button"/>
</p>
</form>

View file

@ -16,6 +16,8 @@
<p>
<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>
</p>
<div id="hidden" style="display:none;">

View file

@ -83,9 +83,9 @@ describe Capybara::XPath do
end
it "should be chainable" do
@query = @xpath.field('First Name').password_field('First Name').to_s
@query = @xpath.field('First Name').input_field(:password, 'First Name').to_s
@driver.find(@query).first.value.should == 'John'
@query = @xpath.field('Password').password_field('Password').to_s
@query = @xpath.field('Password').input_field(:password, 'Password').to_s
@driver.find(@query).first.value.should == 'seeekrit'
end
end
@ -220,4 +220,35 @@ describe Capybara::XPath do
end
end
[ [:email_field, 'html5_email', 'Html5 Email', 'person@email.com'],
[:url_field, 'html5_url', 'Html5 Url', 'http://www.example.com'],
[:search_field, 'html5_search', 'Html5 Search', 'what are you looking for'],
[:tel_field, 'html5_tel', 'Html5 Tel', '911'],
[:color_field, 'html5_color', 'Html5 Color', '#FFF']].each do |method, id, label, output|
describe "##{method}" do
it "should find a file field by label" do
@query = @xpath.send(method, label).to_s
@driver.find(@query).first.value.should == output
end
it "should find a file field by id" do
@query = @xpath.send(method, id).to_s
@driver.find(@query).first.value.should == output
end
it "should be chainable" do
@query = @xpath.send(method, label).password_field(label).to_s
@driver.find(@query).first.value.should == output
@query = @xpath.send(method, 'Password').password_field('Password').to_s
@driver.find(@query).first.value.should == 'seeekrit'
end
it "should be a #fillable_field" do
@query = @xpath.fillable_field(label).to_s
@driver.find(@query).first.value.should == output
end
end
end
end