Capybara is one of the widely used automated testing tools that acts as an effective wrapper for web drivers like Selenium, Webkit, Rack Test and Poltergeist. The customized methods in the Capybara testing tool are implemented in Ruby. Additionally, Ruby has numerous libraries and frameworks – like Ruby on Rails – that make pre-written code accessible, making your testing task easier.
This guide also serves as one of the first steps in Capybara for web application test automation, especially for teams adopting Ruby Capybara test automation in behavior-driven development environments. Scripts written in Gherkin can be mapped to Capybara code for behavior-driven development of projects.
In this article, we describe several Capybara methods that support automated testing tools by simulating user input. Some methods help “set” the input, while others “get” (verify) page elements. Let’s now look at some Gherkin scenarios for a user updating identity details.
Filling in Text Fields
Let us look at an example for filling in a text field using Capybara:
1 |
Scenario: Verify existing data and edit employee info on Edit Employee Profile page |
The underlying Capybara testing code for automating the scenario.
1 | Given(/^Joe Molly is on Edit Employee Profile page$/) do When(/^Joe Molly clicks on Identity tab$/) do And(/^Joe Molly should see “([^”]*)” in “([^“]*)” text box$/) do |value, textBoxName| When(/^Joe enters “([^”]*)” in “([^“]*)” text box$/) do |value, textBoxName| And(/^Joe Molly clicks Save button$/) do Then(/^Joe Molly verifies message “([^”]*)“ on Edit Employee Profile page$/) do |messageText| |
Let’s take a closer look at the methods used in Ruby Capybara test automation.
The visit (line 2) method navigates to the given URL. Then, page.should have_content (line 3) checks whether the text is visible on the page. By default, this method waits up to 2 seconds for the element to appear.
Next, page.find_link (line 7) finds the Identity link, and element.trigger (line 8) clicks on it. Similarly, find_field (line 12) finds the textbox and assigns it to the variable ‘element’. Finally, find_button (line 22) finds the ‘Save’ button, and trigger clicks it.
Get and Set Methods
Get method: The .value method gets the value and stores it in the variable ‘textBoxValue’.
textBoxValue = element.value |
1 |
When(/^Joe enters "([^"]*)" in "([^"]*)" text box$/) do |value, textBoxName| |
Choosing a Radio Button
We can use a similar scenario to handle radio button input.
1 | Scenario: Verify employee's gender is shown correctly in Edit Employee Profile page |
What the code looks like.
1 |
Step definition: Given(/^Joe Molly is on Edit Employee Profile page$/) do When(/^Joe Molly clicks on Identity tab$/) do Then(/^Joe Molly verifies that “([^”]*)” radio option is selected for gender$/) do |radioButtonName| When(/^Joe Molly selects “([^“]*)” radio option for gender$/) do |radioOptionName| And(/^Joe Molly clicks Save button$/) do Then(/^Joe Molly verifies message “([^”]*)“ on Edit Employee Profile page$/) do |messageText| |
Get method: page.has_checked_field? verifies whether the radio button is selected.
1 | Then(/^Joe Molly verifies that "([^"]*)" radio option is selected for gender$/) do |radioButtonName| |
Set method: choose finds the radio button and marks it as checked.
1 | When(/^Joe Molly selects "([^"]*)" radio option for gender$/) do |radioOptionName| |
The Test Automation Playbook
Read The Test Automation Playbook to build a future-ready QA strategy with insights on tools, tactics, and ROI.
In Ruby Capybara test automation, page.has_checked_field? verifies whether the radio button is selected, while choose marks it as checked.
Checking a Checkbox
The check box for the “Do Not Contact” option is to be unchecked:
1 |
Scenario: Verify employee un-checks Do Not Contact option in Edit Employee Profile page |
The code for this scenario
1 |
Given(/^Joe Molly is on Edit Employee Profile page$/) do When(/^Joe Molly clicks on Identity tab$/) do Then(/^Joe Molly verifies that “([^”]*)” checkbox is checked$/) do |checkBoxName| When(/^Joe Molly un-checks “([^“]*)” checkbox$/) do |checkBoxName| And(/^Joe Molly clicks Save button$/) do Then(/^Joe Molly verifies message “([^”]*)“ on Edit Employee Profile page$/) do |messageText| |
Get method: page.has_checked_field? verifies whether the checkbox is selected.
1 | Then(/^Joe Molly verifies that "([^"]*)" checkbox is checked$/) do |checkBoxName| |
Set method: uncheck removes the check from the checkbox.
1 | When(/^Joe Molly un-checks "([^"]*)" checkbox$/) do |checkBoxName| |
Selecting from a Drop-Down Menu
1 |
Scenario: Verify employee's country is shown correctly in Edit Employee Profile page |
The code for selecting the country name from a drop down box
1 |
Given(/^Joe Molly is on Edit Employee Profile page$/) do When(/^Joe Molly clicks on Identity tab$/) do Then(/^Joe Molly verifies that “([^”]*)” is selected in “([^“]*)” drop down$/) do |value, dropDownName| When(/^Joe Molly selects “([^”]*)” from “([^“]*)” drop down$/) do |dropDownValue, dropDownName| |
Get method: value gets the selected option and stores it in dropDownValue.
dropDownValue = element.value
page.select dropDownValue, :from => dropDownName
The .value method retrieves the selected option, while page.select sets the new value in the drop-down box. These examples demonstrate practical first steps in Capybara for web application test automation.
Performance Considerations in Automated Testing Tools
Some Capybara finder methods like select, fill_in, check, and choose retry searching for elements if they are not found immediately. As a result, this can increase test execution time and cause performance issues.
Overall, Capybara stands out among automated testing tools because it provides an easy-to-use framework. It combines the simplicity of Gherkin scripts with the powerful features of Selenium, making it ideal for Ruby Capybara test automation projects.
For more on BDD and Gherkin syntax, read the article “Why go for BDD?”
Frequently Asked Questions (FAQs)
What are automated testing tools?
Automated testing tools are software solutions that simulate user interactions and validate application functionality without manual intervention.
Why is Capybara popular in Ruby Capybara test automation?
Capybara integrates smoothly with Ruby frameworks like Ruby on Rails and supports behavior-driven development using Gherkin syntax.
What are the first steps in Capybara for web application test automation?
The first steps in Capybara for web application test automation include setting up the environment, writing Gherkin scenarios, and mapping them to Capybara step definitions.
How does Capybara simulate user input?
Capybara uses methods like fill_in, choose, select, check, and uncheck to simulate real user interactions.
Can Capybara work with Selenium?
Yes, Capybara acts as a wrapper around Selenium and other drivers to execute browser-based tests.
What are the advantages of using automated testing tools in web applications?
They improve efficiency, reduce human error, speed up regression testing, and ensure consistent test execution.