From ff74e249f08be57b25f35001282700c48118e8ca Mon Sep 17 00:00:00 2001 From: Champier Cyril Date: Fri, 18 May 2018 17:22:14 +0200 Subject: [PATCH] feature: add a default_set_options configuration --- History.md | 7 +++++++ lib/capybara.rb | 2 ++ lib/capybara/node/element.rb | 3 ++- lib/capybara/session/config.rb | 4 +++- lib/capybara/spec/session/node_spec.rb | 7 +++++++ lib/capybara/spec/spec_helper.rb | 1 + 6 files changed, 22 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 16477bf7..fa321ce9 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,10 @@ +# Version 3.2.0 +Release date: unreleased + +### Added + +* New global configuration `default_set_options` used in `Capybara::Node::Element#set` as default `options` hash + # Version 3.1.0 Release date: 2018-05-10 diff --git a/lib/capybara.rb b/lib/capybara.rb index badb8766..a943a87e 100644 --- a/lib/capybara.rb +++ b/lib/capybara.rb @@ -82,6 +82,7 @@ module Capybara # [reuse_server = Boolean] Reuse the server thread between multiple sessions using the same app object (Default: true) # [threadsafe = Boolean] Whether sessions can be configured individually (Default: false) # [server = Symbol] The name of the registered server to use when running the app under test (Default: :webrick) + # [default_set_options = Hash] The default options passed to Node::set (Default: {}) # # === DSL Options # @@ -479,6 +480,7 @@ Capybara.configure do |config| config.automatic_label_click = false config.enable_aria_label = false config.reuse_server = true + config.default_set_options = {} end Capybara.register_driver :rack_test do |app| diff --git a/lib/capybara/node/element.rb b/lib/capybara/node/element.rb index dee38467..de960a72 100644 --- a/lib/capybara/node/element.rb +++ b/lib/capybara/node/element.rb @@ -84,11 +84,12 @@ module Capybara # Set the value of the form element to the given value. # # @param [String] value The new value - # @param [Hash{}] options Driver specific options for how to set the value + # @param [Hash{}] options Driver specific options for how to set the value. Take default values from {Capybara#default_set_options} # # @return [Capybara::Node::Element] The element def set(value, **options) raise Capybara::ReadOnlyElementError, "Attempt to set readonly element with value: #{value}" if readonly? + options = session_options.default_set_options.merge(options) synchronize { base.set(value, options) } self end diff --git a/lib/capybara/session/config.rb b/lib/capybara/session/config.rb index 6f28c206..08ff562e 100644 --- a/lib/capybara/session/config.rb +++ b/lib/capybara/session/config.rb @@ -7,7 +7,7 @@ module Capybara OPTIONS = %i[always_include_port run_server default_selector default_max_wait_time ignore_hidden_elements automatic_reload match exact exact_text raise_server_errors visible_text_only automatic_label_click enable_aria_label save_path asset_host default_host app_host - server_host server_port server_errors].freeze + server_host server_port server_errors default_set_options].freeze attr_accessor(*OPTIONS) @@ -50,6 +50,8 @@ module Capybara # See {Capybara.configure} # @!method server_errors # See {Capybara.configure} + # @!method default_set_options + # See {Capybara.configure} remove_method :server_host diff --git a/lib/capybara/spec/session/node_spec.rb b/lib/capybara/spec/session/node_spec.rb index 0ea41368..1aced418 100644 --- a/lib/capybara/spec/session/node_spec.rb +++ b/lib/capybara/spec/session/node_spec.rb @@ -102,6 +102,13 @@ Capybara::SpecHelper.spec "node" do expect { @session.first('//textarea[@readonly]').set('changed') }.to raise_error(Capybara::ReadOnlyElementError) end + it 'should use global default options', requires: [:js] do + Capybara.default_set_options = { clear: :backspace } + element = @session.first('//input') + expect(element.base).to receive(:set).with('gorilla', clear: :backspace) + element.set('gorilla') + end + context "with a contenteditable element", requires: [:js] do it 'should allow me to change the contents' do @session.visit('/with_js') diff --git a/lib/capybara/spec/spec_helper.rb b/lib/capybara/spec/spec_helper.rb index 44dfad5d..3a80e23e 100644 --- a/lib/capybara/spec/spec_helper.rb +++ b/lib/capybara/spec/spec_helper.rb @@ -29,6 +29,7 @@ module Capybara Capybara.visible_text_only = false Capybara.match = :smart Capybara.enable_aria_label = false + Capybara.default_set_options = {} reset_threadsafe end