Capybara is a software testing tool 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, which has numerous libraries and frameworks - like Ruby on Rails - that make pre-written code accessible to you, making your testing task easier. Scripts written in Gherkin can be mapped to Capybara code for behavior driven development of projects.
This article describes some Capybara methods that automate app testing for simulating user input. Some methods help “set” the input and others “get” (verify) the page elements. Let’s look at some Gherkin scenarios for a user to update their identity details.
Filling in text fields
Let us look into an example for filling in the 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 look at the various methods used :
- The visit (line 2) method navigates to the given URL and page.should have_content (line 3) will check if the text is visible on the page. This method waits for the element to be visible for the default wait time which is 2 seconds in Capybara.
- page.find_link (line 7) will find the link to update the user identity on the page and the element.trigger (line 8) method will ‘click’ on it.
- The find_field method (line 12) finds the field TextboxName and passes it to the variable ‘element’.
- find_button (line 22) will find the element ‘Save’ on the page and the trigger method will ‘click’ on it.
And what about the ‘get’ and ‘set’ methods used here?
Get method: The .value method gets the value and stores it in the variable ‘textBoxValue’.
textBoxValue = element.value |
Set method: fill_in locates the text field by its name, ID or label text and fills in the value.
1 |
When(/^Joe enters "([^"]*)" in "([^"]*)" text box$/) do |value, textBoxName| |
Choose a radio button
We can use a similar scenario to see how to code for user input through a radio button:
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? (line 14) will ‘get’(verify) if the page has a radio button with the given value, id or label.
1 |
Then(/^Joe Molly verifies that "([^"]*)" radio option is selected for gender$/) do |radioButtonName| |
Set method: choose (line 18) will find the radio button via its name, id or label and mark it as checked.
1 |
When(/^Joe Molly selects "([^"]*)" radio option for gender$/) do |radioOptionName| |
'Check' a check box
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? (line 12) will verify if the page has a check box with the given value, id or label.
1 |
Then(/^Joe Molly verifies that "([^"]*)" checkbox is checked$/) do |checkBoxName| |
Set method : uncheck (line 16) will remove the check from the checkbox ‘checkBoxName’.
1 |
When(/^Joe Molly un-checks "([^"]*)" checkbox$/) do |checkBoxName| |
Select an option from a drop down
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 (line 17) gets the value of the previously selected option (the country name “United States”) and stores it in the variable dropDownValue.
dropDownValue = element.value
Set method: page.select(dropDown Value :from => dropDownName) sets the new value (the country name “Canada”) from the variable dropDownName in the drop down box.
page.select dropDownValue, :from => dropDownName
Some of the Capybara finder methods like select, fill_in, check and choose, which I have mentioned above, retry searching for the element if they can't find it at the first attempt. This prolongs the running time of the test suite, creating performance issues.
You can read more about Capybara methods and functions here.
Capybara as a testing tool succeeds in providing an easily usable framework, combining the ease of the Gherkin script and the powerful features of Selenium.
For more on BDD and Gherkin syntax, read the article “Why go for BDD?”