lib/capybara/node/actions.rb in capybara-3.3.1 vs lib/capybara/node/actions.rb in capybara-3.4.0

- old
+ new

@@ -25,17 +25,17 @@ end alias_method :click_on, :click_link_or_button ## # - # Finds a link by id, text or title and clicks it. Also looks at image + # Finds a link by id, Capybara.test_id attribute, text or title and clicks it. Also looks at image # alt text inside the link. # # @macro waiting_behavior # # @overload click_link([locator], options) - # @param [String] locator text, id, title or nested image's alt attribute + # @param [String] locator text, id, Capybara.test_id attribute, title or nested image's alt attribute # @param options See {Capybara::Node::Finders#find_link} # # @return [Capybara::Node::Element] The element clicked def click_link(locator = nil, **options) find(:link, locator, options).click @@ -43,11 +43,11 @@ ## # # Finds a button on the page and clicks it. # This can be any \<input> element of type submit, reset, image, button or it can be a - # \<button> element. All buttons can be found by their id, value, or title. \<button> elements can also be found + # \<button> element. All buttons can be found by their id, Capybara.test_id attribute, value, or title. \<button> elements can also be found # by their text content, and image \<input> elements by their alt attribute # # @macro waiting_behavior # # @overload click_button([locator], **options) @@ -59,11 +59,11 @@ end ## # # Locate a text field or text area and fill it in with the given text - # The field can be found via its name, id or label text. + # The field can be found via its name, id, Capybara.test_id attribute, or label text. # # page.fill_in 'Name', with: 'Bob' # # # @overload fill_in([locator], with:, **options) @@ -78,13 +78,13 @@ # @option options [String] placeholder Match fields that match the placeholder attribute # @option options [String, Array<String>] class Match fields that match the class(es) provided # @option options [Hash] fill_options Driver specific options regarding how to fill fields (Defaults come from Capybara.default_set_options) # # @return [Capybara::Node::Element] The element filled_in - def fill_in(locator = nil, with:, fill_options: {}, **options) - options[:with] = options.delete(:currently_with) if options.key?(:currently_with) - find(:fillable_field, locator, options).set(with, fill_options) + def fill_in(locator = nil, with:, currently_with: nil, fill_options: {}, **find_options) + find_options[:with] = currently_with if currently_with + find(:fillable_field, locator, find_options).set(with, fill_options) end # @!macro label_click # @option options [Boolean] allow_label_click (Capybara.automatic_label_click) Attempt to click the label to toggle state if element is non-visible. @@ -168,17 +168,17 @@ # page.select 'March', from: 'Month' # # @macro waiting_behavior # # @param value [String] Which option to select - # @param from: [String] The id, name or label of the select box + # @param from: [String] The id, Capybara.test_id atrtribute, name or label of the select box # # @return [Capybara::Node::Element] The option element selected def select(value = nil, from: nil, **options) el = from ? find_select_or_datalist_input(from, options) : self - if el.respond_to?(:tag_name) && (el.tag_name == "input") + if el.respond_to?(:tag_name) && (el.tag_name == 'input') select_datalist_option(el, value) else el.find(:option, value, options).select_option end end @@ -192,22 +192,24 @@ # page.unselect 'March', from: 'Month' # # @macro waiting_behavior # # @param value [String] Which option to unselect - # @param from: [String] The id, name or label of the select box + # @param from: [String] The id, Capybara.test_id attribute, name or label of the select box # # @return [Capybara::Node::Element] The option element unselected def unselect(value = nil, from: nil, **options) scope = from ? find(:select, from, options) : self scope.find(:option, value, options).unselect_option end ## # # Find a file field on the page and attach a file given its path. The file field can - # be found via its name, id or label text. + # be found via its name, id or label text. In the case of the file field being hidden for + # styling reasons the `make_visible` option can be used to temporarily change the CSS of + # the file field, attach the file, and then revert the CSS back to original. # # page.attach_file(locator, '/path/to/file.png') # # @overload attach_file([locator], path, **options) # @macro waiting_behavior @@ -254,36 +256,34 @@ end end def select_datalist_option(input, value) 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 - raise ::Capybara::ElementNotFound, %(Unable to find datalist option "#{value}") - end + option = datalist_options.find { |o| o.values_at('value', 'label').include?(value) } + raise ::Capybara::ElementNotFound, %(Unable to find datalist option "#{value}") unless option + input.set(option['value']) rescue ::Capybara::NotSupportedByDriverError # Implement for drivers that don't support JS datalist = find(:xpath, XPath.descendant(:datalist)[XPath.attr(:id) == input[:list]], visible: false) option = datalist.find(:datalist_option, value, disabled: false) input.set(option.value) end def while_visible(element, visible_css) visible_css = { opacity: 1, display: 'block', visibility: 'visible' } if visible_css == true _update_style(element, visible_css) - raise ExpectationNotMet, "The style changes in :make_visible did not make the file input visible" unless element.visible? + raise ExpectationNotMet, 'The style changes in :make_visible did not make the file input visible' unless element.visible? begin yield element ensure _reset_style(element) end end def _update_style(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" + warn 'The :make_visible option is not supported by the current driver - ignoring' end def _reset_style(element) element.execute_script(RESET_STYLE_SCRIPT) rescue StandardError # rubocop:disable Lint/HandleExceptions swallow extra errors