From 4febe3a47397b635d278f5584c3dc3c67e6f1382 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Mon, 9 Jan 2017 12:50:56 -0800 Subject: [PATCH] reset the style after attaching file --- lib/capybara/node/actions.rb | 24 +++++++++++++++++-- lib/capybara/spec/session/attach_file_spec.rb | 6 +++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/capybara/node/actions.rb b/lib/capybara/node/actions.rb index 0d6933c3..2fc2ec37 100644 --- a/lib/capybara/node/actions.rb +++ b/lib/capybara/node/actions.rb @@ -244,7 +244,11 @@ module Capybara ff = find(:file_field, locator, options.merge({visible: :all})) _update_style(ff, style) if ff.visible? - ff.set(path) + begin + ff.set(path) + ensure + _reset_style(ff) + end else raise ExpectationNotMet, "The style changes in :make_visible did not make the file input visible" end @@ -257,6 +261,7 @@ module Capybara def _update_style(element, style) script = <<-JS var el = arguments[0]; + el.capybara_style_cache = el.style.cssText; var css = arguments[1]; for (var prop in css){ if (css.hasOwnProperty(prop)) { @@ -267,10 +272,25 @@ module Capybara begin session.execute_script(script, element, style) rescue Capybara::NotSupportedByDriverError - warn "The :style 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) + script = <<-JS + var el = arguments[0]; + if (el.hasOwnProperty('capybara_style_cache')) { + el.style=el.capybara_style_cache; + delete el.capybara_style_cache; + } + JS + begin + session.execute_script(script, element) + rescue + end + end + + def _check_with_label(selector, checked, locator, options) locator, options = nil, locator if locator.is_a? Hash allow_label_click = options.delete(:allow_label_click) { Capybara.automatic_label_click } diff --git a/lib/capybara/spec/session/attach_file_spec.rb b/lib/capybara/spec/session/attach_file_spec.rb index d0a9c062..1b91af36 100644 --- a/lib/capybara/spec/session/attach_file_spec.rb +++ b/lib/capybara/spec/session/attach_file_spec.rb @@ -130,5 +130,11 @@ Capybara::SpecHelper.spec "#attach_file" do @session.attach_file("hidden_file", __FILE__, make_visible: { color: 'red' }) }.to raise_error(Capybara::ExpectationNotMet) end + + it "resets the style when done" do + @session.visit('/with_js') + @session.attach_file("hidden_file", __FILE__, make_visible: true) + expect(@session.evaluate_script("arguments[0].style.display", @session.find(:css, '#hidden_file', visible: :all))).to eq 'none' + end end end