Web Testing with Capybara

Mritunjai Kumar Rai
TestVagrant
Published in
5 min readDec 7, 2020

Most of us have worked on a different type of web-based testing framework in different languages like Java -Selenium with TestNG, C# - Selenium with MSTest and so many ... Here we are going to talk about one more fantastic framework which is Capybara, and will see how can we write our test cases more readable and with minimal boilerplate code.

About Capybara

Capybara is a web-based automation framework used for creating functional tests that simulate how users would interact with your application.

Capybara is a library/gem useful for an underlying web-based driver. Moreover, it offers a user-friendly DSL (Domain Specific Language) which is useful to describe actions that execute by the underlying web driver.

When the page loads using the DSL (and underlying web driver). Moreover, the Capybara will try to locate the relevant element in the DOM (Document Object Model) and execute the action, such as click button, link, etc.

Capybara is based on the assumption that in a modern web application, potentially everything might happen asynchronously. Whenever you verify that some content is there or some condition is met, Capybara by default waits for the content to appear or the condition to become true. The timeout for this is configurable, of course.

Here are some of the web drivers supported by Capybara:

  1. rack::test: (default driver)
  2. selenium-web driver:
  3. capybara-WebKit:

Popular Ruby-based Testing Framework

Some major testing framework which can be used with Capybara is :

  1. Cucumber is a Ruby-based test tool for BDD.
  2. RSpec is a behaviour-driven development (BDD) framework, inspired by JBehave.
  3. Minitest: is a testing suite for Ruby. It provides a complete suite of testing facilities supporting test-driven development (TDD), behaviour-driven development (BDD), mocking, and benchmarking.

Key Benefits

  • No setup is necessary for Rails and Rack application. Works out of the box.
  • Intuitive API which mimics the language an actual user would use.
  • Switch the backend your tests run against from fast headless mode to an actual browser with no changes to your tests.
  • Powerful synchronization features mean you never have to manually wait for asynchronous processes to complete.

Setup Capybara with a different testing framework

How to do the initial setup :

brew install ruby
brew cask install chromedriver
gem install bundler
gem install cucumber selenium-webdriver
gem install rspec
cucumber - init. // Create cucumber based test folder strucuture
rspec - init. // Create RPSec based test folder structure
bundle init.
bundle gem <Project_Name>
bundle install // Install dependency mentioned in Gemfile

Here I have set up 3 different testing frameworks like Cucumber, RSpec, and Minitest. We can automate our web application using any one of them.

Different Testing framework folder structure

Capybara cheatsheet
Here are a few capybara methods which help us to simulate web-based automation.

Test Project details

Capybara/cucumber test project has the following components

env.rb: have all the required configuration which requires in all test files.

Here ,we are configuring different types of drivers to be used by capybara.

features file: Here we can add scenarios in BDD format.

Step definition: A ruby with a capybara DSL code to execute the test.

page_module.rb: Encapsulated page object and actions on a different layer.

We can use different gems like “page-object” and “prism” to write our test cases in POM .

Rakefile: Rake is a software task management and build automation tool. It allows the user to specify tasks and describe dependencies as well as to group tasks in a namespace.

We can call different tasks and pass “<browser name>” as a parameter so our test will on a specific browser only. We can configure tag in the task so only a specific test will be run Like only we want to run @smoke and @regression suite.

How to run Task:

$rake 'runCucumberTest['chrome']'

Brief about RSpec

RSpec is a testing tool for Ruby, created for behaviour-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly

Comparing with Cucumber DSL:

RSpec generates a “spec” folder, underlying we can add our test files. Like “env.rb”, we have a “spec_helper.rb” file that loads the whole Rails environment, requiring numerous helper files and setting up various configuration options.

For RSpec, I have used two different way to instantiate driver, first is registering driver in spec_helper and creating an instance and using across all spec files. refer below code.

In the second way, I have used “named session” to create multiple sessions from the same driver. Code can be found in the down of the project link.

How To Run RSpec’s Test:

From terminal:

$ bundle exec rspec spec/githubuser_spec.rb

$bundle exec rspec spec/*.rb

Using Rake: Can decide on which browser test has to be run. In Rakefile, we can configure “-tag” as well.

$rake ‘runRpecTest[‘chrome’]’.

From Test Explorer: All RSpec test will reflect in Test explorer, we can select individual test and run it.

Test Explorer: RSpec Tests

Brief about Minitest

Minitest is a testing suite for Ruby. It provides a complete suite of testing facilities supporting test-driven development (TDD), behavior-driven development (BDD), mocking, and benchmarking. It’s small, fast, and it aims to make tests clean and readable. Using Minitest we can write a unit test, controller test, and integration tests...

Likewise cucumber and Rspec, here we have “test_helper.rb” file where we can put our configurable code which gets loaded very initially.

In my case, I have not included any driver initialization code here instead of added in the class file only.

How to Run :

$ruby test/github_minitest.rb.

Conclusion

It was exiciting to explore capybara along with three differnt testing framework more ever all three are slightly different to each other . We have different choices in the test framework here, like Cucumber, RSpec, and Minitest, it's up to you what you chose, personally, all 3 are superb!

Capybara_Test Project link

Resources

  1. https://github.com/teamcapybara/capybara
  2. https://github.com/mattheworiordan/capybara-screenshot#better-looking-html-screenshots
  3. https://github.com/allure-framework/allure-ruby
  4. https://makandracards.com/makandra/14017-useful-methods-to-process-tables-in-cucumber-step-definitions
  5. https://rdoc.info/gems/kosmas58-cucumber/0.3.102/Cucumber/Ast/Table
  6. https://www.rubydoc.info/gems/capybara/Capybara

--

--