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
1
4
require_relative "config.rb"
2
5
require_relative "logger.rb"
3
6
require 'selenium-webdriver'
@@ -8,11 +11,12 @@ class Driver
8
11
attr_accessor :driver
9
12
$focus_driver = nil
10
13
@driver = nil
11
- @main_window = nil
12
- @click_exception_count = nil
13
14
@@drivers = [ ]
14
15
@@drivers_with_names = { }
15
16
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
16
20
def initialize ( driver_name = "Driver" , browser = Config . browser )
17
21
begin
18
22
start ( driver_name , browser )
@@ -27,7 +31,12 @@ def initialize(driver_name = "Driver", browser = Config.browser)
27
31
##############################
28
32
# Custom methods of driver #
29
33
##############################
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
31
40
def start ( driver_name , browser )
32
41
33
42
case browser
@@ -69,13 +78,14 @@ def start(driver_name, browser)
69
78
70
79
target_size = Selenium ::WebDriver ::Dimension . new ( Config . screen_horizontal , Config . screen_vertical )
71
80
@driver . manage . window . size = target_size
72
- @click_exception_count = 0
73
81
@@drivers . push ( self )
74
82
@@drivers_with_names [ self ] = "#{ driver_name } "
75
83
$focus_driver = self
76
84
return self
77
85
end
78
86
87
+ # This is to generate different user agent for each run
88
+ # This avoids captcha verification to some extent
79
89
def get_user_agent
80
90
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" ,
81
91
"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
85
95
return list_of_user_agents . sample
86
96
end
87
97
98
+ # TO load the browser with specific URL.
88
99
def get ( url )
89
100
$focus_driver = self
90
101
@driver . get ( url )
91
102
Log . info ( "#{ $focus_driver} browser is loaded with - #{ url } " )
92
103
end
93
104
105
+ # To Clear cookies from the current browser driver
94
106
def clear_cookies
95
107
$focus_driver = self
96
108
@driver . manage . delete_all_cookies
97
109
Log . info ( "Cleared cookies from the " + Config . browser + " browser #{ $focus_driver} " )
98
110
end
99
111
112
+ # To quit the current browser driver
100
113
def quit
101
114
Log . info ( "Quiting the browser - #{ $focus_driver} " )
102
115
@driver . quit
103
116
@@drivers . delete ( self )
104
117
end
105
118
119
+ # To refresh the current browser driver
106
120
def refresh
107
121
$focus_driver = self
108
122
navigate . refresh
109
123
Log . info ( "#{ $focus_driver} is refreshed" )
110
124
end
111
125
126
+ # To find element in the current browser driver
112
127
def find_element ( locator )
113
128
$focus_driver = self
114
129
Libraries ::Wait . wait_for_element ( locator )
115
130
return @driver . find_element ( locator . how , locator . what )
116
131
end
117
132
133
+ # To save screenshot of the current browser driver
118
134
def save_screenshot ( file = nil )
119
135
$focus_driver = self
120
136
if file . nil?
@@ -126,20 +142,24 @@ def save_screenshot(file = nil)
126
142
@driver . save_screenshot ( file_name )
127
143
end
128
144
145
+ # To return the current browser driver element. This can be used to perform direct selenium actions over the browser driver
129
146
def self . get_current_driver
130
147
return $focus_driver
131
148
end
132
149
150
+ # To find multiple elements in the current browser. This returns a list of elements found for a specific locator
133
151
def find_elements ( locator )
134
152
$focus_driver = self
135
153
return @driver . find_elements ( locator . how , locator . what )
136
154
end
137
155
156
+ # This is to perform action in the browser driver
138
157
def action
139
158
$focus_driver = self
140
159
return @driver . action
141
160
end
142
161
162
+ # This is to scroll the browser window to a specific element
143
163
def scroll_to_locator ( locator )
144
164
$focus_driver = self
145
165
element = find_element ( locator )
0 commit comments