Welcome to the SmartUI SDK sample for Selenium Ruby. This repository demonstrates how to integrate SmartUI visual regression testing with Selenium Ruby.
smartui-ruby-selenium-sample/
├── sdk/
│ ├── sdkCloud.rb # Cloud test
│ ├── sdkLocal.rb # Local test
│ ├── ignore_select.rb # Example with ignore options
│ ├── package.json # SmartUI CLI dependency
│ └── smartui-web.json # SmartUI config (create with npx smartui config:create)
└── hooks/ # Hooks integration examples
└── smartui_hooks.rb
- Ruby 2.7 or higher
- Node.js (for SmartUI CLI)
- LambdaTest account credentials (for Cloud tests)
- Chrome browser (for Local tests)
For Cloud:
export LT_USERNAME='your_username'
export LT_ACCESS_KEY='your_access_key'
export PROJECT_TOKEN='your_project_token'For Local:
export PROJECT_TOKEN='your_project_token'git clone https://github.com/LambdaTest/smartui-ruby-selenium-sample
cd smartui-ruby-selenium-sample/sdkInstall the required dependencies:
npm install @lambdatest/smartui-cli
gem install lambdatest-selenium-driver selenium-webdriverDependencies included:
@lambdatest/smartui-cli- SmartUI CLI (via npm)lambdatest-selenium-driver- SmartUI SDK for Selenium Ruby (via gem)selenium-webdriver- Selenium WebDriver for Ruby (via gem)
Note: ChromeDriver is automatically managed by Selenium WebDriver.
npx smartui config:create smartui-web.jsonThe SmartUI screenshot function is already implemented in the repository.
Cloud Test (sdk/sdkCloud.rb):
driver.navigate.to "https://www.lambdatest.com"
Lambdatest::Selenium::Driver.smartui_snapshot(driver, "screenshot")Local Test (sdk/sdkLocal.rb):
driver.navigate.to "https://www.lambdatest.com"
Lambdatest::Selenium::Driver.smartui_snapshot(driver, "screenshot")Note: The code is already configured and ready to use. You can modify the URL and screenshot name if needed.
npx smartui exec ruby sdkLocal.rbnpx smartui exec ruby sdkCloud.rb- Connects to LambdaTest Cloud using Selenium Remote WebDriver
- Reads credentials from environment variables (
LT_USERNAME,LT_ACCESS_KEY) - Takes screenshot with name:
screenshot
- Runs Selenium locally using Chrome
- Requires Chrome browser installed
- Takes screenshot with name:
screenshot
- Demonstrates how to use ignore options in SmartUI snapshots
- Shows how to exclude specific DOM elements from visual comparison
This repository also includes examples for using SmartUI with LambdaTest Hooks integration. Hooks-based integration allows you to use SmartUI directly within your existing LambdaTest Cloud automation tests without requiring the SmartUI CLI.
SDK Approach (Recommended for Local Testing):
- âś… Works with both local and cloud execution
- âś… Uses SmartUI CLI for configuration and execution
- âś… Supports multiple browsers and viewports via
smartui-web.json - âś… Better for CI/CD integration
- âś… Requires
PROJECT_TOKENenvironment variable
Hooks Approach (Recommended for Cloud-Only Testing):
- âś… Works only with LambdaTest Cloud Grid
- âś… No CLI required - direct integration with LambdaTest
- âś… Uses LambdaTest capabilities for configuration
- âś… Better for existing LambdaTest automation suites
- âś… Requires
LT_USERNAMEandLT_ACCESS_KEYenvironment variables
Location: See the hooks folder for hooks integration examples.
Purpose: Enhance visual regression capabilities in your LambdaTest web automation tests running on LambdaTest Cloud Grid.
Documentation: LambdaTest Selenium Visual Regression Documentation.
Install the required Ruby gems:
gem install selenium-webdriverSet your LambdaTest credentials:
export LT_USERNAME='your_username'
export LT_ACCESS_KEY='your_access_key'In your test file (e.g., hooks/smartui_hooks.rb), configure the capabilities with SmartUI options:
require 'selenium-webdriver'
USERNAME = ENV["LT_USERNAME"]
ACCESS_KEY = ENV["LT_ACCESS_KEY"]
options = Selenium::WebDriver::Options.chrome
options.browser_version = "119.0"
options.platform_name = "Windows 10"
lt_options = {
username: USERNAME,
accessKey: ACCESS_KEY,
project: "Ruby Hooks Test",
sessionName: "Ruby Test",
build: "Ruby Job",
w3c: true,
plugin: "ruby-ruby",
'smartUI.project': "Ruby-Project" # Your SmartUI project name
}
options.add_option('LT:Options', lt_options)Create a WebDriver instance connected to LambdaTest Cloud:
driver = Selenium::WebDriver.for(:remote,
url: "https://hub.lambdatest.com/wd/hub",
capabilities: options)Use driver.execute_script() to capture screenshots at any point in your test:
# Navigate to your page
driver.navigate.to "https://www.lambdatest.com/visual-regression-testing"
# Take a screenshot with basic configuration
config = {
'screenshotName' => 'SmartUI-Home',
'fullPage' => true
}
driver.execute_script("smartui.takeScreenshot", config)
# Take a viewport screenshot (visible area only)
viewport_config = {
'screenshotName' => 'SmartUI-Home-Viewport',
'fullPage' => false
}
driver.execute_script("smartui.takeScreenshot", viewport_config)Execute your test script:
cd hooks
ruby smartui_hooks.rbdriver.navigate.to "https://www.lambdatest.com"
# Screenshot 1: Homepage
config1 = {
'screenshotName' => 'homepage',
'fullPage' => true
}
driver.execute_script("smartui.takeScreenshot", config1)
# Navigate and take another screenshot
driver.navigate.to "https://www.lambdatest.com/pricing"
config2 = {
'screenshotName' => 'pricing-page',
'fullPage' => true
}
driver.execute_script("smartui.takeScreenshot", config2)config = {
'screenshotName' => 'homepage-custom',
'fullPage' => true,
'ignore' => ['antialiasing', 'colors']
}
driver.execute_script("smartui.takeScreenshot", config)The SmartUI hooks support various configuration options:
- screenshotName: Name for the screenshot (required)
- fullPage: Set to
truefor full-page screenshot,falsefor viewport only - ignore: Array of visual artifacts to ignore (
"antialiasing","colors", etc.)
After running your hooks-based tests, visit the LambdaTest Automation Dashboard to view:
- Test execution status
- Screenshots captured
- Visual comparison results
- Build and session details
Navigate to your SmartUI project in the SmartUI Dashboard to see detailed visual regression results.
- Use descriptive, unique names for each screenshot
- Follow Ruby naming conventions (snake_case)
- Include page/component name and state
- Avoid special characters
- After critical user interactions
- Before and after form submissions
- At different stages of multi-step processes
- After page state changes
- Use explicit waits before taking screenshots
- Handle exceptions properly with begin/rescue/ensure
- Use descriptive variable names
- Follow Ruby style guidelines
require 'selenium-webdriver'
wait = Selenium::WebDriver::Wait.new(timeout: 10)
wait.until { driver.find_element(id: 'main-content') }
Lambdatest::Selenium::Driver.smartui_snapshot(driver, "homepage")driver.navigate.to "https://example.com/checkout"
Lambdatest::Selenium::Driver.smartui_snapshot(driver, "checkout-step-1")
driver.find_element(id: "next-step").click
wait.until { driver.find_element(id: "step-2").displayed? }
Lambdatest::Selenium::Driver.smartui_snapshot(driver, "checkout-step-2")begin
driver.navigate.to "https://www.lambdatest.com"
Lambdatest::Selenium::Driver.smartui_snapshot(driver, "homepage")
rescue => e
puts "Screenshot failed: #{e.message}"
raise
ensure
driver.quit
endname: Ruby SmartUI Tests
on: [push, pull_request]
jobs:
visual-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.0'
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: |
cd sdk
gem install lambdatest-selenium-driver selenium-webdriver
npm install @lambdatest/smartui-cli
- name: Run SmartUI tests
env:
PROJECT_TOKEN: ${{ secrets.SMARTUI_PROJECT_TOKEN }}
LT_USERNAME: ${{ secrets.LT_USERNAME }}
LT_ACCESS_KEY: ${{ secrets.LT_ACCESS_KEY }}
run: |
cd sdk
npx smartui exec ruby sdkCloud.rbSolution: Install the gem:
gem install lambdatest-selenium-driverSolution: Set the environment variable:
export PROJECT_TOKEN='your_project_token'Solution:
- Update ChromeDriver
- Use WebDriver Manager for automatic driver management
- Ensure Chrome browser is up to date
Solution:
- Verify
LT_USERNAMEandLT_ACCESS_KEYare set correctly - Check credentials in LambdaTest Profile Settings
{
"web": {
"browsers": ["chrome", "firefox"],
"viewports": [
[1920, 1080],
[1366, 768],
[375, 667]
],
"waitForPageRender": 30000,
"waitForTimeout": 2000
}
}After running the tests, visit your SmartUI project dashboard to view the captured screenshots and compare them with baseline builds.