Class: RoboTest::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/robotest/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 = , opts = {}) ⇒ Driver

Returns a new instance of Driver.



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

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

Instance Attribute Details

#driverObject

Returns the value of attribute driver.



4
5
6
# File 'lib/robotest/driver.rb', line 4

def driver
  @driver
end

Class Method Details

.get_all_driversObject



309
310
311
# File 'lib/robotest/driver.rb', line 309

def self.get_all_drivers
  return @@drivers_with_names
end

.quit_all_driversObject



302
303
304
305
306
307
# File 'lib/robotest/driver.rb', line 302

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



337
338
339
# File 'lib/robotest/driver.rb', line 337

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

Instance Method Details

#actionObject



169
170
171
172
# File 'lib/robotest/driver.rb', line 169

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

#alert(ok_cancel) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/robotest/driver.rb', line 313

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



226
227
228
229
# File 'lib/robotest/driver.rb', line 226

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

#browserObject



181
182
183
184
# File 'lib/robotest/driver.rb', line 181

def browser
  $focus_driver = self
  @driver.browser
end

#capabilitiesObject



186
187
188
189
# File 'lib/robotest/driver.rb', line 186

def capabilities
  $focus_driver = self
  @driver.capabilities
end

#closeObject



281
282
283
284
285
# File 'lib/robotest/driver.rb', line 281

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

#current_urlObject



191
192
193
194
# File 'lib/robotest/driver.rb', line 191

def current_url
  $focus_driver = self
  @driver.current_url
end

#drag_and_drop(source_locator, target_locator) ⇒ Object



341
342
343
344
345
346
347
348
# File 'lib/robotest/driver.rb', line 341

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

#enable_chrome_headless_downloads(directory) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/robotest/driver.rb', line 121

def enable_chrome_headless_downloads(directory)
  bridge = @driver.send(:bridge)
  path = '/session/:session_id/chromium/send_command'
  path[':session_id'] = bridge.session_id
  bridge.http.call(:post, path, {
                     "cmd" => "Page.setDownloadBehavior",
                     "params" => {
                       "behavior" => "allow",
                       "downloadPath" => directory,
                     }
  })
end

#execute_async_script(script, *args) ⇒ Object



196
197
198
199
# File 'lib/robotest/driver.rb', line 196

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

#execute_script(script) ⇒ Object



201
202
203
204
# File 'lib/robotest/driver.rb', line 201

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

#find_element(locator) ⇒ Object



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

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

#find_elements(locator) ⇒ Object



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

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

#get(url) ⇒ Object



134
135
136
137
138
# File 'lib/robotest/driver.rb', line 134

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

#inspectObject



206
207
208
209
# File 'lib/robotest/driver.rb', line 206

def inspect
  $focus_driver = self
  @driver.inspect
end

#is_alert_present?Boolean

Returns:

  • (Boolean)


327
328
329
330
331
332
333
334
335
# File 'lib/robotest/driver.rb', line 327

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

#manageObject



211
212
213
214
# File 'lib/robotest/driver.rb', line 211

def manage
  $focus_driver = self
  @driver.manage
end

#mouseObject



164
165
166
167
# File 'lib/robotest/driver.rb', line 164

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

#mouse_over(locator, index = 1) ⇒ Object



157
158
159
160
161
162
# File 'lib/robotest/driver.rb', line 157

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



174
175
176
177
178
179
# File 'lib/robotest/driver.rb', line 174

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


216
217
218
219
# File 'lib/robotest/driver.rb', line 216

def navigate
  $focus_driver = self
  @driver.navigate
end

#onObject



350
351
352
353
# File 'lib/robotest/driver.rb', line 350

def on
  $focus_driver = self
  yield
end

#page_sourceObject



221
222
223
224
# File 'lib/robotest/driver.rb', line 221

def page_source
  $focus_driver = self
  @driver.page_source
end

#quitObject



287
288
289
290
291
292
# File 'lib/robotest/driver.rb', line 287

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

#quit_allObject



294
295
296
297
298
299
300
# File 'lib/robotest/driver.rb', line 294

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

