Skip to main content

Quick Guide To Run Ruby Tests On LambdaTest Selenium Grid


LambdaTest Selenium Automation Grid is a secure, scalable, and reliable Selenium Grid on cloud that can help you run your Ruby automation scripts on 3000+ different browsers and operating system environments. The grid is specifically built for high performance and gives you not only a greater browser coverage but also an ability to run automation tests in parallel. This helps you reduce your automation test execution time significantly. This post will help you quickly get started with running Ruby test scripts on LambdaTest Selenium Grid.

Prerequisites


First, you would have to install Ruby and gem on your local system. We recommend installing the latest version of Ruby. Installing these platforms is a little different in each operating system

  • For windows, you can download the latest installed the official website.

  • 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

Installing Selenium Dependencies For Ruby


Once you have Ruby and Gem setup, you would now have to install Selenium dependencies. You can install Selenium via simple gem command-

gem install selenium-webdriver

Sample Ruby & Selenium Test To Get You started


Once you have installed necessary dependencies, you are all set to run your Ruby automation scripts on LambdaTest Selenium grid. To get started you can try out the code below. It’s a simple Ruby scripts that opens up Google.com, searches LambdaTest in Google search bar, and then displays the title of the search result page. You can also fork it from our Ruby GitHub repository.

//Sample Ruby and Selenium Automation Script

require 'selenium-webdriver'
require 'test/unit'

class LtTest < Test::Unit::TestCase
"""
LambdaTest selenium automation sample example
Configuration
----------
username: Username can be found at automation dashboard
accessToken: AccessToken can be generated from automation dashboard or profile section

Result
-------
Execute Ruby Automation Tests on LambdaTest Distributed Selenium Grid
"""

def setup
"""
Setup remote driver
Params
----------
platform : Supported platform - (Windows 10, Windows 8.1, Windows 8, Windows 7, macOS High Sierra, macOS Sierra, OS X El Capitan, OS X Yosemite, OS X Mavericks)
browserName : Supported platform - (chrome, firefox, Internet Explorer, MicrosoftEdge)
version : Supported list of version can be found at https://www.lambdatest.com/capabilities-generator/

Result
-------
"""
username= "{YOUR_LAMBDATEST_USERNAME}"
accessToken= "{YOUR_LAMBDATEST_ACCESS_KEY}"
gridUrl = "hub.lambdatest.com/wd/hub"

caps = {
:browserName => "chrome",
:version => "67.0",
:platform => "win10",
:name => "LambdaTest ruby google search name",
:build => "LambdaTest ruby google search build",
:network => true,
:visual => true,
:video => true,
:console => true
}

puts (caps)
# URL: https://{username}:{accessToken}@hub.lambdatest.com/wd/hub
@driver = Selenium::WebDriver.for(:remote,
:url => "https://"+username+":"+accessToken+"@"+gridUrl,
:desired_capabilities => caps)
end

def test_Login
"""
Setup remote driver
Params
----------
Execute test: navigate google.com search LambdaTest
Result
-------
print title
"""
puts("Searching LambdaTest on google.com ")
sleep(15)
@driver.get("https://www.google.com/ncr")
elem = @driver.find_element(:name, 'q')
elem.send_keys("LambdaTest.com")
elem.submit()

puts("Printing title of current page :"+@driver.title)
end
def teardown
"""
Quit selenium driver
"""
@driver.quit
end
end

Here’s how you can execute the test from your terminal.

ruby google-search-lambdatest.rb

The first thing to note in this code is the use of remote webdriver. To access LambdaTest grid, you would have to invoke remote webdriver instead of local webdrivers. In addition, the browser enthronement details are passed on through desired_capabilities.

In this example we are running a test on Windows 10, Chrome 67 environment. You can test your own choice of an environment by changing the parameters in the object. You can leverage our capabilities generator tool to create code for your environments.

Tear Down: The tear down method helps LambdaTest platform understand when the execution of a test is finished. It’s important to tear down each test or else it will give timeout errors.