Skip to content

Commit

Permalink
use Element#execute/evaluate_script internally
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed May 19, 2018
1 parent a5b757d commit cacbf86
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions lib/capybara/node/actions.rb
Expand Up @@ -255,7 +255,7 @@ def find_select_or_datalist_input(from, options)
end

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 })
input.set(option['value'])
else
Expand All @@ -280,13 +280,13 @@ def while_visible(element, visible_css)
end

def _update_style(element, style)
session.execute_script(UPDATE_STYLE_SCRIPT, element, style)
element.execute_script(UPDATE_STYLE_SCRIPT, style)
rescue Capybara::NotSupportedByDriverError
warn "The :make_visible option is not supported by the current driver - ignoring"
end

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
end

Expand All @@ -308,26 +308,24 @@ def _check_with_label(selector, checked, locator, allow_label_click: session_opt
end

UPDATE_STYLE_SCRIPT = <<~'JS'
var el = arguments[0];
el.capybara_style_cache = el.style.cssText;
var css = arguments[1];
this.capybara_style_cache = this.style.cssText;
var css = arguments[0];
for (var prop in css){
if (css.hasOwnProperty(prop)) {
el.style[prop] = css[prop]
this.style[prop] = css[prop]
}
}
JS

RESET_STYLE_SCRIPT = <<~'JS'
var el = arguments[0];
if (el.hasOwnProperty('capybara_style_cache')) {
el.style.cssText = el.capybara_style_cache;
delete el.capybara_style_cache;
if (this.hasOwnProperty('capybara_style_cache')) {
this.style.cssText = this.capybara_style_cache;
delete this.capybara_style_cache;
}
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 }).
map(function(el){ return { "value": el.value, "label": el.label} })
JS
Expand Down

0 comments on commit cacbf86

Please sign in to comment.