Creating our own Selenium Ruby Framework from Scratch
Kushan Shalindra Amarasiri
Director Quality Engineering at Social Catfish
Selenium is still dominating in the test automation arena and it can be used to create scripts in variety of languages. Today, I'm going to enlighten you on how to create a Selenium Ruby framework which I created on my own.
So the first step to do scripting in Ruby is to install Ruby on your machine. You have to download Ruby from the following location
Next step is to install Selenium via Ruby Gems via command prompt
gem install selenium-webdriver -v 2.53.4
Next we need an IDE like RubyMine to code and develop our framework.
My next task was to run a kick off script as follows
require "selenium-webdriver"
#Chome browser instantiation
driver = Selenium::WebDriver.for :chrome
#Loading the Google URL
driver.navigate.to "https://www.google.lk"
#Typing the Selenium in Google search
SearchInput = driver.find_element(:name, "q")
SearchInput.send_keys "Selenium"
#Clicking the search button
SearchButton = driver.find_element(:name, "btnK")
SearchButton.click
#Clicking the search button
GoogleLink = driver.find_element(:xpath => "https://*[@id='rso']/div[1]/div/div[1]/div/div/div[1]/a/h3")
GoogleLink.click
#Asserting whether the registration success message is diaplyed
SuccessMessage = driver.title
puts SuccessMessage
Next I designed the page object model in Selenium Ruby. Following is the login page class
require 'selenium-webdriver'
require 'json'
class LoginPage
USER_NAME = { name: 'uid'}
PASSWORD = { name: 'password'}
LOGIN_BUTTON = { name: 'btnLogin'}
attr_reader :driver
def initialize(driver)
@driver = driver
end
def visit
driver.get ENV['base_url']
end
def login_site()
@file = File.read( "../Data/data.json")
@data_hash = JSON.parse(@file)
driver.find_element(USER_NAME).send_keys @data_hash['user_name']
driver.find_element(PASSWORD).send_keys @data_hash['password']
driver.find_element(LOGIN_BUTTON).click
end
end
Next I created the home page class
require 'selenium-webdriver'
class HomePage
HOMEPAGE_HEADING = { xpath: '/html/body/div[2]/h2' }
attr_reader :driver
def initialize(driver)
@driver = driver
end
def verify_heading()
if driver.find_element(HOMEPAGE_HEADING).displayed?
puts "Successfully logged in"
end
end
end
My next objective was to make the framework data driven. So I choose JSON data driven mechanism as it provides faster data reading capabilities.
{
"URL": "https://demo.guru99.com/V4/",
"user_name": "mngr184115",
"password" : "azUrupE"
}
So next I added the JSON data parsing to my test script
require_relative '../Pages/LoginPage'
require_relative '../Pages/HomePage'
require_relative '../Utility/report'
require 'json'
require 'selenium-webdriver'
@driver = Selenium::WebDriver.for(:chrome)
@file = File.read( "../Data/data.json")
@data_hash = JSON.parse(@file)
url_site = @data_hash['URL']
create_report
insert_head_title ("Report for the Ruby Selenium Automation Framework - Kushan Amarasiri")
insert_reportname_date("My Test Report",$result_date )
start_table
@driver.navigate.to(url_site)
report_row("Navigated to the login page","Navigated to the login page","Chrome","PASS", @driver.title)
login = LoginPage.new(@driver)
login.login_site
homepage = HomePage.new(@driver)
homepage.verify_heading
if homepage.verify_heading == true
report_row("Navigated to home page","Navigated to home page","Chrome","PASS", "Home page navigation")
else
report_row("Navigated to home page","Navigated to home page","Chrome","FAIL", "Login Failed")
end
close_table
Final touch of the framework was to add the reporting capability and I used the HTML reporting into the framework.
require_relative '../Pages/LoginPage'
require_relative '../Pages/HomePage'
require_relative '../Utility/report'
require 'json'
require 'selenium-webdriver'
@driver = Selenium::WebDriver.for(:chrome)
@file = File.read( "../Data/data.json")
@data_hash = JSON.parse(@file)
url_site = @data_hash['URL']
create_report
insert_head_title ("Report for the Ruby Selenium Automation Framework - Kushan Amarasiri")
insert_reportname_date("My Test Report",$result_date )
start_table
@driver.navigate.to(url_site)
report_row("Navigated to the login page","Navigated to the login page","Chrome","PASS", @driver.title)
login = LoginPage.new(@driver)
login.login_site
homepage = HomePage.new(@driver)
homepage.verify_heading
if homepage.verify_heading == true
report_row("Navigated to home page","Navigated to home page","Chrome","PASS", "Home page navigation")
else
report_row("Navigated to home page","Navigated to home page","Chrome","FAIL", "Login Failed")
end
close_table
It generated the following report finally
Therefore I was successfully complete the creation of Ruby Selenium framework on my own.
Test Manager,Program Manager, Delivery Manager ,BA,PM,SM, Generative AI ?? Visa: H1B stamped till 2025 ?? Expertise: MSC, P&C, Guidewire, Duck Creek, Velocity, Insurity Policy Admin Experience 17+ Years
5 年Can you please share phython link...
Test Manager,Program Manager, Delivery Manager ,BA,PM,SM, Generative AI ?? Visa: H1B stamped till 2025 ?? Expertise: MSC, P&C, Guidewire, Duck Creek, Velocity, Insurity Policy Admin Experience 17+ Years
5 年Nice