mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Branch merge
This commit is contained in:
commit
ea6857b52b
8 changed files with 283 additions and 183 deletions
22
README.rdoc
22
README.rdoc
|
@ -153,6 +153,20 @@ For ultimate control, you can instantiate and use a session manually.
|
|||
end
|
||||
session.click_link 'Sign in'
|
||||
|
||||
== XPath an CSS
|
||||
|
||||
Capybara does not try to guess what kind of selector you are going to give it,
|
||||
if you want to use CSS with your 'within' declarations for example, you'll need
|
||||
to do:
|
||||
|
||||
within(:css, 'ul li') { ... }
|
||||
|
||||
Alternatively you can set the default selector to CSS, which may help if you are
|
||||
moving from Webrat and used CSS a lot, or simply generally prefer CSS:
|
||||
|
||||
Capybara.default_selector = :css
|
||||
within('ul li') { ... }
|
||||
|
||||
== Gotchas:
|
||||
|
||||
* Install JRuby and the 'celerity' gem, version 0.7.4 (0.7.5 has a bug with
|
||||
|
@ -186,6 +200,14 @@ For ultimate control, you can instantiate and use a session manually.
|
|||
<tt><a href="/same/url#"></tt> instead. You can achieve this in Rails with
|
||||
<tt>link_to('foo', :anchor => '')</tt>
|
||||
|
||||
== Contributors:
|
||||
|
||||
The following people have dedicated their time and effort to Capybara:
|
||||
|
||||
* Jonas Nicklas
|
||||
* Dennis Rogenius
|
||||
* Rob Holland
|
||||
|
||||
== License:
|
||||
|
||||
(The MIT License)
|
||||
|
|
1
Rakefile
1
Rakefile
|
@ -22,7 +22,6 @@ $hoe = Hoe.spec 'capybara' do
|
|||
['selenium-webdriver', '>= 0.0.3'],
|
||||
['rack', '>= 1.0.0'],
|
||||
['rack-test', '>= 0.5.2'],
|
||||
['database_cleaner', '>= 0.2.3']
|
||||
]
|
||||
|
||||
self.extra_dev_deps = [
|
||||
|
|
|
@ -7,10 +7,6 @@ After do
|
|||
Capybara.reset_sessions!
|
||||
end
|
||||
|
||||
require 'database_cleaner'
|
||||
require 'database_cleaner/cucumber'
|
||||
DatabaseCleaner.strategy = :truncation
|
||||
|
||||
Before('@javascript') do
|
||||
Capybara.current_driver = Capybara.javascript_driver
|
||||
end
|
||||
|
|
|
@ -22,7 +22,13 @@ module Capybara
|
|||
end
|
||||
|
||||
def current_session
|
||||
session_pool["#{current_driver}#{app.object_id}"] ||= Capybara::Session.new(current_driver, app)
|
||||
session_pool["#{current_driver}#{app.object_id}"] ||= begin
|
||||
Capybara::Session.new(current_driver, app)
|
||||
end
|
||||
end
|
||||
|
||||
def current_session?
|
||||
session_pool.has_key?("#{current_driver}#{app.object_id}")
|
||||
end
|
||||
|
||||
def reset_sessions!
|
||||
|
|
|
@ -3,10 +3,14 @@ module Capybara
|
|||
extend(self)
|
||||
|
||||
def save_and_open_page(html)
|
||||
require 'tempfile'
|
||||
tempfile = Tempfile.new("capybara#{rand(1000000)}")
|
||||
name="capybara-#{Time.new.strftime("%Y%m%d%H%M%S")}.html"
|
||||
|
||||
FileUtils.touch(name) unless File.exist?(name)
|
||||
|
||||
tempfile = File.new(name,'w')
|
||||
tempfile.write(rewrite_css_and_image_references(html))
|
||||
tempfile.close
|
||||
|
||||
open_in_browser(tempfile.path)
|
||||
end
|
||||
|
||||
|
|
|
@ -1,4 +1,14 @@
|
|||
class Capybara::Session
|
||||
module Capybara
|
||||
|
||||
class << self
|
||||
attr_writer :default_selector
|
||||
|
||||
def default_selector
|
||||
@default_selector ||= :xpath
|
||||
end
|
||||
end
|
||||
|
||||
class Session
|
||||
|
||||
FIELDS_PATHS = {
|
||||
:text_field => proc { |id| "//input[@type='text'][@id='#{id}']" },
|
||||
|
@ -92,7 +102,7 @@ class Capybara::Session
|
|||
end
|
||||
|
||||
def within(kind, scope=nil)
|
||||
kind, scope = :xpath, kind unless scope
|
||||
kind, scope = Capybara.default_selector, kind unless scope
|
||||
scope = css_to_xpath(scope) if kind == :css
|
||||
raise Capybara::ElementNotFound, "scope '#{scope}' not found on page" if find(scope).empty?
|
||||
scopes.push(scope)
|
||||
|
@ -137,7 +147,7 @@ class Capybara::Session
|
|||
button
|
||||
end
|
||||
|
||||
private
|
||||
private
|
||||
|
||||
def css_to_xpath(css)
|
||||
Nokogiri::CSS.xpath_for(css).first
|
||||
|
@ -186,5 +196,5 @@ private
|
|||
locator = current_scope.to_s + locator
|
||||
driver.find(locator)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
43
spec/save_and_open_page_spec.rb
Normal file
43
spec/save_and_open_page_spec.rb
Normal file
|
@ -0,0 +1,43 @@
|
|||
require File.expand_path('spec_helper', File.dirname(__FILE__))
|
||||
|
||||
require 'capybara/save_and_open_page'
|
||||
require 'launchy'
|
||||
describe Capybara::SaveAndOpenPage do
|
||||
describe "#save_save_and_open_page" do
|
||||
before do
|
||||
|
||||
@time = Time.new.strftime("%Y%m%d%H%M%S")
|
||||
@name = "capybara-#{@time}.html"
|
||||
|
||||
@temp_file = mock("FILE")
|
||||
@temp_file.stub!(:write)
|
||||
@temp_file.stub!(:close)
|
||||
@temp_file.stub!(:path).and_return(@name)
|
||||
|
||||
File.should_receive(:exist?).and_return true
|
||||
File.should_receive(:new).and_return @temp_file
|
||||
|
||||
@html = <<-HTML
|
||||
<html>
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<h1>test</h1>
|
||||
</body>
|
||||
<html>
|
||||
HTML
|
||||
|
||||
Launchy::Browser.stub(:run)
|
||||
end
|
||||
|
||||
it "should create a new temporary file" do
|
||||
@temp_file.should_receive(:write).with @html
|
||||
Capybara::SaveAndOpenPage.save_and_open_page @html
|
||||
end
|
||||
|
||||
it "should open the file in the browser" do
|
||||
Capybara::SaveAndOpenPage.should_receive(:open_in_browser).with(@name)
|
||||
Capybara::SaveAndOpenPage.save_and_open_page @html
|
||||
end
|
||||
end
|
||||
end
|
|
@ -543,6 +543,26 @@ shared_examples_for "session" do
|
|||
end
|
||||
end
|
||||
|
||||
context "with the default selector" do
|
||||
it "should use XPath" do
|
||||
@session.within("//li[contains(., 'With Simple HTML')]") do
|
||||
@session.click_link('Go')
|
||||
end
|
||||
@session.body.should include('<h1>Bar</h1>')
|
||||
end
|
||||
end
|
||||
|
||||
context "with the default selector set to CSS" do
|
||||
it "should use CSS" do
|
||||
Capybara.default_selector = :css
|
||||
@session.within("ul li[contains('With Simple HTML')]") do
|
||||
@session.click_link('Go')
|
||||
end
|
||||
@session.body.should include('<h1>Bar</h1>')
|
||||
Capybara.default_selector = :xpath
|
||||
end
|
||||
end
|
||||
|
||||
context "with click_link" do
|
||||
it "should click links in the given scope" do
|
||||
@session.within("//li[contains(.,'With Simple HTML')]") do
|
||||
|
|
Loading…
Reference in a new issue