Class: Driver
- Inherits:
-
Object
- Object
- Driver
- Defined in:
- lib/driver.rb
Constant Summary collapse
- @@driver =
nil
Class Method Summary collapse
- ._save_to_s3_if_configured(screenshot_path) ⇒ Object
- .close_window ⇒ Object
- .current_domain ⇒ Object
- .current_url ⇒ Object
- .driver ⇒ Object
- .driver=(driver) ⇒ Object
- .evaluate_script(script) ⇒ Object
-
.execute_script(script, element) ⇒ Object
Execute Javascript on the element.
-
.execute_script_driver(script) ⇒ Object
Execute Javascript on the page.
- .go_back ⇒ Object
- .go_forward ⇒ Object
- .html ⇒ Object
- .list_open_windows ⇒ Object
- .nav(path) ⇒ Object
- .open_new_window(url) ⇒ Object
- .quit ⇒ Object
- .refresh ⇒ Object
- .reset ⇒ Object
- .s3 ⇒ Object
- .save_screenshot(type = 'saved') ⇒ Object
- .switch_to_frame(by, locator) ⇒ Object
- .switch_to_main_window ⇒ Object
- .switch_to_next_window ⇒ Object
- .switch_to_parent_frame ⇒ Object
- .switch_to_window(title) ⇒ Object
- .title ⇒ Object
- .verify_url(given_url) ⇒ Object
-
.visit(path) ⇒ Object
# Driver Commands # =============== #.
Class Method Details
._save_to_s3_if_configured(screenshot_path) ⇒ Object
206 207 208 209 210 211 |
# File 'lib/driver.rb', line 206 def self._save_to_s3_if_configured(screenshot_path) if Gridium.config.screenshots_to_s3 url = @s3.save_file(screenshot_path) Log.info("#{screenshot_path} saved to #{url}") end end |
.close_window ⇒ Object
229 230 231 232 |
# File 'lib/driver.rb', line 229 def self.close_window Log.debug("Closing window (#{driver.title})...") DriverExtensions.close_window end |
.current_domain ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/driver.rb', line 138 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_url ⇒ Object
129 130 131 |
# File 'lib/driver.rb', line 129 def self.current_url driver.current_url end |
.driver ⇒ Object
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 |
# File 'lib/driver.rb', line 27 def self.driver begin unless @@driver Log.debug("=====> Driver.driver: instantiating new 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) Log.debug("Remote Browser Requested: #{@@driver}") #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 if Gridium.config.screenshots_to_s3 #do stuff s3_project_folder = Gridium.config.project_name_for_s3 s3_subfolder = Gridium.config.subdirectory_name_for_s3 Log.debug("configuring s3 to save files to this directory: #{s3_project_folder} in addition to being saved locally") @s3 = Gridium::GridiumS3.new(s3_project_folder, s3_subfolder) Log.debug("s3 is #{@s3}") else Log.debug("s3 screenshots not enabled in spec_helper; they will be only be saved locally") @s3 = nil 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.}]") $fail_test_instantly = true Kernel.fail(e.) end end |
.driver=(driver) ⇒ Object
72 73 74 75 |
# File 'lib/driver.rb', line 72 def self.driver= driver @@driver.quit if @@driver @@driver = driver end |
.evaluate_script(script) ⇒ Object
192 193 194 |
# File 'lib/driver.rb', line 192 def self.evaluate_script(script) driver.execute_script "return #{script}" end |
.execute_script(script, element) ⇒ Object
Execute Javascript on the element
171 172 173 174 175 176 177 178 179 |
# File 'lib/driver.rb', line 171 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
188 189 190 |
# File 'lib/driver.rb', line 188 def self.execute_script_driver(script) driver.execute_script(script) end |
.go_back ⇒ Object
113 114 115 |
# File 'lib/driver.rb', line 113 def self.go_back driver.navigate.back end |
.go_forward ⇒ Object
117 118 119 |
# File 'lib/driver.rb', line 117 def self.go_forward driver.navigate.forward end |
.html ⇒ Object
121 122 123 |
# File 'lib/driver.rb', line 121 def self.html driver.page_source end |
.list_open_windows ⇒ Object
214 215 216 217 218 219 220 221 222 |
# File 'lib/driver.rb', line 214 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 |
.nav(path) ⇒ Object
100 101 102 103 |
# File 'lib/driver.rb', line 100 def self.nav(path) Log.debug("====> Driver.nav: #{@@driver}") visit(Gridium.config.url + path) end |
.open_new_window(url) ⇒ Object
224 225 226 227 |
# File 'lib/driver.rb', line 224 def self.open_new_window(url) Log.debug("Opening new window and loading url (#{url})...") DriverExtensions.open_new_window(url) end |
.quit ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/driver.rb', line 105 def self.quit if @@driver Log.debug('Shutting down web driver...') @@driver.quit @@driver = nil end end |
.refresh ⇒ Object
133 134 135 |
# File 'lib/driver.rb', line 133 def self.refresh driver.navigate.refresh end |
.reset ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/driver.rb', line 8 def self.reset Log.debug("====> Driver.reset: #{@@driver}") driver.manage. 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 # 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 |
.s3 ⇒ Object
67 68 69 70 |
# File 'lib/driver.rb', line 67 def self.s3 #TODO figure out why I can't just use attr_reader :s3 @s3 end |
.save_screenshot(type = 'saved') ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/driver.rb', line 196 def self.save_screenshot(type = 'saved') Log.debug ("Capturing screenshot of browser...") = Time.now.strftime("%Y_%m_%d__%H_%M_%S") screenshot_path = File.join($current_run_dir, "screenshot__#{}__#{type}.png") driver.save_screenshot(screenshot_path) _save_to_s3_if_configured(screenshot_path) SpecData.screenshots_captured.push("screenshot__#{}__#{type}.png") # used by custom_formatter.rb for embedding in report screenshot_path end |
.switch_to_frame(by, locator) ⇒ Object
264 265 266 267 268 |
# File 'lib/driver.rb', line 264 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_window ⇒ Object
257 258 259 260 261 262 |
# File 'lib/driver.rb', line 257 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_window ⇒ Object
250 251 252 253 254 255 |
# File 'lib/driver.rb', line 250 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_frame ⇒ Object
270 271 272 273 274 |
# File 'lib/driver.rb', line 270 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
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/driver.rb', line 234 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 |
.title ⇒ Object
125 126 127 |
# File 'lib/driver.rb', line 125 def self.title driver.title end |
.verify_url(given_url) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/driver.rb', line 151 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 #
#
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/driver.rb', line 81 def self.visit(path) Log.debug("====> Driver.Visit: #{@@driver}") 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.} - Also be sure to check the url formatting. http:// is required for proper test execution (www is optional).") end end |