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

Add Element#evaluate_async_script

This commit is contained in:
Thomas Walpole 2018-06-03 11:20:29 -07:00
parent 25b942324d
commit 5a75fc140f
3 changed files with 34 additions and 1 deletions

View file

@ -381,6 +381,24 @@ module Capybara
JS
end
##
#
# Evaluate the given JavaScript in the context of the element and obtain the result from a
# callback function which will be passed as the last argument to the script. `this` in the
# script will refer to the element this is called on
#
# @param [String] script A string of JavaScript to evaluate
# @return [Object] The result of the evaluated JavaScript (may be driver specific)
#
def evaluate_async_script(script, *args)
session.evaluate_async_script(<<~JS, self, *args)
(function (){
#{script}
}).apply(arguments[0], Array.prototype.slice.call(arguments,1));
JS
end
def reload
if @allow_reload
begin

View file

@ -497,6 +497,21 @@ Capybara::SpecHelper.spec "node" do
end
end
describe "#evaluate_async_script", requires: %i[js es_args] do
it "should evaluate the given script in the context of the element" do
@session.visit('/with_js')
el = @session.find(:css, '#with_change_event')
expect(el.evaluate_async_script("arguments[0](this.value)")).to eq('default value')
end
it "should support returning elements after asynchronous operation" do
@session.visit('/with_js')
change = @session.find(:css, '#change') # ensure page has loaded and element is available
el = change.evaluate_async_script("var cb = arguments[0]; setTimeout(function(el){ cb(el) }, 100, this)")
expect(el).to eq(change)
end
end
describe '#reload', requires: [:js] do
context "without automatic reload" do
before { Capybara.automatic_reload = false }

View file

@ -273,7 +273,7 @@ RSpec.shared_examples "Capybara::Session" do |session, mode|
expect(@animation_session).to have_no_link('transition me away', wait: 0.5)
end
it "should disable CSS animations", :focus_ do
it "should disable CSS animations" do
@animation_session.visit('with_animation')
@animation_session.click_link('animate me away')
expect(@animation_session).to have_no_link('animate me away', wait: 0.5)