Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added separate execute_script method, closes #71

  • Loading branch information...
commit 745835770d3a65f21ffcf19b999a169a91945bd8 1 parent 87abb6c
@jnicklas authored
View
7 README.rdoc
@@ -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
4 lib/capybara/driver/base.rb
@@ -11,6 +11,10 @@ def find(query)
raise NotImplementedError
end
+ def execute_script(script)
+ raise Capybara::NotSupportedByDriverError
+ end
+
def evaluate_script(script)
raise Capybara::NotSupportedByDriverError
end
View
5 lib/capybara/driver/celerity_driver.rb
@@ -125,6 +125,11 @@ def find(selector)
def wait?; true; end
+ def execute_script(script)
+ browser.execute_script script
+ nil
+ end
+
def evaluate_script(script)
browser.execute_script "#{script}"
end
View
4 lib/capybara/driver/selenium_driver.rb
@@ -128,6 +128,10 @@ def find(selector)
def wait?; true; end
+ def execute_script(script)
+ browser.execute_script script
+ end
+
def evaluate_script(script)
browser.execute_script "return #{script}"
end
View
4 lib/capybara/session.rb
@@ -236,6 +236,10 @@ def wait_until(timeout = Capybara.default_wait_time)
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
10 lib/capybara/spec/session/javascript.rb
@@ -35,12 +35,20 @@
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')
Please sign in to comment.
Something went wrong with that request. Please try again.