Capybara Cucumber Code Example

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

Get a free account and launch your Gridlastic selenium grid, then run this code locally and test your selenium grid.


#features/test.feature

Feature: Google search

Scenario: Search for webdriver        
	Given I am on the Google homepage
	Then I will search for "webdriver"
	Then I should see "webdriver"

#features/step_definitions/test_steps.rb

Given(/^I am on the Google homepage$/) do
    visit 'http://www.google.com/ncr'
end

Then(/^I will search for "(.*?)"$/) do |searchText|
    fill_in 'q', :with => searchText
end

Then(/^I should see "(.*?)"$/) do |expectedText|
	expect(page).to have_content(expectedText)
end

#features/support/env.rb

require 'capybara/cucumber'
require 'selenium/webdriver'

capabilities = Selenium::WebDriver::Remote::Capabilities.new
capabilities['browserName'] = "chrome"
capabilities['version'] = "latest"
capabilities['platform'] = "WIN10"
capabilities['platformName'] = "windows"
capabilities['video'] = "True"

Capybara.register_driver :selenium do |app|
# Note: starting from selenium version 3.9.1 you must also include "platformName": "windows" in the request when testing with firefox and IE.
# If the requested test environment is not registered with the selenium grid 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 = 1200 #seconds
  
  Capybara::Selenium::Driver.new(app, http_client: client,
    :browser => :remote,
    :url => "https://USERNAME:ACCESS_KEY@HUB_SUBDOMAIN.gridlastic.com/wd/hub",
    :desired_capabilities => capabilities)
end

Capybara.default_driver = :selenium
Capybara.run_server = false

#TEST VIDEO
#VIDEO_URL set to like "https://s3-ap-southeast-2.amazonaws.com/b2729248-ak68-6948-a2y8-80e7479te16a/9ag7b09j-6a38-58w2-bb01-17qw724ce46t/play.html?".
#Find this VIDEO_URL value in your Gridlastic dashboard.
session_id = Capybara.current_session.driver.browser.instance_variable_get(:@bridge).session_id
STDOUT.puts "TEST VIDEO URL: "+VIDEO_URL+session_id

#Maximize window. 
Capybara.current_session.driver.browser.manage.window.maximize
# On LINUX/FIREFOX the "manage.window.maximize" option above does not expand browser window to max screen size. Resize as below:
#window = Capybara.current_session.driver.browser.manage.window
#window.resize_to(1920,1080) # width, height
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.