Master Web Automation with Capybara & Selenium
On This Page What is Capybara?What is Selenium?June 08, 2026 · 8 min read · Tool Comparison
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. Why Use Capybara and Selenium for Web Automation? Getting Started with Capybara and Selenium This article explains on what is Capybara and how to use it with Selenium. 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 class GoogleSearch def search GoogleSearch.new.search Read More: 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 Selenium supports all the below languages Capybara with selenium is a powerful combination which provides below key advantages Read More: Get started by setting up the required thing to run the Capybara with Selenium Prerequisites To install ruby run below command (macos) Next install the bundler, bundler will manage the gems 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 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 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 Read More: 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. 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 Define the driver you are go to use, in the case it will be selenium Next measure is to define the class and function which will contain the actions def validateNavigationHeader BstackDemo.new.validateNavigationHeader Inside the class specify to include two faculty 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 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 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 Read More: Best practices will insure that tryout will be reliable, Stable, Maintainable and Efficient. RSpec.describe & # 8216; Google Search & # 8217;, case:: lineament do Capybara.register_driver: headless_chrome do |app| Capybara.default_driver =: headless_chrome RSpec.describe & # 8216; Google Search & # 8217;, type:: characteristic do Read More: 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. 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. On This Page # Ask-and-Contributeabout this subject with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.Getting Started with Capybara & amp; Selenium for Web Testing
Overview
What is Capybara?
require & # 8216; capybara/dsl & # 8217;
Capybara.default_driver =: selenium_chrome # Uses Chrome browser
include Capybara: :DSL
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
endWhat is Selenium?
Why use Capybara and Selenium for Web Automation Testing?
Get Started with Capybara and Selenium for Web
brew install ruby
gem install bundler
bundle init
source & # 8216; https: //rubygems.org & # 8217; gem & # 8216; capybara & # 8217;
gem & # 8216; selenium-webdriver & # 8217;
gem & # 8216; rspec & # 8217;bundle install
How Capybara and Selenium Work Together
Writing Your Maiden Test with Capybara and Selenium
require & # 8216; capybara/dsl & # 8217; ask & # 8216; rspec & # 8217;
Capybara.default_driver =: selenium_chrome
stratum BstackDemo include Capybara: :DSL
include RSpec: :Matchers
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
endinclude Capybara: :DSL include RSpec: :Matchers
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
Endruby bstackDemoTest.rb
Better Practices for Testing with Capybara and Selenium
require & # 8216; capybara/rspec & # 8217;
Capybara.default_driver =: selenium_chrome
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
endexpect (notice (& # 8216; # favourites strong & # 8217;, wait: 20)) .to have_content (& # 8216; Favourites & # 8217;)
require & # 8216; capybara/rspec & # 8217; command & # 8216; selenium-webdriver & # 8217;
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
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
endRSpec.configure do |config| config.after (: each) do
Capybara.reset_sessions!
end
endWhy choose BrowserStack to run Selenium Tests?
Conclusion
Related Guides
Automate This With SUSA
Test Your App Autonomously