LambdaTest ● Automation Testing ● Ruby with Selenium: Run Ruby Automation Testing Scripts on Online Selenium Grid

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:

Running a sample Ruby Automation Testing Script on LambdaTest Automation. Setting up environment for testing your locally hosted web pages or website. Core Capabilities: understanding and configuring them in your Selenium test suite. 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 here https://rubyinstaller.org/downloads/.

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 1 $ sudo apt - get install ruby - full If you are using MacOS, you can run a homebrew command like this:

$ brew install ruby 1 $ 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 1 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 = 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 => "RubyRSpecTestSample", :build => "LambdaTestSampleApp", :network => true, :visual => true, :video => true, :console => true } 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/" ) 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 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 //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 = 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 = > "RubyRSpecTestSample" , : build = > "LambdaTestSampleApp" , : network = > true , : visual = > true , : video = > true , : console = > true } 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/" ) 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 1 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 1 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 Web Driver

driver = Selenium::WebDriver.for(:remote,:url => "https://"+lt_user+":"+lt_appkey+"@hub.lambdatest.com/wd/hub", :desired_capabilities => caps) 1 driver = Selenium :: WebDriver . for ( : remote , : url = > "https://" + lt_user + ":" + lt_appkey + "@hub.lambdatest.com/wd/hub" , : 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:

Capability Selected Value Selenium Version 3.13.0 Resolution 1920×1080 Operating System OS X Yosemite Browser Version 71.0 Browser Google Chrome

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

//OS/Browser Configurations capabilities = Selenium::WebDriver::Remote::Capabilities.new capabilities["build"] = "your build name" capabilities["name"] = "your test name" capabilities["platform"] = "OS X Yosemite" // Selected Operating System capabilities["browserName"] = "Chrome" // Selected browser name capabilities["version"] = "71.0", // Selected browser version capabilities["resolution"] = "1920x1080", // Selected screen resolution capabilities["selenium_version"] = "3.13.0", // Selected Selenium version capabilities["visual"] = true, capabilities["chrome.driver"] = 2.43 1 2 3 4 5 6 7 8 9 10 11 //OS/Browser Configurations capabilities = Selenium :: WebDriver :: Remote :: Capabilities . new capabilities [ "build" ] = "your build name" capabilities [ "name" ] = "your test name" capabilities [ "platform" ] = "OS X Yosemite" // Selected Operating System capabilities [ "browserName" ] = "Chrome" // Selected browser name capabilities [ "version" ] = "71.0" , // Selected browser version capabilities [ "resolution" ] = "1920x1080" , // Selected screen resolution capabilities [ "selenium_version" ] = "3.13.0" , // Selected Selenium version capabilities [ "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 Lambda 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 Lambda 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 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. Documentation For Windows User

Documentation For Mac User

Documentation For Linux User

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 Lambda Tunnel.

//Syntax to Start Lambda Tunnel LT -user [user's login email] -key [user's access key] 1 2 //Syntax to Start Lambda Tunnel LT - user [ user 's login email] -key [user' s access key ]

For example, If you have the following:

Field Example Values Login Email LoremIpsum@lambdatest.com Access Key 987zyx987

Then, the command would be:

//Example Command To Start Lambda Tunnel LT -user LoremIpsum@lambdatest.com -key 987zyx987 1 2 //Example Command To Start Lambda Tunnel LT - user LoremIpsum @ lambdatest . com - key 987zyx987

The above command will set up Lambda 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); 1 2 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") 1 2 3 //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.