Capybara Reference

Clicking buttons

<button data-testid="submitButton"></button>

Here are 4 different ways to click on the above button element:

  • click_button(‘submitButton’)
  • click_on(‘submitButton’)
  • find(:button, ‘submitButton’).click
  • find(:element, ‘data-testid’: ‘submitButton’).click

Clicking links

<a href = "/sample-path/">click here</a>

You can click on a link by using the display text if it doesn’t have an ID:

  • click_link(‘click here’)

Inputting text

<textarea data-testid="address"></textarea>
  • find(:element, ‘data-testid’:’address’).send_keys(‘my input text’)
<input id="fromDate">
  • fill_in ‘fromDate’, :with => ’12/12/2025 00:00′
  • find(:field, ‘fromDate’).send_keys ’12/12/2025 00:00′
<input name="recipient">
  • fill_in ‘recipient’, :with => ‘recipient name’

Checking and unchecking checkboxes

<input type="checkbox" id="billed">
  • check(‘billed’)
  • uncheck(‘billed’)

Choosing radio buttons

<input type="radio" data-testid="optionNo">
  • choose(‘optionNo’)

Uploading files

<input type="file" id="fileupload">
  • page.find(‘input[type=”file”]’, visible: false).attach_file(‘documents/samplefile.pdf’, makevisible: true)