Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jnicklas/capybara
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Feb 20, 2010
2 parents cb1ec61 + 4c632f3 commit 295c62f
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/capybara.rb
Expand Up @@ -14,7 +14,7 @@ class LocateHiddenElementError < CapybaraError; end
class InfiniteRedirectError < TimeoutError; end

class << self
attr_accessor :debug, :asset_root, :app_host, :run_server, :default_host
attr_accessor :debug, :asset_root, :app_host, :run_server
attr_accessor :default_selector, :default_wait_time, :ignore_hidden_elements

def default_selector
Expand Down
19 changes: 12 additions & 7 deletions lib/capybara/driver/rack_test_driver.rb
Expand Up @@ -10,10 +10,19 @@ def text
end

def [](name)
value = node[name.to_s]
return value.to_s if value
attr_name = name.to_s
case
when 'select' == tag_name && 'value' == attr_name
option = node.xpath(".//option[@selected='selected']").first || node.xpath(".//option").first
option.content if option
when 'input' == tag_name && 'checkbox' == type && 'checked' == attr_name
node[attr_name] == 'checked' ? true : false
else
node[attr_name]
end
end


def set(value)
if tag_name == 'input' and %w(text password hidden file).include?(type)
node['value'] = value.to_s
Expand Down Expand Up @@ -68,7 +77,7 @@ def path
private

def type
self[:type]
node[:type]
end

def form
Expand Down Expand Up @@ -185,10 +194,6 @@ def find(selector)

private

def build_rack_mock_session # :nodoc:
Rack::MockSession.new(app, Capybara.default_host)
end

def current_path
request.path rescue ""
end
Expand Down
1 change: 0 additions & 1 deletion lib/capybara/rails.rb
Expand Up @@ -9,4 +9,3 @@
end.to_app

Capybara.asset_root = Rails.root.join('public')
Capybara.default_host = "http://test.host"
27 changes: 17 additions & 10 deletions lib/capybara/server.rb
Expand Up @@ -39,28 +39,35 @@ def responsive?
is_running_on_port?(port)
end

def handler
begin
require 'rack/handler/thin'
Rack::Handler::Thin
rescue LoadError
begin
require 'rack/handler/mongrel'
Rack::Handler::Mongrel
rescue LoadError
require 'rack/handler/webrick'
Rack::Handler::WEBrick
end
end
end

def boot
find_available_port
Capybara.log "application has already booted" and return self if responsive?
Capybara.log "booting Rack applicartion on port #{port}"

Timeout.timeout(10) do
Thread.new do
begin
require 'rack/handler/thin'
Rack::Handler::Thin.run(Identify.new(@app), :Port => port)
rescue LoadError
require 'rack/handler/mongrel'
Rack::Handler::Mongrel.run(Identify.new(@app), :Port => port)
rescue LoadError
require 'rack/handler/webrick'
Rack::Handler::WEBrick.run(Identify.new(@app), :Port => port, :AccessLog => [])
end
handler.run(Identify.new(@app), :Port => port, :AccessLog => [])
end
Capybara.log "checking if application has booted"

loop do
Capybara.log("application has booted") and break if responsive?
Capybara.log("waiting for application to boot...")
sleep 0.5
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/capybara/xpath.rb
Expand Up @@ -69,9 +69,9 @@ def link(locator)
end

def button(locator)
xpath = append("//input[@type='submit' or @type='image'][@id=#{s(locator)} or contains(@value,#{s(locator)})]")
xpath = append("//input[@type='submit' or @type='image' or @type='button'][@id=#{s(locator)} or contains(@value,#{s(locator)})]")
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("//input[@type='submit' or @type='image' or @type='button'][@value=#{s(locator)}]")
xpath = xpath.prepend("//button[@value=#{s(locator)} or text()=#{s(locator)}]")
end

Expand Down
11 changes: 11 additions & 0 deletions spec/dsl/check_spec.rb
Expand Up @@ -5,6 +5,17 @@ module CheckSpec
before do
@session.visit('/form')
end

describe "'checked' attribute" do
it "should be true if checked" do
@session.check("Terms of Use")
@session.find(:xpath, "//input[@id='form_terms_of_use']")['checked'].should be_true
end

it "should be false if unchecked" do
@session.find(:xpath, "//input[@id='form_terms_of_use']")['checked'].should be_false
end
end

it "should check a checkbox by id" do
@session.check("form_pets_cat")
Expand Down
9 changes: 9 additions & 0 deletions spec/dsl/select_spec.rb
Expand Up @@ -3,6 +3,15 @@
before do
@session.visit('/form')
end

it "should return value of the first option" do
@session.find_field('Title').value.should == 'Mrs'
end

it "should return value of the selected option" do
@session.select("Miss", :from => 'Title')
@session.find_field('Title').value.should == 'Miss'
end

it "should select an option from a select box by id" do
@session.select("Finish", :from => 'form_locale')
Expand Down
1 change: 1 addition & 0 deletions spec/views/form.erb
Expand Up @@ -138,6 +138,7 @@
</div>

<p>
<input type="button" name="form[fresh]" id="fresh_btn" value="i am fresh"/>
<input type="submit" name="form[awesome]" id="awe123" value="awesome"/>
<input type="submit" name="form[crappy]" id="crap321" value="crappy"/>
<input type="image" name="form[okay]" id="okay556" value="okay"/>
Expand Down
17 changes: 17 additions & 0 deletions spec/xpath_spec.rb
Expand Up @@ -155,6 +155,23 @@
@driver.find(@query).first.value.should == 'seeekrit'
end
end

describe '#button' do
it "should find a button by id or content" do
@query = @xpath.button('awe123').to_s
@driver.find(@query).first.value.should == 'awesome'
@query = @xpath.button('okay556').to_s
@driver.find(@query).first.value.should == 'okay'
@query = @xpath.button('click_me_123').to_s
@driver.find(@query).first.value.should == 'click_me'
@query = @xpath.button('Click me!').to_s
@driver.find(@query).first.value.should == 'click_me'
@query = @xpath.button('fresh_btn').to_s
@driver.find(@query).first.value.should == 'i am fresh'
@query = @xpath.button('i am fresh').to_s
@driver.find(@query).first[:name].should == 'form[fresh]'
end
end

describe '#radio_button' do
it "should find a radio button by id or label" do
Expand Down

0 comments on commit 295c62f

Please sign in to comment.