Skip to main content

TestUnit with Selenium: Run TestUnit Automation Scripts on Selenium Grid Cloud


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 perform Selenium Automation testing using TestUnit on LambdaTest.

This topic will be focusing on:

  1. Running a sample TestUnit automation script on LambdaTest Automation.
  2. Setting up environment for testing your locally hosted web pages or website using TestUnit automation.
  3. Core Capabilities: understanding and configuring them in your Selenium test suite.
  4. Exploring advanced features of LambdaTest.

Prerequisites For Running TestUnit With Selenium#


All the code samples in this documentation can be found in the TestUnit LambdaTest Repository on Image GitHub. You can either download or clone the repository to quickly run your tests.

First, you would have to install Ruby 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 from here.
  • 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
  • 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 TestUnit Automation#


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

Getting Started Using TestUnit With Selenium & 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 ourTestUnit Automation 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.

require 'rubygems'require 'selenium-webdriver'require 'test/unit'require_relative  "readConf" class UnitTestLTSample < 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 =>  "UnitTest Sample Test",            :build =>  "UnitTest Selenium Test",              }          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

Now, lets create config.conf file.

LT_USERNAME=<YOUR USERNAME>LT_APPKEY=<YOUR ACCESS KEY>LT_OPERATING_SYSTEM = win10LT_BROWSER=chromeLT_BROWSER_VERSION=63.0

Now, add readConfig.rb file.

require ('rubygems')require ('parseconfig') class ReadConfig    #function to read .conf file    def getDetails(key)            my_config = ParseConfig.new( File.expand_path(File.dirname(__FILE__)) +'https://www.lambdatest.com/support/docs/config.conf')        value = my_config.params[key]        return value    endend

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 TestUnit automation tests.

Execute The Tests Using TestUnit Automation#


To perform the tests using TestUnit with Selenium, you would need to execute the below command in your terminal/cmd.

gem bundlebundle installruby UnitTestLTSample.rb