Ruby code example using Selenium Grid

This Ruby code example will get you a quick start using a selenium grid to run a single test

Install the latest selenium client version equivalent to your selected selenium grid version. Example: If you selected to use selenium version 4.15.0 for your selenium grid, install the latest 4.15.x for Ruby.

gem install selenium-webdriver -v 4.15.0

Selenium version V4


require 'selenium-webdriver'

options = Selenium::WebDriver::Options.chrome(browser_version: 'latest', platform_name: 'linux')

# Enable video recording
gridlastic_options = {}
gridlastic_options[:video] = true
options.add_option('gridlastic:options', gridlastic_options)


# If the requested test environment is not registered with the hub
# or busy, allow enough time for the Gridlastic auto scaling
# functionality to launch a node with the requested environment.
client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 600 # seconds


driver = Selenium::WebDriver.for(:remote, :url => "https://USERNAME:ACCESS_KEY@HUB_SUBDOMAIN.gridlastic.com/wd/hub", :http_client => client, options: options)


begin
driver.manage.timeouts.implicit_wait = 60 # seconds
#driver.manage.window.maximize # If Linux always set window size like driver.manage.window.size = Selenium::WebDriver::Dimension.new(1920, 1080)
driver.manage.window.size = Selenium::WebDriver::Dimension.new(1920, 1080)


print "Video: VIDEO_URL" + driver.session_id

driver.navigate.to "https://www.gridlastic.com/?demo"
sleep(10) # for demo purposes pause 10 seconds
ensure
driver.quit
end


Selenium version V3


gem 'selenium-webdriver', '= 3.14.0'
require 'selenium-webdriver'

# If the requested test environment is not registered with the hub
# or busy, allow enough time for the Gridlastic auto scaling
# functionality to launch a node with the requested environment.
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 600 # seconds.  

caps = Selenium::WebDriver::Remote::Capabilities.chrome(
:version               => "latest",
:platform              => "WIN10",
:platformName          => "windows",
:video                 => "True"
)
driver = Selenium::WebDriver.for(:remote, :url => "https://USERNAME:ACCESS_KEY@HUB_SUBDOMAIN.gridlastic.com/wd/hub", :http_client => client, :desired_capabilities => caps)
driver.manage.timeouts.implicit_wait = 60 # seconds
driver.manage.window.maximize # If Linux always set window size like driver.manage.window.size = Selenium::WebDriver::Dimension.new(1920, 1080)

begin
#print 'Video: ' + VIDEO_URL + driver.capabilities['webdriver.remote.sessionid']
print 'Video: ' + VIDEO_URL + driver.session_id  # If using chrome 75+

driver.navigate.to "https://www.google.com/ncr"
element = driver.find_element(:name, 'q')
element.send_keys "Hello WebDriver!"
element.submit
ensure
driver.quit
end

The Gridlastic hub endpoint and the video url used in this code example is displayed after launching your Gridlastic selenium grid. See documention Selenium Grid Configuration Parameters where to find these credentials and replace with your own.


Ruby Selenium Proxy Example

Use a selenium proxy to route your tests via our hosted Squid proxy or use your own proxy locally to test behind firewall internal sites using Gridlastic Connect Proxy Setup.

PROXY = "hub_subdomain.gridlastic.com:8001"; # hosted Squid proxy on your selenium grid hub
#PROXY = "your_gridlastic_connect_subdomain.gridlastic.com:9999"; # An example Gridlastic Connect endpoint

proxy = Selenium::WebDriver::Proxy.new(
  :http     => PROXY,
  :ftp      => PROXY,
  :ssl      => PROXY
)

caps = Selenium::WebDriver::Remote::Capabilities.chrome(:proxy => proxy)


NOTE: Gridlastic auto scaling requires all 3 test environment parameters platform, browser and browser version to be specified in each request in order to launch test nodes to fulfill test demand. Video recording is optional. See test environments for capabilities options.
It is important to ensure that "driver.quit" is always called for proper test execution and creation of video recordings of failed tests.