Skip to content

Commit 05a7ab9

Browse files
comments added
1 parent a567e69 commit 05a7ab9

16 files changed

+136
-25
lines changed

config/global.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# This is the global configuration which this test framework uses at all the runs
2+
# This file is parsed in the libraries/config.rb file and used within the tests
3+
# A capability is added to specify this prams as an environment variable and use the same within the tests
4+
# Params declared as environment variables gets higher preference over the prams mentioned in this file
15
browser: chrome
26
headless: false
37
screen_horizontal: 1280

config/production.yml

+7
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1+
# This file is where environment specific values are declared
2+
# Like this file is to hold the production environment details which can hold url, username, password and other details if needed
3+
# If we need to run the same tests in a different set of environment,
4+
# One can create a new file with new values and use the same to run the tests
5+
# For example, for staging environment run, create a file staging.yml and mention the same during the run as environment variable,
6+
# run as `run_config=staging.yml ruby spec/test_find_professionals.rb` this will pick the values from staging.yml file and run.
7+
# By default producition.yml file is taken for run, one can change that in libraies/config.rb file.
18
url: https://www.upwork.com/

libraries/assert.rb

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1+
# This library file is to have the common assertion methods that are used across the tests
2+
# The idea of this is inherited from rpsec-expectations,
3+
# where we can call the assertions at anypoint in the tests without using any objects
14
require_relative 'logger.rb'
25
include Libraries
36

7+
# This method gets two values as a mandatory input and logs appropriately in the stdout
8+
# If two values are same, then the log will be printed as PASS else FAIL
9+
# One can send the logger message as well if needed
10+
# For example:
11+
# 1. expect_to_equal(1,1)
12+
# -> PASS > 1 is equal to 1
13+
# 1. expect_to_equal(1,2)
14+
# -> FAIL > 1 is not equal to 2
15+
# 1. expect_to_equal(1,1, "custom message")
16+
# -> PASS > custom message
417
def expect_to_equal(value1, value2, message = nil)
518
case value1.class.to_s
619
when "Array"
@@ -24,11 +37,3 @@ def expect_to_equal(value1, value2, message = nil)
2437
end
2538
end
2639
end
27-
28-
def expect_not_to_equal(value1, value2, message = nil)
29-
if value1 != value2
30-
Log.pass(message || "#{value1} is not equal to #{value2}")
31-
else
32-
Log.fail(message || "#{value1} is equal to #{value2}")
33-
end
34-
end

libraries/config.rb

+19
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,69 @@
1+
# This file is where the global configs and run configs are parsed
2+
# Here we are giving preference in the following order
3+
# Environment variable > Vaiable declared in the global.yml file > default value
14
require 'yaml'
25

6+
# All the methods under this config class are class methods
7+
# This gives us flexibility in fethching the values directly across the framework
38
module Libraries
49
class Config
510

11+
# To parse and read the global config and run config files
12+
# Files will be parsed only once irrespective of any number of method calls
13+
# One can direcly use the global vaiables $config or $run_config directly within the tests
14+
# Or use the custom methods for specific entities to have default fallback.
615
def self.read_config_file
716
$config ||= YAML.load_file(File.dirname(__FILE__).split("/libraries")[0] + '/config/global.yml')
817
run_config_file = ENV["run_config"] || "production.yml"
918
$run_config ||= YAML.load_file(File.dirname(__FILE__).split("/libraries")[0] + '/config/' + run_config_file)
1019
end
1120

21+
# To set the logger level that is needed for a run, one can change by running,
22+
#`logger_level=test ruby spec/test_find_professionals.rb`
1223
def self.logger_level
1324
Config.read_config_file
1425
return ENV["logger_level"] || $config["logger_level"] || "info"
1526
end
1627

28+
# To specify which browser to be used for running the tests, change durning run time by,
29+
# `browser=firefox ruby spec/test_find_professionals.rb`
1730
def self.browser
1831
Config.read_config_file
1932
return ENV["browser"] || $config["browser"] || "chrome"
2033
end
2134

35+
# To specify weather to run the tess in headless mode, If we are running the tests in linux, headless mode is forced
2236
def self.headless
2337
Config.read_config_file
2438
linux_check = true if Gem::Platform.local.os.include? "linux"
2539
return linux_check || ENV["headless"] || $config["headless"] || false
2640
end
2741

42+
# To mention the implicit wait that is used by selenium during a page load
2843
def self.implicit_wait
2944
Config.read_config_file
3045
return ENV["implicit_wait"] || $config["implicit_wait"] || 10
3146
end
3247

48+
# To specify the screenshots that is taken during test run path.
3349
def self.screenshot_location
3450
Config.read_config_file
3551
return ENV["screenshot_location"] || $config["screenshot_location"] || "reports/screenshots"
3652
end
3753

54+
# To specify the browser screen horizontal size
3855
def self.screen_horizontal
3956
Config.read_config_file
4057
return $config["screen_horizontal"] || 1200
4158
end
4259

