module Capybara::Node::Actions

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