feature: add a default_set_options configuration

This commit is contained in:
Champier Cyril 2018-05-18 17:22:14 +02:00
parent b1f4709c6e
commit ff74e249f0
6 changed files with 22 additions and 2 deletions

View File

@ -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

View File

@ -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|

View File

@ -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

View File

@ -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

View File

@ -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')

View File

@ -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