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

Added separate execute_script method, closes #71

This commit is contained in:
Jonas Nicklas 2010-06-29 22:41:33 +02:00
parent 87abb6cf0f
commit 745835770d
6 changed files with 32 additions and 2 deletions

View file

@ -228,7 +228,12 @@ You can also find specific elements, in order to manipulate them:
=== Scripting
In drivers which support it, you can easily evaluate JavaScript:
In drivers which support it, you can easily execute JavaScript:
page.execute_script("$('body').empty()")
For simple expressions, you can return the result of the script. Note
that this may break with more complicated expressions:
result = page.evaluate_script('4 + 4');

View file

@ -11,6 +11,10 @@ class Capybara::Driver::Base
raise NotImplementedError
end
def execute_script(script)
raise Capybara::NotSupportedByDriverError
end
def evaluate_script(script)
raise Capybara::NotSupportedByDriverError
end

View file

@ -125,6 +125,11 @@ class Capybara::Driver::Celerity < Capybara::Driver::Base
def wait?; true; end
def execute_script(script)
browser.execute_script script
nil
end
def evaluate_script(script)
browser.execute_script "#{script}"
end

View file

@ -128,6 +128,10 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base
def wait?; true; end
def execute_script(script)
browser.execute_script script
end
def evaluate_script(script)
browser.execute_script "return #{script}"
end

View file

@ -236,6 +236,10 @@ module Capybara
WaitUntil.timeout(timeout,driver) { yield }
end
def execute_script(script)
driver.execute_script(script)
end
def evaluate_script(script)
driver.evaluate_script(script)
end

View file

@ -35,12 +35,20 @@ shared_examples_for "session with javascript support" do
end
describe "#evaluate_script" do
it "should return the evaluated script" do
it "should evaluate the given script and return whatever it produces" do
@session.visit('/with_js')
@session.evaluate_script("1+3").should == 4
end
end
describe "#execute_script" do
it "should execute the given script and return nothing" do
@session.visit('/with_js')
@session.execute_script("$('#change').text('Funky Doodle')").should be_nil
@session.should have_css('#change', :text => 'Funky Doodle')
end
end
describe '#locate' do
it "should wait for asynchronous load" do
@session.visit('/with_js')