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

Support returning an element from evaluate_script with selenium

This commit is contained in:
Thomas Walpole 2017-01-27 11:11:13 -08:00
parent dbeec59726
commit 11d99b84c8
3 changed files with 14 additions and 2 deletions

View file

@ -91,7 +91,9 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
end
def evaluate_script(script, *args)
browser.execute_script("return #{script}", *args.map { |arg| arg.is_a?(Capybara::Selenium::Node) ? arg.native : arg} )
result = browser.execute_script("return #{script}", *args.map { |arg| arg.is_a?(Capybara::Selenium::Node) ? arg.native : arg} )
result = Capybara::Selenium::Node.new(self, result) if result.is_a? Selenium::WebDriver::Element
result
end
def save_screenshot(path, _options={})

View file

@ -631,12 +631,14 @@ module Capybara
#
def evaluate_script(script, *args)
@touched = true
if driver.method(:evaluate_script).arity == 1
result = if driver.method(:evaluate_script).arity == 1
raise Capybara::NotSupportedByDriverError, "The current driver does not support arguments being passed with execute_script" unless args.empty?
driver.evaluate_script(script)
else
driver.evaluate_script(script, *args.map { |arg| arg.is_a?(Capybara::Node::Element) ? arg.base : arg} )
end
result = Capybara::Node::Element.new(self, result, nil, nil) if result.is_a?(Capybara::Driver::Node)
result
end
##

View file

@ -119,5 +119,13 @@ RSpec.shared_examples "Capybara::Session" do |session, mode|
expect(element.path).to eq('/html/body/div[2]/a[1]')
end
end
describe "#evaluate_script" do
it "can return elements" do
@session.visit('/form')
element = @session.evaluate_script("document.getElementById('form_title')")
expect(element).to eq @session.find(:id, 'form_title')
end
end
end
end