Skip to main content

Ruby with Selenium: Run Ruby Automation Testing Scripts on Online 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 topic will help you to automate your website testing using Ruby with Selenium on LambdaTest.

This topic will be focusing on:

  1. Running a sample Ruby Automation Testing Script 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 For Running Ruby With Selenium#

First, you would have to install Ruby automation testing framework 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.

Installing Selenium Dependencies For Ruby Automation Testing#

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 With Ruby Automation Testing & 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 Ruby automation testing framework 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 (source from LambdaTest Sample Scripts Ruby)require 'rubygems'require 'selenium-webdriver'require 'test/unit'require_relative  "readConf"class AdvanceTest < Test::Unit::TestCase    def setup        config =          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 =>  "RubyRSpecTestSample",            :build =>  "LambdaTestSampleApp",              }           puts (caps)        @driver = Selenium::WebDriver.for(:remote,:url => "https://"+lt_user+":"+lt_appkey+"", :desired_capabilities => caps)         @driver.manage.window.maximize         @driver.get("" )     end     def test_Login         item_name = "Yey, Lets add it to list"         #Click on First Checkbox        fCheckbox = @driver.find_element(:name, 'li1')         fCheckbox.submit         #Click on Second Checkbox        sCheckbox = @driver.find_element(:name, 'li2')         sCheckbox.submit             #Enter Item Name        itemNameInput = @driver.find_element(:id, 'sampletodotext')         itemNameInput.send_keys item_name         #Click on Add Button        addButton = @driver.find_element(:id, 'addbutton')         addButton.submit         # Verify Added Item        getItemName = @driver.find_element(:xpath, '/html/body/div/div/div/ul/li[6]/span').text         assert_equal(getItemName, item_name )     end     def teardown         @driver.quit     end end

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.

Executing The Test Using Ruby with Selenium#

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

ruby LambdaTest.rb`

Understanding & Configuring Ruby Desired Capabilities#

As we said earlier the first step is to configure your test scripts to connect with LambdaTest Selenium automation gird. In the above mentioned Ruby code, the first thing you would notice is the invoking of remote WebDriver instead of the native browser WebDrivers. So for example if you are planning to run on Firefox browser in your local machine, you would be using Firefox browser driver like this:

Local Driver

driver = Selenium::WebDriver.for :firefox`

However, to run on LambdaTest Selenium Grid, you would have to change it remote WebDriver and at the same time pass capabilities related to browser, browser versions etc. This is how you code would look like:

Remote WebDriver

driver = Selenium::WebDriver.for(:remote,:url => "https://"+lt_user+":"+lt_appkey+"",  :desired_capabilities => caps)

To read about all the Selenium and custom capabilities offered by LambdaTest, visit our Selenium Automation Capabilities document.

LambdaTest Capabilities Generator will automatically generate the program, based on your provided input. For Instance, if you select the below configurations:

Selenium Version3.13.0
Operating SystemOS X Yosemite
Browser Version71.0
BrowserGoogle Chrome

Then, LambdaTest Capabilities Generator will automatically generate the below program:

//OS/Browser Configurationscapabilities = Selenium::WebDriver::Remote::Capabilities.newcapabilities["build"] = "your build name"capabilities["name"] = "your test name"capabilities["platform"] = "OS X Yosemite" // Selected Operating Systemcapabilities["browserName"] = "Chrome"    // Selected browser namecapabilities["version"] = "71.0",        // Selected browser versioncapabilities["resolution"] = "1920x1080", // Selected screen resolutioncapabilities["selenium_version"] = "3.13.0", // Selected Selenium versioncapabilities["visual"] = true,capabilities["chrome.driver"] = 2.43 

The most important capabilities to understand here are ‘browserName’, ‘versions’, and ‘platform’. They define which browser environment you wish to run the test on. Rest of the capabilities are important in test management and debugging. We have an inbuilt capabilities generator tool as well that you use to generate capabilities code for your test suite.

Advanced LambdaTest Capabilities#

In addition to default Selenium Grid capabilities, LambdaTest also has platform specific capabilities like a video recording of test runs, console logs of each test run, network logs of each test run, custom profiles for specific browsers, etc. Do checkout our documentations on LambdaTest Advanced Capabilities as well.

Test Locally Hosted Projects Using Ruby With Selenium#

Perform cross browser testing on your locally hosted web app or website using LambdaTest tunnel for identifying bugs before your website goes live. This tunnel would help to establish an SSH(Secure Shell) connection for hosting your locally stored web pages from your local machine to LambdaTest cloud servers. You can use LambdaTest tunnel to perform cross browser testing using Ruby with Selenium on the web-apps or websites which may only be accessible within your organization’s firewall. You can even perform local host testing through different geographic IP to analyze how your website would work when accessed from different continents of the world. Wish to know more about LambdaTest tunnel?

Follow our documentation on LambdaTest 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:

If you are done with the download of binary file then open your cmd(Command Prompt) / terminal and move the pointer to the same location where the binary zip file is extracted in your system. Type the below command and hit Enter for launching the SSH connection through LambdaTest tunnel.

//Syntax to Start LambdaTest tunnelLT -user [user's login email] -key [user's access key]

For example, If you have the following:

Login Email[email protected]
Access Key987zyx987

Then, the command would be:

//Example Command To Start LambdaTest tunnelLT -user -key 987zyx987

The above command will set up LambdaTest tunnel successfully! Now, for performing automation testing using Ruby with Selenium you need to add the below to your capabilities.

DesiredCapabilities capabilities = new DesiredCapabilities();        capabilities.SetCapability("tunnel", true);

You may also use LambdaTest Capabilities Generator for providing you with the above capability.

Mark Status As Pass or Fail#

While performing automation testing using Ruby with Selenium on LambdaTest Selenium Grid, it is imperative to flag an automation test as either pass or fail. This is completely dependent on your testing requirement with respect to the validation of expected behaviour. You can mark whether an automation test passed or fail by adding the below code fragment in your automation script.

//LambdaTest Annotation@driver.execute_script("lambda-status=passed")@driver.execute_script("lambda-status=failed")

Queuing Automation Testing Test Cases#

If you have more test cases to run than your concurrency limit then you can rack them up in queue. However, to prevent the LambdaTest platform from over-exploitation, we have defined a limit on the maximum queuing capacity.

At LambdaTest, your maximum queue capacity is dependent upon your concurrent user sessions. The following formula would help you to calculate your queuing potential.

Maximum Queue Capacity

- Maximum number of test cases that can be queued = n + 150

- Here, n = number of concurrent sessions.

For example, if you are opting for a pricing where you can avail up to 10 concurrent sessions. Then the maximum number of test cases that can be queued will be equal to 10 + 150 i.e. 160 queued test cases. Queuing allows you to manage your test pipeline in a hassle-free manner. The process of scheduling and executing the test cases is automatically handled by LambdaTest for you.

LambdaTest Queuing Policy: Follow out LambdaTest Queuing Policy to know more.

Queuing Timeout

There is also a limit on how long a test case can stay in the queue. If your test case stays in the queue for more than 15 minutes, the test case would be timed out and would not be executed.

Last updated on