Class: Driver

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

Constant Summary collapse

@@driver =
nil

Class Method Summary collapse

Class Method Details

.close_windowObject



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

def self.close_window
  Log.debug("Closing window (#{driver.title})...")
  DriverExtensions.close_window
end

.current_domainObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/driver.rb', line 115

def self.current_domain
  site_url = driver.current_url.to_s
  # domain = site_url.match(/(https?:\/\/)?(\S*\.)?([\w\d]*\.\w+)\/?/i)[3]
  domain = URI.parse(site_url)
  host = domain.host
  if (!host.nil?)
    Log.debug("Current domain is: (#{host}).")
    return host
  else
    Log.error("Unable to parse URL.")
  end
end

.current_urlObject



106
107
108
# File 'lib/driver.rb', line 106

def self.current_url
  driver.current_url
end

.driverObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/driver.rb', line 25

def self.driver
  begin
    unless @@driver
      @browser_type = Gridium.config.browser
      ##Adding support for remote browsers
      if Gridium.config.browser_source == :remote
        @@driver = Selenium::WebDriver.for(:remote, url: Gridium.config.target_environment, desired_capabilities: Gridium.config.browser)
        #this file detector is only used for remote drivers and is needed to upload files from test_host through Grid to browser
        @@driver.file_detector = lambda do |args|
          str = args.first.to_s
          str if File.exist?(str)
        end
      else
        @@driver = Selenium::WebDriver.for(Gridium.config.browser)
      end
      reset
    end
    @@driver
  rescue Exception => e
    Log.debug(e.backtrace.inspect)
    Log.info("Driver did not load within (#{Gridium.config.page_load_timeout}) seconds.  [#{e.message}]")
    $fail_test_instantly = true
    Kernel.fail(e.message)
  end
end

.driver=(driver) ⇒ Object



51
52
53
54
# File 'lib/driver.rb', line 51

def self.driver= driver
  @@driver.quit if @@driver
  @@driver = driver
end

.evaluate_script(script) ⇒ Object



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

def self.evaluate_script(script)
  driver.execute_script "return #{script}"
end

.execute_script(script, element) ⇒ Object

Execute Javascript on the element

Parameters:

  • script (String)
    • Javascript source to execute

  • element (Element)

Returns:

  • The value returned from the script



148
149
150
151
152
153
154
155
156
# File 'lib/driver.rb', line 148

def self.execute_script(script, element)
  if element.is_a?(Element)
    #we dont care if Gridium.config.visible_elements_only is set to true or not
    ele = driver.find_element(element.by, element.locator)
    driver.execute_script(script, ele)
  else
    driver.execute_script(script, element)
  end
end

.execute_script_driver(script) ⇒ Object

Execute Javascript on the page

Parameters:

  • script (String)
    • Javascript source to execute

Returns:

  • The value returned from the script



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

def self.execute_script_driver(script)
  driver.execute_script(script)
end

.go_backObject



90
91
92
# File 'lib/driver.rb', line 90

def self.go_back
  driver.navigate.back
end

.go_forwardObject



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

def self.go_forward
  driver.navigate.forward
end

.htmlObject



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

def self.html
  driver.page_source
end

.list_open_windowsObject



181
182
183
184
185
186
187
188
189
# File 'lib/driver.rb', line 181

def self.list_open_windows
  handles = driver.window_handles
  Log.debug("List of active windows:")
  handles.each do |handle|
    driver.switch_to.window(handle)
    Log.debug("|  Window with title: (#{driver.title}) and handle: #{handle} is currently open.")
  end
  driver.switch_to.window(driver.window_handles.first)
end


78
79
80
# File 'lib/driver.rb', line 78

def self.nav(path)
  visit(Gridium.config.url + path)
end

.open_new_window(url) ⇒ Object



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

def self.open_new_window(url)
  Log.debug("Opening new window and loading url (#{url})...")
  DriverExtensions.open_new_window(url)
end

.quitObject



82
83
84
85
86
87
88
# File 'lib/driver.rb', line 82

def self.quit
  if @@driver
    Log.debug('Shutting down web driver...')
    @@driver.quit
    @@driver = nil
  end
end

.refreshObject



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

