Class: Teber::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/teber/driver.rb

Constant Summary collapse

@@drivers =
[]
@@drivers_with_names =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver_name = "Driver", browser = ) ⇒ Driver

Returns a new instance of Driver.



11
12
13
14
15
16
17
18
19
20
# File 'lib/teber/driver.rb', line 11

def initialize(driver_name = "Driver", browser = $conf["browser"])
  begin
    start(driver_name,browser)
    puts "#{driver_name} is initialized"
  rescue Exception => e
    puts "#{driver_name} is failed to initialize \n\n #{e.backtrace}\n\nRetrying to initialize #{driver_name}"
    start(driver_name,browser)
    puts "#{driver_name} is initialized after an exception"
  end
end

Instance Attribute Details

#driverObject

Returns the value of attribute driver.



3
4
5
# File 'lib/teber/driver.rb', line 3

def driver
  @driver
end

Class Method Details

.direct_add_to_allure_env(params) ⇒ Object



73
74
75
76
77
# File 'lib/teber/driver.rb', line 73

def self.direct_add_to_allure_env(params)
  location =  File.join("#{Pathname.pwd}","reports/allure/environment.properties")
  file = File.open(location,"a")
  file.puts "#{params}\n"
end

.get_all_driversObject



255
256
257
# File 'lib/teber/driver.rb', line 255

def self.get_all_drivers
  return @@drivers_with_names
end

.get_current_driverObject



259
260
261
# File 'lib/teber/driver.rb', line 259

def self.get_current_driver
  return $focus_driver
end

.quit_all_driversObject



248
249
250
251
252
253
# File 'lib/teber/driver.rb', line 248

def self.quit_all_drivers
  @@drivers.each do |driver|
    driver.quit if driver != self
  end
  puts "deleted all the browsers"
end

.switch_to(driver) ⇒ Object



287
288
289
# File 'lib/teber/driver.rb', line 287

def self.switch_to(driver)
  $focus_driver = driver
end

Instance Method Details

#actionObject



115
116
117
118
# File 'lib/teber/driver.rb', line 115

def action
  $focus_driver = self
  return @driver.action
end

#add_to_allure_env(params) ⇒ Object



67
68
69
70
71
# File 'lib/teber/driver.rb', line 67

def add_to_allure_env(params)
  location =  File.join("#{Pathname.pwd}","reports/allure/environment.properties")
  file = File.open(location,"a")
  file.puts "#{params}\n"
end

#alert(ok_cancel) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/teber/driver.rb', line 263

def alert(ok_cancel)
  sleep 2
  alert = @driver.switch_to.alert
  alertMsg=alert.text
  if ok_cancel
    alert.accept
    puts "The alert was accepted in #{$focus_driver} with alert message #{alertMsg}"
  else
    alert.dismiss
    puts "The alert was dismissed in #{$focus_driver} with alert message #{alertMsg}"
  end
  return alertMsg
end

#body_textObject



172
173
174
175
# File 'lib/teber/driver.rb', line 172

def body_text
  $focus_driver = self
  @driver.find_element(:css, 'body').text
end

#browserObject



127
128
129
130
# File 'lib/teber/driver.rb', line 127

def browser
  $focus_driver = self
  @driver.browser
end

#capabilitiesObject



132
133
134
135
# File 'lib/teber/driver.rb', line 132

def capabilities
  $focus_driver = self
  @driver.capabilities
end

#closeObject



227
228
229
230
231
# File 'lib/teber/driver.rb', line 227

def close
  $focus_driver = self
  @driver.close
  puts "Closed the browser - #{$focus_driver}"
end

#current_urlObject



137
138
139
140
# File 'lib/teber/driver.rb', line 137

def current_url
  $focus_driver = self
  @driver.current_url
end

#drag_and_drop(source_locator, target_locator) ⇒ Object



291
292
293
294
295
296
297
298
# File 'lib/teber/driver.rb', line 291

def drag_and_drop(source_locator, target_locator)
  source = find_element(source_locator)
  target = find_element(target_locator)
  @driver.action.click_and_hold(source).perform
  @driver.action.move_to(target).release.perform
  sleep 3
  puts "In driver #{$focus_driver} - #{source_locator.how} => source_locator.what locator was dragged and moved to this locator #{target_locator.how} => #{target_locator.what}"
end

#execute_async_script(script, *args) ⇒ Object



142
143
144
145
# File 'lib/teber/driver.rb', line 142

def execute_async_script(script, *args)
  $focus_driver = self
  @driver.execute_async_script(script, *args)
end

#execute_script(script, *args) ⇒ Object



147
148
149
150
# File 'lib/teber/driver.rb', line 147

def execute_script(script, *args)
  $focus_driver = self
  @driver.execute_script(script, *args)
end

#find_element(locator) ⇒ Object



92
93
94
95
96
# File 'lib/teber/driver.rb', line 92

def find_element(locator)
  $focus_driver = self
  Teber::Wait.wait_for_element(locator)
  return @driver.find_element(locator.how,locator.what)
end

#find_elements(locator) ⇒ Object



98
99
100
101
# File 'lib/teber/driver.rb', line 98

def find_elements(locator)
  $focus_driver = self
  return @driver.find_elements(locator.how,locator.what)
end

#get(url) ⇒ Object



79
80
81
82
83
84
# File 'lib/teber/driver.rb', line 79

def get(url)
  $focus_driver = self
  @driver.get(url)
  add_to_allure_env("URL = #{url}")
  puts "#{$focus_driver} loaded with - #{url}"
end

#inspectObject



152
153
154
155
# File 'lib/teber/driver.rb', line 152

def inspect
  $focus_driver = self
  @driver.inspect
end

#is_alert_present?Boolean

Returns:

  • (Boolean)