#refreshObject



140
141
142
143
144
# File 'lib/robotest/driver.rb', line 140

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

#revert_to(window = nil) ⇒ Object



270
271
272
273
274
275
276
277
278
279
# File 'lib/robotest/driver.rb', line 270

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



231
232
233
234
235
236
# File 'lib/robotest/driver.rb', line 231

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



262
263
264
265
266
267
268
# File 'lib/robotest/driver.rb', line 262

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, opts = {}) ⇒ Object

Custom methods of robotest #



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/robotest/driver.rb', line 27

def start(driver_name, browser, opts = {})
  dimensions = $conf['dimensions']

  if (!dimensions['horizontal'] or !dimensions['vertical'])
    dimensions = {'horizontal' => 1366, 'vertical' => 768}
  end

  if (!$conf['implicit_wait'])
    puts "Specify implicit_wait for your project in the .yml files"
    $conf['implicit_wait'] = 20
  end

  prefs = {}
  if opts.empty?
    switches = $conf['switches']
    prefs = $conf['prefs'] if $conf.key?('prefs')
  elsif opts.key?(:switches)
    switches = opts[:switches]
  elsif opts.key?(:prefs)
    prefs = opts[:prefs]
  end

  case browser

  when 'firefox', 'ff'
    options = Selenium::WebDriver::Firefox::Options.new
    switches.map { |k| options.add_argument(k) }
    if prefs['profile']
      if prefs['profile']['name']
        profile = Selenium::WebDriver::Firefox::Profile.from_name(prefs['profile']['name'])
        options.profile = profile
      end
    end
    if prefs['download']
      options.add_preference("browser.download.folderList", 2)
      options.add_preference("browser.download.dir", "#{Pathname.pwd}/#{prefs['download']['default_directory']}")
      options.add_preference("browser.download.manager.alertOnEXEOpen", false)
      options.add_preference("browser.helperApps.neverAsk.saveToDisk" , "application/msword,application/csv,text/csv,image/png ,image/jpeg, application/pdf, text/html,text/plain,application/octet-stream")
    end
    caps = Selenium::WebDriver::Remote::Capabilities.firefox(
      'marionette' => true,
      'moz:useNonSpecCompliantPointerOrigin' => false,
      'moz:webdriverClick' => false
    )
    @driver = Selenium::WebDriver.for :firefox, :desired_capabilities => caps, options: options
    
  when 'ie', 'internet_explorer'
    caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer('ie.ensureCleanSession' => true, 'ie.browserCommandLineSwitches' => 'private')
    @driver = Selenium::WebDriver.for(:internet_explorer, :desired_capabilities => caps)

  when 'edge'
    @driver = Selenium::WebDriver.for :edge

  when 'chrome'
    options = Selenium::WebDriver::Chrome::Options.new
    # options.add_preference(:download, prefs["download"]) if prefs["download"]
    if prefs
      prefs.each do |key, value|
        options.add_preference(key, value)
      end
    end

    if opts.key?(:extension)
      options.add_extension(opts[:extension])
    end
    switches.map { |k| options.add_argument(k) }
    @driver = Selenium::WebDriver.for(:chrome, options: options)
    if prefs["download"]
      enable_chrome_headless_downloads("#{Pathname.pwd}/#{prefs["download"]["default_directory"]}")
    else
      enable_chrome_headless_downloads(Pathname.pwd)
      puts "WARNING: Download path is set to the current root folder if it is not specified in the config file.\nExample: "
      puts "prefs:
                download:
                  default_directory: downloads/"
    end

  when 'safari'
    @driver = Selenium::WebDriver.for(:safari, opts)

  else
    raise ArgumentError, "Specify a proper browser while initiating a driver \n \n#{browser.inspect}"
  end

  target_size = Selenium::WebDriver::Dimension.new(dimensions["horizontal"], dimensions["vertical"])
  @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



238
239
240
241
242
243
244
# File 'lib/robotest/driver.rb', line 238

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



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/robotest/driver.rb', line 246

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