def self.refresh
  driver.navigate.refresh
end

.resetObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/driver.rb', line 8

def self.reset
  driver.manage.delete_all_cookies
  driver.manage.timeouts.page_load = Gridium.config.page_load_timeout
  driver.manage.timeouts.implicit_wait = Gridium.config.element_timeout

  # Ensure the browser is maximized to maximize visibility of element
  # Currently doesn't work with chromedriver, but the following workaround does:
  if @browser_type.eql?(:chrome)
    width = driver.execute_script("return screen.width;")
    height = driver.execute_script("return screen.height;")
    driver.manage.window.move_to(0, 0)
    driver.manage.window.resize_to(width, height)
  else
    driver.manage.window.maximize
  end
end

.save_screenshot(type = 'saved') ⇒ Object



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

def self.save_screenshot(type = 'saved')
  Log.debug ("Capturing screenshot of browser...")
  timestamp = Time.now.strftime("%Y_%m_%d__%H_%M_%S")
  screenshot_path = File.join($current_run_dir, "screenshot__#{timestamp}__#{type}.png")
  driver.save_screenshot(screenshot_path)
  SpecData.screenshots_captured.push("screenshot__#{timestamp}__#{type}.png")   # used by custom_formatter.rb for embedding in report
end

.switch_to_frame(by, locator) ⇒ Object



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

def self.switch_to_frame(by, locator)
  Log.debug("Attempting to switch to Frame at: #{locator}")
  driver.switch_to.frame(driver.find_element(by, locator))
  Log.debug("Frame at: #{locator} is now active frame!")
end

.switch_to_main_windowObject



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

def self.switch_to_main_window
  current_title = driver.title
  Log.debug("Current window is: (#{current_title}).  Switching to main window...")
  driver.switch_to.window(driver.window_handles.first)
  Log.debug("Window (#{driver.title}) is now the active window.")
end

.switch_to_next_windowObject



217
218
219
220
221
222
# File 'lib/driver.rb', line 217

def self.switch_to_next_window
  current_title = driver.title
  Log.debug("Current window is: (#{current_title}).  Switching to next window...")
  driver.switch_to.window(driver.window_handles.last)
  Log.debug("Window (#{driver.title}) is now the active window.")
end

.switch_to_parent_frameObject



237
238
239
240
241
# File 'lib/driver.rb', line 237

def self.switch_to_parent_frame
  Log.debug("Switching back to main parent frame")
  driver.switch_to.parent_frame
  Log.debug("Now back to Parent Frame")
end

.switch_to_window(title) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/driver.rb', line 201

def self.switch_to_window(title)
  current_title = driver.title
  Log.debug("Current window is: (#{current_title}).  Switching to next window (#{title})...")
  handles = driver.window_handles
  driver.switch_to.window(handles.first)
  handles.each do |handle|
    driver.switch_to.window(handle)
    if driver.title == title
      Log.debug("Window (#{driver.title}) is now the active window.")
      return
    end
  end
  list_open_windows
  Log.error("Unable to switch to window with title (#{title}).")
end

.titleObject



102
103
104
# File 'lib/driver.rb', line 102

def self.title
  driver.title
end

.verify_url(given_url) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/driver.rb', line 128

def self.verify_url(given_url)
  Log.debug('Verifying URL...')
  current_url = self.current_url.to_s
  current_domain = self.current_domain.to_s
  if current_url.include?(given_url)
    Log.debug("Confirmed. (#{current_url}) includes (#{given_url}).")
    $verification_passes += 1
  else
    Log.error("(#{current_url}) does not include (#{given_url}).")
  end
end

.visit(path) ⇒ Object

#

Driver Commands #

#


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/driver.rb', line 60

def self.visit(path)
  begin
    if path
      Log.debug("Navigating to url: (#{path}).")
      driver
      time_start = Time.now
      driver.navigate.to(path)
      time_end = Time.new
      page_load = (time_end - time_start)
      Log.debug("Page loaded in (#{page_load}) seconds.")
      $verification_passes += 1
    end
  rescue Exception => e
    Log.debug(e.backtrace.inspect)
    Log.error("#{e.message} - Also be sure to check the url formatting.  http:// is required for proper test execution (www is optional).")
  end
end