Skip to main content

Running Automation Scripts on Selenium Grid Cloud Using Capybara Ruby

LambdaTest provides support with Selenium WebDriver for pacing the execution of your automation test scripts. LambdaTest is a cloud-based, cross browser testing tool, providing a Selenium Grid of 2000+ browsers and browser versions running through real operating systems to speed up automation testing of your web-app or website. This documentation will help you perform Selenium automation testing with Capybara on LambdaTest.

This topic will be focusing on:

  • Running a sample Selenium with CapyBara Ruby on LambdaTest Automation.
  • Setting up environment for testing your locally hosted web pages or website.
  • Running multiple tests in parallel with CapyBara Ruby using LambdaTest Selenium Grid.

Prerequisites For Running CapyBara Ruby Using Selenium Grid#

All the code samples in this documentation can be found in the Capybara-Ruby LambdaTest Repository on Image GitHub. You can either download or clone the repository to quickly run your tests.

First, you would have to install Ruby and gem on your local system. Installing these platforms is a little different in each operating system

  • For windows, you can download the latest installed from here.
  • If you are using Linux or Ubuntu OS, you can run a simple apt command like below. You can also refer to this doc to know more about which command suits your OS better.
$ sudo apt-get install ruby-full
  • If you are using MacOS, you can run a homebrew command like this:
$ brew install ruby
  • To run tests in parallel you will require the parallel_tests gem.

  • LambdaTest binary file for running tests on your locally hosted web pages.

  • LambdaTest Authentication Credentials

    Be aware of your LambdaTest authentication credentials i.e. your LambdaTest username, access key and HubURL. You need to set them up as your environment variables. You can retrieve them from your LambdaTest automation dashboard by clicking on the key icon near the help button.

For Linux/Mac:

export LT_USERNAME=<LT-username> &&export LT_APIKEY=<LT-access-key>

For Windows:

set LT_USERNAME=<LT-username> &&set LT_APIKEY=<LT-access-key>

Installing Selenium Dependencies For RSpec#

Once you have ruby and gem setup, you would now have to install Selenium dependencies. Use the below gem command for installing Selenium:

bundle install

Setting Up The Environment#

You need to clone our GitHub repository which demonstrates a sample of CapyBara Ruby.

After cloning, you need to navigate to the cloned directory and install project dependencies using the below command:

gem install selenium-webdriver

The example mentioned below would help you to execute your automation test using CapyBara Ruby.

Feature: Add to Todo list functionality Background:  Given I am on Scenario: Add Todo List    When I click on first item    When I click on second item    When I add new item "New item to the list"    Then I should see new item in list "New item to the list"

Now Create the support rb file.

require 'yaml'require 'selenium/webdriver'require 'capybara/cucumber' # monkey patch to avoid reset sessionsclass Capybara::Selenium::Driver < Capybara::Driver::Base  def reset!    if @browser'about:blank')    end  endend TASK_ID = (ENV['TASK_ID'] || 0).to_iCONFIG_NAME = ENV['CONFIG_NAME'] || 'single' CONFIG = YAML.load(, "../../config/#{CONFIG_NAME}.config.yml")))CONFIG['user'] = ENV['LT_USERNAME'] || CONFIG['user']CONFIG['key'] = ENV['LT_ACCESS_KEY'] || CONFIG['key']  Capybara.register_driver :lambdatest do |app|  @caps = CONFIG['common_caps'].merge(CONFIG['browser_caps'][TASK_ID])  if (CONFIG_NAME=='jenkins')puts ENV['LT_GRID_URL']lt_browser = ENV['LT_BROWSER_NAME']lt_os = ENV['LT_PLATFORM']lt_browser_version = ENV['LT_BROWSER_VERSION']lt_res = ENV['LT_RESOLUTION']@caps={"browserName"=>lt_browser, "version"=>lt_browser_version, "platform"=>lt_os, "resolution"=>lt_res, "build"=>"capybara-lambdatest", "name"=>"single-Test-Jenkins","video"=>true, "network"=>true, "console"=>true, "visual"=>true },        :browser => :remote,        :url => ENV['LT_GRID_URL'],        :desired_capabilities => @caps      ) else,    :browser => :remote,    :url => "https://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub",    :desired_capabilities => @caps  )endend Capybara.default_driver = :lambdatestCapybara.run_server = false
require 'rake'require 'parallel'require 'cucumber/rake/task' do |task|  ENV['CONFIG_NAME'] ||= "single"  task.cucumber_opts = ['--format=pretty', 'features/single.feature']end task :default => :single do |task|  task.cucumber_opts = ['--format=pretty', 'features/local.feature', 'CONFIG_NAME=local']end task :parallel do |t, args|  @num_parallel = 4[*1..@num_parallel], :in_processes => @num_parallel) do |task_id|    ENV["TASK_ID"] = (task_id - 1).to_s    ENV['name'] = "parallel_test"    ENV['CONFIG_NAME'] = "parallel"     Rake::Task["single"].invoke    Rake::Task["single"].reenable  endend do |task|  task.cucumber_opts = ['--format=pretty', 'features/single.feature', 'CONFIG_NAME=jenkins']end  task :test do |t, args|  Rake::Task["single"].invoke  Rake::Task["single"].reenable  Rake::Task["local"].invoke  Rake::Task["parallel"].invokeend

Note: Update *.config.yml files inside the config/ directory with your [LambdaTest Username and Access Key].

Running Tests Using CapyBara Ruby#

Navigate to the directory where you cloned the sample of CapyBara Ruby and run the following command.

  • To run a single test.
bundle exec rake single

Testing Locally Hosted or Privately Hosted Projects#

To help you perform cross browser testing of your locally stored web pages, LambdaTest provides an SSH(Secure Shell) tunnel connection with the name Lambda Tunnel. With Lambda Tunnel, you can execute your tests using CapyBara Ruby to perform automated cross browser testing on browsers offered by online Selenium grid at LambdaTest. So you make sure how well your changes look, even before your customers. Curious to know more about Lambda Tunnel?

Follow our documentation on Lambda Tunnel to know it all. OS specific instructions to download and setup tunnel binary can be found at the following links.

Download the binary file of:

Use the below command for running a local test.

bundle exec rake local

Running Tests In Parallel Using Capybara Ruby#

Parallel Testing is one of the most demanding features of LambdaTest Selenium Grid. By parallel testing, you can run more than one test case, simultaneously. This means that Parallel testing would allow you to execute numerous automation test cases altogether. So you execute a single test scenario across different browsers or could run different test scenarios across the same browser but with different browser versions. Wondering how many parallel test cases can you run? That depends entirely on the number of concurrent session under your opted plan.

bundle exec rake parallel

Monitor and analyze your test result on the LambdaTest Automation Dashboard.

Deploy your code in a reliable manner at scale using CapyBara Ruby with LambdaTest Selenium Grid, and ensure it looks robust across every browser to provide a seamless user experience to all your visitors. Happy Testing!

Last updated on