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

support elements in evaluate_script result structures

This commit is contained in:
Thomas Walpole 2017-01-28 14:34:44 -08:00
parent 30ed04d841
commit fa75398921
4 changed files with 56 additions and 5 deletions

View file

@ -92,8 +92,7 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
def evaluate_script(script, *args) def evaluate_script(script, *args)
result = 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 unwrap_script_result(result)
result
end end
def save_screenshot(path, _options={}) def save_screenshot(path, _options={})
@ -327,4 +326,17 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
def silenced_unknown_error_messages def silenced_unknown_error_messages
[ /Error communicating with the remote browser/ ] [ /Error communicating with the remote browser/ ]
end end
def unwrap_script_result(arg)
case arg
when Array
arg.map { |e| unwrap_script_result(e) }
when Hash
arg.each { |k, v| arg[k] = unwrap_script_result(v) }
when Selenium::WebDriver::Element
Capybara::Selenium::Node.new(self, arg)
else
arg
end
end
end end

View file

@ -637,8 +637,7 @@ module Capybara
else else
driver.evaluate_script(script, *args.map { |arg| arg.is_a?(Capybara::Node::Element) ? arg.base : arg} ) driver.evaluate_script(script, *args.map { |arg| arg.is_a?(Capybara::Node::Element) ? arg.base : arg} )
end end
result = Capybara::Node::Element.new(self, result, nil, nil) if result.is_a?(Capybara::Driver::Node) element_script_result(result)
result
end end
## ##
@ -844,5 +843,18 @@ module Capybara
def scopes def scopes
@scopes ||= [nil] @scopes ||= [nil]
end end
def element_script_result(arg)
case arg
when Array
arg.map { |e| element_script_result(e) }
when Hash
arg.each { |k, v| arg[k] = element_script_result(v) }
when Capybara::Driver::Node
Capybara::Node::Element.new(self, arg, nil, nil)
else
arg
end
end
end end
end end

View file

@ -18,4 +18,11 @@ Capybara::SpecHelper.spec "#evaluate_script", requires: [:js] do
expect(@session).to have_css('#change', text: 'Doodle Funk') expect(@session).to have_css('#change', text: 'Doodle Funk')
end end
it "should support returning elements", requires: [:js, :es_args] do
@session.visit('/with_js')
el = @session.find(:css, '#change')
el = @session.evaluate_script("document.getElementById('change')")
expect(el).to be_instance_of(Capybara::Node::Element)
expect(el).to eq(@session.find(:css, '#change'))
end
end end

View file

@ -121,11 +121,31 @@ RSpec.shared_examples "Capybara::Session" do |session, mode|
end end
describe "#evaluate_script" do describe "#evaluate_script" do
it "can return elements" do it "can return an element" do
@session.visit('/form') @session.visit('/form')
element = @session.evaluate_script("document.getElementById('form_title')") element = @session.evaluate_script("document.getElementById('form_title')")
expect(element).to eq @session.find(:id, 'form_title') expect(element).to eq @session.find(:id, 'form_title')
end end
it "can return arrays of nested elements" do
@session.visit('/form')
elements = @session.evaluate_script('document.querySelectorAll("#form_city option")')
elements.each do |el|
expect(el).to be_instance_of Capybara::Node::Element
end
expect(elements).to eq @session.find(:css, '#form_city').all(:css, 'option').to_a
end
it "can return hashes with elements" do
@session.visit('/form')
result = @session.evaluate_script("{ a: document.getElementById('form_title'), b: {c: document.querySelectorAll('#form_city option')}}")
expect(result).to eq({
'a' => @session.find(:id, 'form_title'),
'b' => {
'c' => @session.find(:css, '#form_city').all(:css, 'option').to_a
}
})
end
end end
end end
end end