Module: Capybara::Node::Actions

Included in:
Base
Defined in:
lib/capybara/node/actions.rb

Instance Method Summary collapse

Instance Method Details

#attach_file([locator], paths, **options) ⇒ Capybara::Node::Element

Find a descendant 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. 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. If no locator is passed this will match self or a descendant.

# will attach file to a descendant file input element that has a name, id, or label_text matching 'My File'
page.attach_file('My File', '/path/to/file.png')

# will attach file to el if it's a file input element
el.attach_file('/path/to/file.png')

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time find will wait is controlled through Capybara.default_max_wait_time

Options Hash (**options):

  • wait (false, Numeric) — default: Capybara.default_max_wait_time

    Maximum time to wait for matching element to appear.

  • match (Symbol) — default: Capybara.match

    The matching strategy to use (:one, :first, :prefer_exact, :smart).

  • exact (Boolean) — default: Capybara.exact

    Match the exact label name/contents or accept a partial match.

  • multiple (Boolean)

    Match field which allows multiple file selection

  • id (String, Regexp)

    Match fields that match the id attribute

  • name (String)

    Match fields that match the name attribute

  • class (String, Array<String>, Regexp)

    Match fields that match the class(es) provided

  • 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)



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/capybara/node/actions.rb', line 257

def attach_file(locator = nil, paths, make_visible: nil, **options) # rubocop:disable Style/OptionalArguments
  Array(paths).each do |path|
    raise Capybara::FileNotFound, "cannot attach file, #{path} does not exist" unless File.exist?(path.to_s)
  end
  options[:allow_self] = true if locator.nil?
  # 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(paths) }
  else
    find(:file_field, locator, options).set(paths)
  end
end

#check([locator], **options) ⇒ Capybara::Node::Element

Find a descendant check box and mark it as checked. The check box can be found via name, id or label text. If no locator is provided this will match against self or a descendant.

# will check a descendant checkbox with a name, id, or label text matching 'German'
page.check('German')

# will check `el` if it's a checkbox element
el.check()

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time find will wait is controlled through Capybara.default_max_wait_time

Options Hash (**options):

  • option (String)

    Value of the checkbox to select

  • id (String, Regexp)

    Match fields that match the id attribute

  • name (String)

    Match fields that match the name attribute

  • class (String, Array<String>, Regexp)

    Match fields that match the class(es) provided

  • allow_label_click (Boolean) — default: Capybara.automatic_label_click

    Attempt to click the label to toggle state if element is non-visible.

  • wait (false, Numeric) — default: Capybara.default_max_wait_time

    Maximum time to wait for matching element to appear.



147
148
149
# File 'lib/capybara/node/actions.rb', line 147

def check(locator = nil, **options)
  _check_with_label(:checkbox, true, locator, options)
end

#choose([locator], **options) ⇒ Capybara::Node::Element

Find a descendant radio button and mark it as checked. The radio button can be found via name, id or label text. If no locator is provided this will match against self or a descendant.

# will choose a descendant radio button with a name, id, or label text matching 'Male'
page.choose('Male')

# will choose `el` if it's a radio button element
el.choose()

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time find will wait is controlled through Capybara.default_max_wait_time

Options Hash (**options):

  • option (String)

    Value of the radio_button to choose

  • id (String, Regexp)

    Match fields that match the id attribute

  • name (String)

    Match fields that match the name attribute

  • class (String, Array<String>, Regexp)

    Match fields that match the class(es) provided

  • wait (false, Numeric) — default: Capybara.default_max_wait_time

    Maximum time to wait for matching element to appear.

  • allow_label_click (Boolean) — default: Capybara.automatic_label_click

    Attempt to click the label to toggle state if element is non-visible.



119
120
121
# File 'lib/capybara/node/actions.rb', line 119

def choose(locator = nil, **options)
  _check_with_label(:radio_button, true, locator, options)
end

#click_button([locator], **options) ⇒ Capybara::Node::Element

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

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time find will wait is controlled through Capybara.default_max_wait_time

Options Hash (**options):

  • wait (false, Numeric) — default: Capybara.default_max_wait_time

    Maximum time to wait for matching element to appear.



56
57
58
# File 'lib/capybara/node/actions.rb', line 56

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

Finds a link by id, Capybara.test_id attribute, text or title and clicks it. Also looks at image alt text inside the link.

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time find will wait is controlled through Capybara.default_max_wait_time

Options Hash (**options):

  • wait (false, Numeric) — default: Capybara.default_max_wait_time

    Maximum time to wait for matching element to appear.



40
41
42
# File 'lib/capybara/node/actions.rb', line 40

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

