mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Merge remote branch 'denro/evaluate_script'
Conflicts: lib/capybara.rb lib/capybara/driver/selenium_driver.rb lib/capybara/dsl.rb spec/session/culerity_session_spec.rb spec/session/selenium_session_spec.rb spec/session_spec.rb
This commit is contained in:
commit
d183409d19
14 changed files with 134 additions and 84 deletions
|
|
@ -119,6 +119,10 @@ Querying:
|
||||||
find_button
|
find_button
|
||||||
field_labeled
|
field_labeled
|
||||||
|
|
||||||
|
Scripting:
|
||||||
|
|
||||||
|
evaluate_script – Returns the value of the executed javascript (only works on javascript supported drivers)
|
||||||
|
|
||||||
Debugging:
|
Debugging:
|
||||||
|
|
||||||
save_and_open_page
|
save_and_open_page
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ module Capybara
|
||||||
class CapybaraError < StandardError; end
|
class CapybaraError < StandardError; end
|
||||||
class DriverNotFoundError < CapybaraError; end
|
class DriverNotFoundError < CapybaraError; end
|
||||||
class ElementNotFound < CapybaraError; end
|
class ElementNotFound < CapybaraError; end
|
||||||
|
class NotSupportedByDriverError < CapybaraError; end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :debug, :asset_root
|
attr_accessor :debug, :asset_root
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,10 @@ class Capybara::Driver::Culerity < Capybara::Driver::Base
|
||||||
|
|
||||||
def wait?; true; end
|
def wait?; true; end
|
||||||
|
|
||||||
|
def evaluate_script(script)
|
||||||
|
browser.execute_script "#{script}"
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def url(path)
|
def url(path)
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,10 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base
|
||||||
|
|
||||||
def wait?; true; end
|
def wait?; true; end
|
||||||
|
|
||||||
|
def evaluate_script(script)
|
||||||
|
driver.execute_script "return #{script}"
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def url(path)
|
def url(path)
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ module Capybara
|
||||||
:visit, :body, :click_link, :click_button, :fill_in, :choose, :has_xpath?, :has_css?,
|
:visit, :body, :click_link, :click_button, :fill_in, :choose, :has_xpath?, :has_css?,
|
||||||
:check, :uncheck, :attach_file, :select, :has_content?, :within, :within_fieldset,
|
:check, :uncheck, :attach_file, :select, :has_content?, :within, :within_fieldset,
|
||||||
:within_table, :save_and_open_page, :find, :find_field, :find_link, :find_button,
|
:within_table, :save_and_open_page, :find, :find_field, :find_link, :find_button,
|
||||||
:field_labeled, :all, :wait_for
|
:field_labeled, :all, :wait_for, :evaluate_script
|
||||||
]
|
]
|
||||||
SESSION_METHODS.each do |method|
|
SESSION_METHODS.each do |method|
|
||||||
class_eval <<-RUBY, __FILE__, __LINE__+1
|
class_eval <<-RUBY, __FILE__, __LINE__+1
|
||||||
|
|
|
||||||
|
|
@ -156,6 +156,14 @@ module Capybara
|
||||||
find(XPath.button(locator))
|
find(XPath.button(locator))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def evaluate_script(script)
|
||||||
|
begin
|
||||||
|
driver.evaluate_script(script)
|
||||||
|
rescue NoMethodError
|
||||||
|
raise NotSupportedByDriverError
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def css_to_xpath(css)
|
def css_to_xpath(css)
|
||||||
|
|
|
||||||
|
|
@ -63,20 +63,27 @@ shared_examples_for 'driver' do
|
||||||
end
|
end
|
||||||
|
|
||||||
shared_examples_for "driver with javascript support" do
|
shared_examples_for "driver with javascript support" do
|
||||||
|
before { @driver.visit('/with_js') }
|
||||||
|
|
||||||
describe '#find' do
|
describe '#find' do
|
||||||
it "should find dynamically changed nodes" do
|
it "should find dynamically changed nodes" do
|
||||||
@driver.visit('/with_js')
|
|
||||||
@driver.find('//p').first.text.should == 'I changed it'
|
@driver.find('//p').first.text.should == 'I changed it'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#drag_to' do
|
describe '#drag_to' do
|
||||||
it "should drag and drop an object" do
|
it "should drag and drop an object" do
|
||||||
@driver.visit('/with_js')
|
|
||||||
draggable = @driver.find('//div[@id="drag"]').first
|
draggable = @driver.find('//div[@id="drag"]').first
|
||||||
droppable = @driver.find('//div[@id="drop"]').first
|
droppable = @driver.find('//div[@id="drop"]').first
|
||||||
draggable.drag_to(droppable)
|
draggable.drag_to(droppable)
|
||||||
@driver.find('//div[contains(., "Dropped!")]').should_not be_nil
|
@driver.find('//div[contains(., "Dropped!")]').should_not be_nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#evaluate_script" do
|
||||||
|
it "should return the value of the executed script" do
|
||||||
|
@driver.evaluate_script('1+1').should == 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -114,6 +114,7 @@ describe Capybara do
|
||||||
end
|
end
|
||||||
|
|
||||||
it_should_behave_like "session"
|
it_should_behave_like "session"
|
||||||
|
it_should_behave_like "session without javascript support"
|
||||||
|
|
||||||
it "should be possible to include it in another class" do
|
it "should be possible to include it in another class" do
|
||||||
klass = Class.new do
|
klass = Class.new do
|
||||||
|
|
|
||||||
|
|
@ -19,5 +19,6 @@ describe Capybara::Session do
|
||||||
end
|
end
|
||||||
|
|
||||||
it_should_behave_like "session"
|
it_should_behave_like "session"
|
||||||
|
it_should_behave_like "session without javascript support"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ describe Capybara::Session do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#driver' do
|
describe '#driver' do
|
||||||
it "should be a rack test driver" do
|
it "should be a selenium driver" do
|
||||||
@session.driver.should be_an_instance_of(Capybara::Driver::Selenium)
|
@session.driver.should be_an_instance_of(Capybara::Driver::Selenium)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -834,9 +834,28 @@ shared_examples_for "session" do
|
||||||
extract_results(@session)['villain_name'].should == 'Quantum'
|
extract_results(@session)['villain_name'].should == 'Quantum'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
shared_examples_for "session without javascript support" do
|
||||||
|
describe "#evaluate_script" do
|
||||||
|
before{ @session.visit('/with_js') }
|
||||||
|
it "should raise an error" do
|
||||||
|
running {
|
||||||
|
@session.evaluate_script("1+5")
|
||||||
|
}.should raise_error(Capybara::NotSupportedByDriverError)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
shared_examples_for "session with javascript support" do
|
shared_examples_for "session with javascript support" do
|
||||||
|
describe "#evaluate_script" do
|
||||||
|
before{ @session.visit('/with_js') }
|
||||||
|
it "should return the evaluated script" do
|
||||||
|
@session.evaluate_script("1+3").should == 4
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#wait_for' do
|
describe '#wait_for' do
|
||||||
it "should wait for asynchronous load" do
|
it "should wait for asynchronous load" do
|
||||||
@session.visit('/with_js')
|
@session.visit('/with_js')
|
||||||
|
|
@ -880,3 +899,4 @@ describe Capybara::Session do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue