Master Web Automation with Capybara & Selenium

On This Page What is Capybara?What is Selenium?June 08, 2026 · 8 min read · Tool Comparison

Getting Started with Capybara & amp; Selenium for Web Testing

Selenium with Capybara is a powerful combination for creating robust test mechanisation framework for Web Application. Capybara cater Intuitive DSL (Which mimics the language of an actual exploiter) and is compose in Ruby.

Capybara is agnostical about the driver pass your tests and comes with Rack: :Test and Selenium support progress in. WebKit is supported through an extraneous gem.

Overview

Why Use Capybara and Selenium for Web Automation?

  • Intuitive and Readable Tests: Capybara & # 8217; s domain-specific language (DSL) create trial easy to write and interpret.
  • Simulates Real User Behavior: Selenium mimics genuine user interactions with browsers, ensuring tests reflect real-world usage.
  • Potent Synchronization: Capybara automatically handles waits for asynchronous processes, eliminating manual wait in your tests.
  • Integration with RSpec and Cucumber: Capybara integrates easily with Ruby testing frameworks like RSpec and Cucumber.

Getting Started with Capybara and Selenium

  • Prerequisites: Install Ruby, set up an IDE like VS Code, and use Bundler to manage dependencies.
  • Installation: Add involve gems (capybara, selenium-webdriver, rspec) to the Gemfile and run bundle install.
  • Write Your First Test: Use Capybara DSL to see a situation, perform actions, and assert behavior utilise RSpec for validation.

This article explains on what is Capybara and how to use it with Selenium.

What is Capybara?

Capybara is a testing library that is wide used for end-to-end web application examination and is ruby-based. Capybara comes with built-in support forSupport to selenium, so that exploiter can just mention the driver in the test and it & # 8217; s ready to use.

Below is the example of how the test looks using capybara

require & # 8216; capybara/dsl & # 8217;
Capybara.default_driver =: selenium_chrome # Uses Chrome browser

class GoogleSearch
include Capybara: :DSL