Finds a button or link and clicks it. See #click_button and #click_link for what locator will match against for each type of element

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time find will wait is controlled through Capybara.default_max_wait_time

Options Hash (**options):

  • wait (false, Numeric) — default: Capybara.default_max_wait_time

    Maximum time to wait for matching element to appear.



24
25
26
# File 'lib/capybara/node/actions.rb', line 24

def click_link_or_button(locator = nil, **options)
  find(:link_or_button, locator, options).click
end

#fill_in([locator], with: , **options) ⇒ Capybara::Node::Element

Locate a text field or text area and fill it in with the given text The field can be found via its name, id, Capybara.test_id attribute, or label text. If no locator is provided will operate on self or a descendant

# will fill in a descendant fillable field with name, id, or label text matching 'Name'
page.fill_in 'Name', with: 'Bob'

# will fill in `el` if it's a fillable field
el.fill_in with: 'Tom'

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time find will wait is controlled through Capybara.default_max_wait_time

Options Hash (**options):

  • wait (false, Numeric) — default: Capybara.default_max_wait_time

    Maximum time to wait for matching element to appear.

  • currently_with (String)

    The current value property of the field to fill in

  • multiple (Boolean)

    Match fields that can have multiple values?

  • id (String, Regexp)

    Match fields that match the id attribute

  • name (String)

    Match fields that match the name attribute

  • placeholder (String)

    Match fields that match the placeholder attribute

  • class (String, Array<String>, Regexp)

    Match fields that match the class(es) provided

  • fill_options (Hash)

    Driver specific options regarding how to fill fields (Defaults come from Capybara.default_set_options)



87
88
89
90
91
# File 'lib/capybara/node/actions.rb', line 87

def fill_in(locator = nil, with:, currently_with: nil, fill_options: {}, **find_options)
  find_options[:with] = currently_with if currently_with
  find_options[:allow_self] = true if locator.nil?
  find(:fillable_field, locator, find_options).set(with, fill_options)
end

#select(value = nil, from: nil, **options) ⇒ Capybara::Node::Element

If `:from` option is present, `select` finds a select box, or text input with associated datalist, on the page and selects a particular option from it. Otherwise it finds an option inside current scope and selects it. If the select box is a multiple select, select can be called multiple times to select more than one option. The select box can be found via its name, id or label text. The option can be found by its text.

page.select 'March', from: 'Month'

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time find will wait is controlled through Capybara.default_max_wait_time

Options Hash (**options):

  • wait (false, Numeric) — default: Capybara.default_max_wait_time

    Maximum time to wait for matching element to appear.



197
198
199
200
201
202
203
204
205
# File 'lib/capybara/node/actions.rb', line 197

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')
    select_datalist_option(el, value)
  else
    el.find(:option, value, options).select_option
  end
end

#uncheck([locator], **options) ⇒ Capybara::Node::Element

Find a descendant check box and uncheck it. The check box can be found via name, id or label text. If no locator is provided this will match against self or a descendant.

# will uncheck a descendant checkbox with a name, id, or label text matching 'German'
page.uncheck('German')

# will uncheck `el` if it's a checkbox element
el.uncheck()

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time find will wait is controlled through Capybara.default_max_wait_time

Options Hash (**options):

  • option (String)

    Value of the checkbox to deselect

  • id (String, Regexp)

    Match fields that match the id attribute

  • name (String)

    Match fields that match the name attribute

  • class (String, Array<String>, Regexp)

    Match fields that match the class(es) provided

  • allow_label_click (Boolean) — default: Capybara.automatic_label_click

    Attempt to click the label to toggle state if element is non-visible.

  • wait (false, Numeric) — default: Capybara.default_max_wait_time

    Maximum time to wait for matching element to appear.



175
176
177
# File 'lib/capybara/node/actions.rb', line 175

def uncheck(locator = nil, **options)
  _check_with_label(:checkbox, false, locator, options)
end

#unselect(value = nil, from: nil, **options) ⇒ Capybara::Node::Element

Find a select box on the page and unselect a particular option from it. If the select box is a multiple select, unselect can be called multiple times to unselect more than one option. The select box can be found via its name, id or label text.

page.unselect 'March', from: 'Month'

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time find will wait is controlled through Capybara.default_max_wait_time

Options Hash (**options):

  • wait (false, Numeric) — default: Capybara.default_max_wait_time

    Maximum time to wait for matching element to appear.



223
224
225
226
# File 'lib/capybara/node/actions.rb', line 223

def unselect(value = nil, from: nil, **options)
  scope = from ? find(:select, from, options) : self
  scope.find(:option, value, options).unselect_option
end