277
278
279
280
281
282
283
284
285
# File 'lib/teber/driver.rb', line 277

def is_alert_present?
  begin
    alert = @driver.switch_to.alert
    alertMsg=alert.text
    return true
  rescue Exception => e
    return false
  end
end

#manageObject



157
158
159
160
# File 'lib/teber/driver.rb', line 157

def manage
  $focus_driver = self
  @driver.manage
end

#mouseObject



110
111
112
113
# File 'lib/teber/driver.rb', line 110

def mouse
  $focus_driver = self
  return @driver.mouse
end

#mouse_over(locator, index = 1) ⇒ Object



103
104
105
106
107
108
# File 'lib/teber/driver.rb', line 103

def mouse_over(locator,index=1)
  $focus_driver = self
  element=find_elements(locator)[index-1]
  @driver.action.move_to(element).perform
  puts "mouse over for the element - #{locator.how} => #{locator.what} is done"
end

#move_and_click(locator) ⇒ Object



120
121
122
123
124
125
# File 'lib/teber/driver.rb', line 120

def move_and_click(locator)
  $focus_driver = self
  ele=find_element(locator)
  @driver.action.move_to(ele).click.perform
  puts "Mouse over the locator and then click for - #{locator.how} => #{locator.what} is done"
end


162
163
164
165
# File 'lib/teber/driver.rb', line 162

def navigate
  $focus_driver = self
  @driver.navigate
end

#page_sourceObject



167
168
169
170
# File 'lib/teber/driver.rb', line 167

def page_source
  $focus_driver = self
  @driver.page_source
end

#quitObject



233
234
235
236
237
238
# File 'lib/teber/driver.rb', line 233

def quit
  @driver.quit
  @@drivers.delete(self)
  $focus_driver = @@drivers[0]
  puts "Quit the browser - #{$focus_driver}"
end

#quit_allObject



240
241
242
243
244
245
246
# File 'lib/teber/driver.rb', line 240

def quit_all
  @@drivers.each do |driver|
    driver.quit if driver != self
  end
  self.quit
  puts "deleted all the browsers"
end

#refreshObject



86
87
88
89
90
# File 'lib/teber/driver.rb', line 86

def refresh
  $focus_driver = self
  navigate.refresh
  puts "#{$focus_driver} is refreshed"
end

#revert_to(window = nil) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'lib/teber/driver.rb', line 216

def revert_to(window=nil)
  $focus_driver = self
  if window != nil
    @driver.switch_to.window(window)
    puts "Switched back to another window - #{window} in #{$focus_driver}"
  else
    @driver.switch_to.window(@main_window)
    puts "Switched back to main window in #{focus_driver}"
  end
end

#save_screenshot(file_name = nil) ⇒ Object



177
178
179
180
181
182
# File 'lib/teber/driver.rb', line 177

def save_screenshot(file_name = nil)
  $focus_driver = self
  file_name = "#{Pathname.pwd}/#{$conf['screenshot_location']}/#{Time.new.strftime("%Y-%m-%d-%H-%M-%S-%L-%N")}.png" if file_name.nil?
  puts "#{$focus_driver}'s Screenshot saved in this path => #{file_name}"
  @driver.save_screenshot(file_name)
end

#scroll_to_locator(locator) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/teber/driver.rb', line 208

def scroll_to_locator(locator)
  $focus_driver = self
  element = find_element(locator)
  @driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center', inline: 'nearest'});",element)
  puts "Scroll to this locator - #{locator.how} => #{locator.what} on #{$focus_driver}"
  sleep 1
end

#start(driver_name, browser) ⇒ Object

Custom methods of driver #



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
# File 'lib/teber/driver.rb', line 26

def start(driver_name, browser)
  
  case browser
    
  when 'chrome'
    options = Selenium::WebDriver::Chrome::Options.new
    if ENV['MODE'] == "headless" or $conf["mode"] == "headless"
      switches = ["disable-infobars", "disable-gpu", "disable-dev-shm-usage", "no-sandbox", "headless"]
    else
      switches = ["disable-infobars", "disable-gpu", "disable-dev-shm-usage", "no-sandbox"]
    end
    switches.map { |k| options.add_argument(k) }
    @driver = Selenium::WebDriver.for(:chrome, options: options)
    @driver.manage.timeouts.implicit_wait = $conf["implicit_wait"]
    
  when 'firefox', 'ff'
      # to be added
  
  when 'ie', 'internet_explorer'
      # to be added
  
  when 'edge'
      # to be added 
  
  when 'safari'
      # to be added
    
  else
    raise ArgumentError, "Specify a proper browser while initiating a driver \n \n#{browser.inspect}"
  end
    
  target_size = Selenium::WebDriver::Dimension.new(1200, 700)
  @driver.manage.window.size = target_size
  @click_exception_count=0
  @@drivers.push(self)
  @@drivers_with_names[self] = "#{driver_name}"
  $focus_driver = self
  puts "#{driver_name} - #{self}"
  return self
end

#switch_to_frame(locator) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/teber/driver.rb', line 184

def switch_to_frame(locator)
  $focus_driver = self
  @main_window=@driver.window_handle
  @driver.switch_to.frame(find_element(locator))
  puts "Switched to iframe - #{locator.how} => #{locator.what} on #{$focus_driver}"
  return @main_window
end

#switch_to_window(locator = nil) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/teber/driver.rb', line 192

def switch_to_window(locator=nil)
  $focus_driver = self
  @main_window=@driver.window_handle
  locator.click if locator != nil
  windows=@driver.window_handles
  new_window=nil;
  windows.length.times do |i|
    if windows[i] != @main_window
      new_window=windows[i]
    end
  end
  @driver.switch_to.window(new_window)
  puts "Switched to new window on #{$focus_driver}"
  return @main_window
end