def search
visit & # 8216; https: //www.google.com & # 8217;
fill_in & # 8216; q & # 8217;, with: & # 8216; Capybara Selenium & # 8217;
find (& # 8216; input [name= & # 8221; btnK & # 8221;] & # 8217;) .click
sleep 2 # Wait to see the result
end
end

GoogleSearch.new.search

Read More:

What is Selenium?

Selenium is a very democratic test automation framework used for automatize web based application. Selenium comes with various browser driver which will interact with Existent browsers by sending the bid.

Selenium indorse all below browsers

  • Google Chrome
  • Mozilla Firefox
  • Microsoft Edge
  • Safari
  • Opera

Selenium supports all the below languages

  • Python
  • Java
  • C#
  • Javascript/Typescript
  • Ruby
  • Kotlin

Why use Capybara and Selenium for Web Automation Testing?

Capybara with selenium is a powerful combination which provides below key advantages

  • Test written are Easy to write and Read:Capybara provides intuitive DSL for defining tests and those tests will feature activeness integration from Selenium
  • Simulates User Behavior:Selenium mimics real time user interaction with Browser, this ensures that tests written are simulate the real time user journey
  • Powerful synchronization:Capybara provides this feature to automatically wait for asynchronous processes to complete and you don & # 8217; t need to add manual wait
  • Works with RSpec, Cucumber:Capybara provides leisurely way of integrating with popular crimson testing framework.

Read More:

Get Started with Capybara and Selenium for Web

Get started by setting up the required thing to run the Capybara with Selenium

Prerequisites

  1. Ruby
  2. IDE (VS Code)

To install ruby run below command (macos)

brew install ruby

Next install the bundler, bundler will manage the gems

gem install bundler

Now you can create the project folder where the tests will reside. Once the folder is create execute below command from the theme of the pamphlet

bundle init

This will make a gem file. A gem file is where you add all the dependencies required for the project

Open the Gem file and paste the below

source & # 8216; https: //rubygems.org & # 8217; gem & # 8216; capybara & # 8217;
gem & # 8216; selenium-webdriver & # 8217;
gem & # 8216; rspec & # 8217;

you are adding three habituation here: capybara, selenium-webdriver and rspec.

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

Save the gem file and run the below command to install the gemstone

bundle install

Talk to an Expert

Read More:

How Capybara and Selenium Work Together

Capybara provides the users to write tests in intuitive DSL (Domain-Specific Language) and selenium will be used as a driver, which will run activeness in the real web browsers like Chrome, Firefox or Edge.

Capybara likewise provides an assertion to validate look behavior.

Writing Your Maiden Test with Capybara and Selenium

Write your first trial using capybara and selenium. UseBrowserStack & # 8217; s Demo sitefor make a scenario for the trial.

In the undertaking create a new file calledbstackDemoTest.rb

In the newly created file, add the imports first

require & # 8216; capybara/dsl & # 8217; ask & # 8216; rspec & # 8217;

Define the driver you are go to use, in the case it will be selenium

Capybara.default_driver =: selenium_chrome

Next measure is to define the class and function which will contain the actions

stratum BstackDemo include Capybara: :DSL
include RSpec: :Matchers

def validateNavigationHeader
visit & # 8216; https: //bstackdemo.com/ & # 8217;
require (encounter (& # 8216; # favourites strong & # 8217;)) .to have_content (& # 8216; Favourites & # 8217;)
sleep 2 # Wait to see the result before the handwriting loss
end
end

BstackDemo.new.validateNavigationHeader

Inside the class specify to include two faculty

include Capybara: :DSL include RSpec: :Matchers

The module Capybara: :DSL is included to support the DSL fashion of pen exam and RSpec: :Matchers will allow us to write affirmation for the exam

def validateNavigationHeader visit & # 8216; https: //bstackdemo.com/ & # 8217;
expect (detect (& # 8216; # favourites strong & # 8217;)) .to have_content (& # 8216; Favourites & # 8217;)
sleep 2 # Wait to see the upshot before the script exits
end
End

In the above code block you are defining the methodvalidateNavigationHeaderwhich contains all the activity that has to be execute inside browser

Perform two actions in the above test

  1. Visiting BrowserStack & # 8217; s Demo site
  2. Finding the navigation link and corroborate its substance

Sleep of 2 seconds is optional and merely for the demonstration intent, to see the result in the browser.

Save the file and execute the below command to run the test

ruby bstackDemoTest.rb

Read More:

Better Practices for Testing with Capybara and Selenium

Best practices will insure that tryout will be reliable, Stable, Maintainable and Efficient.

  • Write Structured Tests:Do not write champaign ruby tests, instead use RSpec for writing and organising trial. Below is the test compose with RSpec
require & # 8216; capybara/rspec & # 8217;
Capybara.default_driver =: selenium_chrome

RSpec.describe & # 8216; Google Search & # 8217;, case:: lineament do
it & # 8216; perform a search & # 8217; do
visit & # 8216; https: //bstackdemo.com/ & # 8217;
expect (discover (& # 8216; # favourites strong & # 8217;)) .to have_content (& # 8216; Favourites & # 8217;)
sleep 2 # Wait to see the result before the script release
end
end

  • Use Dynamic wait instead of Hardcoded hold:Capybara can mechanically wait for the constituent
expect (notice (& # 8216; # favourites strong & # 8217;, wait: 20)) .to have_content (& # 8216; Favourites & # 8217;)
  • Always run tests in headless mode: Running in headless manner will always be quicker. If it & # 8217; s CI or bulk exam execution, run them in headless mode. the example code to run in headless mode will look like below
require & # 8216; capybara/rspec & # 8217; command & # 8216; selenium-webdriver & # 8217;

Capybara.register_driver: headless_chrome do |app|
choice = Selenium: :WebDriver: :Chrome: :Options.new
options.add_argument (& # 8216; & # 8211; headless & # 8217;)
options.add_argument (& # 8216; & # 8211; disable-gpu & # 8217;)
options.add_argument (& # 8216; & # 8211; window-size=1280,800 & # 8217;)
Capybara: :Selenium: :Driver.new (app, browser:: chrome, selection: options)
end

Capybara.default_driver =: headless_chrome

RSpec.describe & # 8216; Google Search & # 8217;, type:: characteristic do
it & # 8216; performs a search & # 8217; do
visit & # 8216; https: //bstackdemo.com/ & # 8217;
expect (find (& # 8216; # favourites strong & # 8217;, wait: 20)) .to have_content (& # 8216; Favourites & # 8217;)
sleep 2 # Wait to see the result before the script passing
end
end

  • Session houseclean up after each test:Resetting the session after each exam execution will ensure that trial are not dependent on each other and there is no trial datum wetting across. Every test should be capable to execute independently. This can be accomplish using below codification
RSpec.configure do |config| config.after (: each) do
Capybara.reset_sessions!
end
end

Read More:

Why choose BrowserStack to run Selenium Tests?

Here are some understanding why you might prefer BrowserStack Automate to run Selenium tests:

1. Cross-Browser and:BrowserStack provides approach to a wide ambit of real devices, browser, and OS combinations. This allow you to run your Selenium tests across various environments without needing to set up the infrastructure yourself.

2. :Unlike emulators, BrowserStack offers existent devices for testing. This ensures more accurate results, simulate how the covering performs on actual exploiter devices.

3. No Infrastructure Management:With BrowserStack Automate, you don & # 8217; t have to worry about keep physical or virtual machines for testing. It handles the entire infrastructure, saving time and effort.

4. Instant Access to Browsers & amp; Devices:You can rapidly run tests on the late browser variant, and as new versions are released, BrowserStack update their platform mechanically.

5. Execution:Run multiple tests in latitude, which speeds up the testing process significantly. This aid in cutting down the time required for fixation or functional examination.

6. Scalability:BrowserStack offers scalable solution, allowing you to run tests on a declamatory number of devices and browsers simultaneously, without worrying about server overload or restriction.

7. Integration with CI/CD Tools:BrowserStack integrates easily with CI/CD tool like Jenkins, Travis CI, CircleCI, and GitHub Actions. This get it simple to automate your testing line and ensure uninterrupted delivery.

8.:Test your web application from different geographic locations to simulate various user experiences, assist to secure your covering works optimally general.

9. Real-Time Debugging:BrowserStack cater detailed log, screenshots, and picture recording for each test run. This makes debug easier and faster, especially when act on complex issue.

10. Support for Various Automation Frameworks:Besides Selenium, BrowserStack support various testing frameworks, such as Appium, Cypress, and Playwright, providing flexibleness in test performance.

Conclusion

You get seen how quicker it is to set up Capybara with selenium and how to execute the tests. Capybara and Selenium is a knock-down combination to pen automated tests for web covering, Capybara cater intuitive DSL and Selenium providing capability to interact with Real browsers.

Tags
7,000+ Views

# Ask-and-Contributeabout this subject with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free