added click_element with specs (only works in webdriver so far)

This commit is contained in:
Andrew Brown 2009-12-16 13:25:12 +01:00
parent 41c7bb3d99
commit 7b8e6a640d
8 changed files with 30 additions and 4 deletions

View File

@ -50,7 +50,7 @@ module Capybara
:visit, :body, :click_link, :click_button, :drag, :fill_in, :choose, :has_xpath?, :has_css?,
:check, :uncheck, :attach_file, :select, :has_content?, :within, :within_fieldset,
:within_table, :save_and_open_page, :find, :find_field, :find_link, :find_button,
:field_labeled, :all, :wait_for, :evaluate_script, :click
:field_labeled, :all, :wait_for, :evaluate_script, :click, :click_element
]
SESSION_METHODS.each do |method|
class_eval <<-RUBY, __FILE__, __LINE__+1

View File

@ -43,6 +43,12 @@ module Capybara
button.click
end
def click_element(locator)
element = wait_for(XPath.element(locator))
raise Capybara::ElementNotFound, "the element '#{locator}' could not be found" unless element
element.click
end
def drag(source_locator, target_locator)
source = wait_for(source_locator)
raise Capybara::ElementNotFound, "drag source '#{source_locator}' not found on page" unless source

View File

@ -34,6 +34,10 @@ module Capybara
def fillable_field(locator)
text_field(locator).password_field(locator).text_area(locator)
end
def element(locator)
append("//div[@id=#{s(locator)}]")
end
def content(locator)
append("/descendant-or-self::*[contains(.,#{s(locator)})]")

View File

@ -0,0 +1,11 @@
module ClickElementSpec
shared_examples_for "click_element" do
describe '#click_element' do
it "should click on a element" do
@session.visit('/with_js')
@session.click_element('fool_to_cry')
@session.body.should include('<div id="fool_to_cry">daddy your a fool to cry</div>')
end
end
end
end

View File

@ -40,7 +40,7 @@ shared_examples_for "session" do
it_should_behave_like "select"
it_should_behave_like "uncheck"
it_should_behave_like "wait_for"
it_should_behave_like "within"
it_should_behave_like "within"
end

View File

@ -1,5 +1,4 @@
require File.expand_path('spec_helper', File.dirname(__FILE__))
require 'nokogiri'
shared_examples_for "session with javascript support" do
@ -49,5 +48,7 @@ shared_examples_for "session with javascript support" do
@session.fill_in('new_field', :with => 'Testing...')
end
end
it_should_behave_like "click_element"
end

View File

@ -1,5 +1,4 @@
require File.expand_path('spec_helper', File.dirname(__FILE__))
require 'nokogiri'
shared_examples_for "session without javascript support" do

View File

@ -24,6 +24,9 @@
}, 500);
return false;
});
$("#fool_to_cry").click(function(){
$(this).html('daddy your a fool to cry')
});
});
//]]>
</script>
@ -32,6 +35,8 @@
<body id="with_js">
<h1>FooBar</h1>
<div id="fool_to_cry">and she said</div>
<p id="change">This is text</p>
<div id="drag">
<p>This is a draggable element.</p>