use Element#execute/evaluate_script internally

This commit is contained in:
Thomas Walpole 2018-05-18 15:04:49 -07:00
parent a5b757d6d9
commit cacbf86b2f
1 changed files with 10 additions and 12 deletions

View File

@ -255,7 +255,7 @@ module Capybara
end end
def select_datalist_option(input, value) def select_datalist_option(input, value)
datalist_options = session.evaluate_script(DATALIST_OPTIONS_SCRIPT, input) datalist_options = input.evaluate_script(DATALIST_OPTIONS_SCRIPT)
if (option = datalist_options.find { |o| o['value'] == value || o['label'] == value }) if (option = datalist_options.find { |o| o['value'] == value || o['label'] == value })
input.set(option['value']) input.set(option['value'])
else else
@ -280,13 +280,13 @@ module Capybara
end end
def _update_style(element, style) def _update_style(element, style)
session.execute_script(UPDATE_STYLE_SCRIPT, element, style) element.execute_script(UPDATE_STYLE_SCRIPT, style)
rescue Capybara::NotSupportedByDriverError rescue Capybara::NotSupportedByDriverError
warn "The :make_visible option is not supported by the current driver - ignoring" warn "The :make_visible option is not supported by the current driver - ignoring"
end end
def _reset_style(element) def _reset_style(element)
session.execute_script(RESET_STYLE_SCRIPT, element) element.execute_script(RESET_STYLE_SCRIPT)
rescue StandardError # rubocop:disable Lint/HandleExceptions swallow extra errors rescue StandardError # rubocop:disable Lint/HandleExceptions swallow extra errors
end end
@ -308,26 +308,24 @@ module Capybara
end end
UPDATE_STYLE_SCRIPT = <<~'JS' UPDATE_STYLE_SCRIPT = <<~'JS'
var el = arguments[0]; this.capybara_style_cache = this.style.cssText;
el.capybara_style_cache = el.style.cssText; var css = arguments[0];
var css = arguments[1];
for (var prop in css){ for (var prop in css){
if (css.hasOwnProperty(prop)) { if (css.hasOwnProperty(prop)) {
el.style[prop] = css[prop] this.style[prop] = css[prop]
} }
} }
JS JS
RESET_STYLE_SCRIPT = <<~'JS' RESET_STYLE_SCRIPT = <<~'JS'
var el = arguments[0]; if (this.hasOwnProperty('capybara_style_cache')) {
if (el.hasOwnProperty('capybara_style_cache')) { this.style.cssText = this.capybara_style_cache;
el.style.cssText = el.capybara_style_cache; delete this.capybara_style_cache;
delete el.capybara_style_cache;
} }
JS JS
DATALIST_OPTIONS_SCRIPT = <<~'JS' DATALIST_OPTIONS_SCRIPT = <<~'JS'
Array.prototype.slice.call((arguments[0].list||{}).options || []). Array.prototype.slice.call((this.list||{}).options || []).
filter(function(el){ return !el.disabled }). filter(function(el){ return !el.disabled }).
map(function(el){ return { "value": el.value, "label": el.label} }) map(function(el){ return { "value": el.value, "label": el.label} })
JS JS