Skip to main content

RSpec With Selenium: Run RSpec Tests On LambdaTest Selenium Grid


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 to perform Selenium Automation testing with RSpec on LambdaTest.

This topic will be focusing on:

  1. Running a sample RSpec with Selenium on LambdaTest Automation.
  2. Setting up environment for testing your locally hosted web pages or website.
  3. Core Capabilities: understanding and configuring them in your Selenium test suite.
  4. Exploring advanced features of LambdaTest.

Prerequisites To Run RSpec Tests With Selenium#


All the code samples in this documentation can be found in the RSpec 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 RSpec tests in parallel you will require the parallel_tests gem.
  • LambdaTest binary file to run RSpec tests on your locally hosted web pages.

Installing Selenium Dependencies To Run RSpec Tests#


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

gem install selenium-webdriver

Getting Started Using RSpec with Selenium Automation & LambdaTest#


Now that we are all set with prerequisites. It is time we look at a sample code, this code will validate if there is any change in the title of Google’s home page. This code is also available on our RSpec GitHub repository for you to clone and play around.

By using this code example, we will first validate your LambdaTest credentials for authentication purpose. Later, the code will select the basic capabilities such as OS, browser, browser version and so on.

//LambdaTest.rb require_relative  "readConf"require "json"require "selenium-webdriver"require "rspec" describe "Add Product and checkout" do         before(:each) do         config = ReadConfig.new()      lt_user = ENV['LT_USERNAME']    lt_appkey = ENV['LT_APPKEY']    lt_os = ENV['LT_OPERATING_SYSTEM']    lt_browser = ENV['LT_BROWSER']    lt_browser_version = ENV['LT_BROWSER_VERSION']    if(lt_user == "" || lt_user == nil)        lt_user = config.getDetails('LT_USERNAME')    end    if(lt_appkey == "" || lt_appkey == nil)        lt_appkey = config.getDetails('LT_APPKEY')    end    if(lt_browser == "" || lt_browser == nil)        lt_browser = config.getDetails('LT_BROWSER')    end    if(lt_os == "" || lt_os ==nil)        lt_os = config.getDetails('LT_OPERATING_SYSTEM')    end    if(lt_browser_version == "" || lt_browser_version == nil)        lt_browser_version = config.getDetails('LT_BROWSER_VERSION')    end    caps = {                               :browserName => lt_browser,                 :version => lt_browser_version,                 :platform =>  lt_os,        :name =>  "RSpec Sample Test",        :build =>  "RSpec Selenium Sample",          }      puts (caps)    @driver = Selenium::WebDriver.for(:remote,        :url => "https://"+lt_user+":"+lt_appkey+"@hub.lambdatest.com/wd/hub",        :desired_capabilities => caps)         @driver.manage.window.maximize         @driver.get("https://lambdatest.github.io/sample-todo-app/" )    sleep(3)         end     after(:each) do      @driver.quit     end     it "Select a product and add into cart, Checkout" do     itemName = 'Yey, Lets add it to list'     #Click on First Checkbox    @driver.find_element(:name, 'li1').click        #Click on Second Checkbox    @driver.find_element(:name, 'li2').displayed?        #Enter item name       @driver.find_element(:id, 'sampletodotext').send_keys itemName              @driver.find_element(:id, 'addbutton').displayed?    @driver.find_element(:id, 'addbutton').click            # Verify Item added successfully    textAddedItem = @driver.find_element(:css, "[class='list-unstyled'] li:nth-child(6) span").text    expect(textAddedItem).to eq(itemName)  end      end

Now, lets create config.conf file.

//config.conf
LT_USERNAME=<YOUR USERNAME>LT_APPKEY=<YOUR ACCESS KEY>LT_OPERATING_SYSTEM = win10LT_BROWSER=chromeLT_BROWSER_VERSION=63.0

Now, add readConfig.rb file.

//readConfig.rb
require ('rubygems')require ('parseconfig') class ReadConfig    #function to read .conf file    def getDetails(key)            my_config = ParseConfig.new( File.expand_path(File.dirname(__FILE__)) +'https://www.lambdatest.com/support/docs/config.conf')        value = my_config.params[key]        return value    endend

The Selenium WebDriver test would open a URL, mark the first two items in the list as done, add an item in the list, and return the total number of pending item. Your results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on LambdaTest dashboard. LambdaTest Dashboard will help you view all your text logs, screenshots and video recording for your entire Selenium tests.

Execute The Test Using RSpec With Selenium#


You would need to execute the below command in your terminal/cmd.

gem bundlebundle installrspec LambdaTest.rb
Last updated on