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 to automate your website testing using RSpec with Selenium on LambdaTest.
This topic will be focusing on:
First, you would have to install Ruby and gem on your local system. Installing these platforms is a little different in each operating system
1 |
$ sudo apt-get install ruby-full |
1 |
$ brew install ruby |
Once you have ruby and gem setup, you would now have to install Selenium dependencies. Use the below gem command for installing Selenium:
1 |
gem install selenium-webdriver |
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 our RSpec 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
//LambdaTest.rb require_relative "readConf" require "json" require "selenium-webdriver" require "rspec" describe "Add Product and checkout" do before(:each) do 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 => "RSpec Sample Test", :build => "RSpec Selenium Sample", :network => true, :visual => true, :video => true, :console => true } 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/" ) sleep(3) end after(:each) do @driver.quit end it "Select a product and add into cart, Checkout" do itemName = 'Yey, Lets add it to list' #Click on First Checkbox @driver.find_element(:name, 'li1').click #Click on Second Checkbox @driver.find_element(:name, 'li2').displayed? #Enter item name @driver.find_element(:id, 'sampletodotext').send_keys itemName @driver.find_element(:id, 'addbutton').displayed? @driver.find_element(:id, 'addbutton').click # Verify Item added successfully textAddedItem = @driver.find_element(:css, "[class='list-unstyled'] li:nth-child(6) span").text expect(textAddedItem).to eq(itemName) end end |
Now, lets create config.conf file.
1 2 3 4 5 6 7 |
//config.conf LT_USERNAME=<YOUR USERNAME> LT_APPKEY=<YOUR ACCESS KEY> LT_OPERATING_SYSTEM = win10 LT_BROWSER=chrome LT_BROWSER_VERSION=63.0 |
Now, add readConfig.rb file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//readConfig.rb 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://cdn.lambdatest.com/support/docs/config.conf') value = my_config.params[key] return value end end |
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 Selenium tests.
You would need to execute the below command in your terminal/cmd.
1 2 3 |
gem bundle bundle install rspec LambdaTest.rb |