60+
# To specify the browser screen vertical size
4361
def self.screen_vertical
4462
Config.read_config_file
4563
return $config["screen_vertical"] || 700
4664
end
4765

66+
# To return the url used for the tests
4867
def self.url
4968
Config.read_config_file
5069
return ENV["url"] || $run_config["url"]

libraries/driver.rb

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# This file holds the driver methods that are going to be used across this framework
2+
# This is to create a wrapper around the selenium-webdriver in order to combine and use to our convinience
3+
# This wrapper class helps us to use the browser of our choice and could use with high flexibility
14
require_relative "config.rb"
25
require_relative "logger.rb"
36
require 'selenium-webdriver'
@@ -8,11 +11,12 @@ class Driver
811
attr_accessor :driver
912
$focus_driver = nil
1013
@driver = nil
11-
@main_window = nil
12-
@click_exception_count = nil
1314
@@drivers = []
1415
@@drivers_with_names = {}
1516

17+
# While initilizing the driver class a browser driver is initialized of our choice and assigned to the class object
18+
# Ex: chrome_driver = Driver.new
19+
# Ex: firefox_driver = Driver.new(browser="firefox") # or set the firefox variable as environment variable or in the global.yml file
1620
def initialize(driver_name = "Driver", browser = Config.browser)
1721
begin
1822
start(driver_name,browser)
@@ -27,7 +31,12 @@ def initialize(driver_name = "Driver", browser = Config.browser)
2731
##############################
2832
# Custom methods of driver #
2933
##############################
30-
34+
35+
# This is the main method to initialize the browser of our choice along with the specified options
36+
# Specify the browser name and the current driver name to get the browser initiated
37+
# List of browsers supported as of now chrome & firefox
38+
# If you need to initialize the browser in headless mode set the headless true either in global.yml or in the environment variable
39+
# Browser dimentions are also set from the global.yml file. If the tests are supposed to run in different dimentions change the settings promptly
3140
def start(driver_name, browser)
3241

3342
case browser
@@ -69,13 +78,14 @@ def start(driver_name, browser)
6978

7079
target_size = Selenium::WebDriver::Dimension.new(Config.screen_horizontal, Config.screen_vertical)
7180
@driver.manage.window.size = target_size
72-
@click_exception_count=0
7381
@@drivers.push(self)
7482
@@drivers_with_names[self] = "#{driver_name}"
7583
$focus_driver = self
7684
return self
7785
end
7886

87+
# This is to generate different user agent for each run
88+
# This avoids captcha verification to some extent
7989
def get_user_agent
8090
list_of_user_agents = ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246",
8191
"Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36",
@@ -85,36 +95,42 @@ def get_user_agent
8595
return list_of_user_agents.sample
8696
end
8797

98+
# TO load the browser with specific URL.
8899
def get(url)
89100
$focus_driver = self
90101
@driver.get(url)
91102
Log.info("#{$focus_driver} browser is loaded with - #{url}")
92103
end
93104

105+
# To Clear cookies from the current browser driver
94106
def clear_cookies
95107
$focus_driver = self
96108
@driver.manage.delete_all_cookies
97109
Log.info("Cleared cookies from the " + Config.browser + " browser #{$focus_driver}")
98110
end
99111

112+
# To quit the current browser driver
100113
def quit
101114
Log.info("Quiting the browser - #{$focus_driver}")
102115
@driver.quit
103116
@@drivers.delete(self)
104117
end
105118

119+
# To refresh the current browser driver
106120
def refresh
107121
$focus_driver = self
108122
navigate.refresh
109123
Log.info("#{$focus_driver} is refreshed")
110124
end
111125

126+
# To find element in the current browser driver
112127
def find_element(locator)
113128
$focus_driver = self
114129
Libraries::Wait.wait_for_element(locator)
115130
return @driver.find_element(locator.how,locator.what)
116131
end
117132

133+
# To save screenshot of the current browser driver
118134
def save_screenshot(file = nil)
119135
$focus_driver = self
120136
if file.nil?
@@ -126,20 +142,24 @@ def save_screenshot(file = nil)
126142
@driver.save_screenshot(file_name)
127143
end
128144

145+
# To return the current browser driver element. This can be used to perform direct selenium actions over the browser driver
129146
def self.get_current_driver
130147
return $focus_driver
131148
end
132149

150+
# To find multiple elements in the current browser. This returns a list of elements found for a specific locator
133151
def find_elements(locator)
134152
$focus_driver = self
135153
return @driver.find_elements(locator.how,locator.what)
136154
end
137155

156+
# This is to perform action in the browser driver
138157
def action
139158
$focus_driver = self
140159
return @driver.action
141160
end
142161

162+
# This is to scroll the browser window to a specific element
143163
def scroll_to_locator(locator)
144164
$focus_driver = self
145165
element = find_element(locator)

0 commit comments

Comments
 (0)