module Capybara::Node::Actions

def _check_with_label(selector, checked, locator, allow_label_click: session_options.automatic_label_click, **options)

def _check_with_label(selector, checked, locator, allow_label_click: session_options.automatic_label_click, **options)
  synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do
    begin
      el = find(selector, locator, options)
      el.set(checked)
    rescue => e
      raise unless allow_label_click && catch_error?(e)
      begin
        el ||= find(selector, locator, options.merge(visible: :all))
        find(:label, for: el, visible: true).click unless el.checked? == checked
      rescue # swallow extra errors - raise original
        raise e
      end
    end
  end
end

def _reset_style(element)

def _reset_style(element)
  session.execute_script(RESET_STYLE_SCRIPT, element)
rescue # swallow extra errors
end

def _update_style(element, style)

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

def attach_file(locator = nil, path, make_visible: nil, **options) # rubocop:disable Style/OptionalArguments

Returns:
  • (Capybara::Node::Element) - The file field element

Options Hash: (**options)
  • make_visible (true, Hash) -- A Hash of CSS styles to change before attempting to attach the file, if `true` { opacity: 1, display: 'block', visibility: 'visible' } is used (may not be supported by all drivers)
  • :class (String, Array) -- Match fields that match the class(es) provided
  • name (String) -- Match fields that match the name attribute
  • id (String) -- Match fields that match the id attribute
  • multiple (Boolean) -- Match field which allows multiple file selection
  • exact (Boolean) -- Match the exact label name/contents or accept a partial match.
  • match (Symbol) -- The matching strategy to use (:one, :first, :prefer_exact, :smart).

Parameters:
  • path (String) -- The path of the file that will be attached, or an array of paths
  • locator (String) -- Which field to attach the file to
def attach_file(locator = nil, path, make_visible: nil, **options) # rubocop:disable Style/OptionalArguments
  Array(path).each do |p|
    raise Capybara::FileNotFound, "cannot attach file, #{p} does not exist" unless File.exist?(p.to_s)
  end
  # Allow user to update the CSS style of the file input since they are so often hidden on a page
  if make_visible
    ff = find(:file_field, locator, options.merge(visible: :all))
    while_visible(ff, make_visible) { |el| el.set(path) }
  else
    find(:file_field, locator, options).set(path)
  end
end

def check(locator, **options)

Returns:
  • (Capybara::Node::Element) - The element checked or the label clicked

Options Hash: (**options)
  • :class (String, Array) -- Match fields that match the class(es) provided
  • name (String) -- Match fields that match the name attribute
  • id (String) -- Match fields that match the id attribute
  • :option (String) -- Value of the checkbox to select

Parameters:
  • locator (String) -- Which check box to check

Overloads:
  • check([locator], options)
def check(locator, **options)
  _check_with_label(:checkbox, true, locator, **options)
end

def choose(locator = nil, **options)

Returns:
  • (Capybara::Node::Element) - The element chosen or the label clicked

Options Hash: (**options)
  • :class (String, Array) -- Match fields that match the class(es) provided
  • :name (String) -- Match fields that match the name attribute
  • :id (String) -- Match fields that match the id attribute
  • :option (String) -- Value of the radio_button to choose

Parameters:
  • locator (String) -- Which radio button to choose

Overloads:
  • choose([locator], options)
def choose(locator = nil, **options)
  _check_with_label(:radio_button, true, locator, **options)
end

def click_button(locator = nil, **options)

Returns:
  • (Capybara::Node::Element) - The element clicked

Parameters:
  • options () -- See {Capybara::Node::Finders#find_button}
  • locator (String) -- Which button to find

Overloads:
  • click_button([locator], options)
def click_button(locator = nil, **options)
  find(:button, locator, options).click
end

def click_link(locator = nil, **options)

Returns:
  • (Capybara::Node::Element) - The element clicked

Parameters:
  • options () -- See {Capybara::Node::Finders#find_link}
  • locator (String) -- text, id, title or nested image's alt attribute

Overloads:
  • click_link([locator], options)
def click_link(locator = nil, **options)
  find(:link, locator, options).click
end

def click_link_or_button(locator = nil, **options)

Returns:
  • (Capybara::Node::Element) - The element clicked

Parameters:
  • locator (String) -- See {Capybara::Node::Actions#click_button} and {Capybara::Node::Actions#click_link}

Overloads:
  • click_link_or_button([locator], options)

Options Hash: (**options)
  • wait (false, Numeric) -- Maximum time to wait for matching element to appear.
def click_link_or_button(locator = nil, **options)
  find(:link_or_button, locator, options).click
end

def fill_in(locator = nil, with:, fill_options: {}, **options)

Returns:
  • (Capybara::Node::Element) - The element filled_in

Options Hash: (**options)
  • :class (String, Array) -- Match fields that match the class(es) provided
  • :placeholder (String) -- Match fields that match the placeholder attribute
  • :name (String) -- Match fields that match the name attribute
  • :id (String) -- Match fields that match the id attribute
  • :multiple (Boolean) -- Match fields that can have multiple values?
  • :currently_with (String) -- The current value property of the field to fill in
  • :fill_options (Hash) -- Driver specific options regarding how to fill fields
  • :with (String) -- The value to fill in - required

Parameters:
  • options (Hash) --
  • locator (String) -- Which field to fill in

Overloads:
  • fill_in([locator], options={})
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)
end

def select(value = nil, from: nil, **options)

Returns:
  • (Capybara::Node::Element) - The option element selected

Options Hash: (**options)
  • :from (String) -- The id, name or label of the select box

Parameters:
  • value (String) -- Which option to select
def select(value = nil, from: nil, **options)
  scope = from ? find(:select, from, options) : self
  scope.find(:option, value, options).select_option
end

def uncheck(locator = nil, **options)

Returns:
  • (Capybara::Node::Element) - The element unchecked or the label clicked

Options Hash: (**options)
  • :class (String, Array) -- Match fields that match the class(es) provided
  • name (String) -- Match fields that match the name attribute
  • id (String) -- Match fields that match the id attribute
  • :option (String) -- Value of the checkbox to deselect

Parameters:
  • locator (String) -- Which check box to uncheck

Overloads:
  • uncheck([locator], options)
def uncheck(locator = nil, **options)
  _check_with_label(:checkbox, false, locator, **options)
end

def unselect(value = nil, from: nil, **options)

Returns:
  • (Capybara::Node::Element) - The option element unselected

Parameters:
  • options (Hash{:from => String}) -- The id, name or label of the select box
  • value (String) -- Which option to unselect
def unselect(value = nil, from: nil, **options)
  scope = from ? find(:select, from, options) : self
  scope.find(:option, value, options).unselect_option
end

def while_visible(element, visible_css)

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?
  begin
    yield element
  ensure
    _reset_style(element)
  end
end