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:
The underlying Capybara testing code for automating the scenario
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’.
Set method: fill_in locates the text field by its name, ID or label text and fills in the value.
Choose a radio button
We can use a similar scenario to see how to code for user input through a radio button:
What the code looks like
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.
Set method: choose (line 18) will find the radio button via its name, id or label and mark it as checked.
'Check' a check box
The check box for the “Do Not Contact” option is to be unchecked:
The code for this scenario
Get method: page.has_checked_field? (line 12) will verify if the page has a check box with the given value, id or label.
Set method : uncheck (line 16) will remove the check from the checkbox ‘checkBoxName’.
Select an option from a drop down
The code for selecting the country name from a drop down box
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.
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.
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?”