Class: Maze::Driver::Browser
- Inherits:
-
Object
- Object
- Maze::Driver::Browser
- Defined in:
- lib/maze/driver/browser.rb
Overview
Handles browser automation fundamentals
Instance Attribute Summary collapse
-
#capabilities ⇒ Object
readonly
Returns the value of attribute capabilities.
Instance Method Summary collapse
-
#driver_quit ⇒ Object
Quits the driver.
- #find_element(*args) ⇒ Object
-
#initialize(driver_for, selenium_url = nil, capabilities = nil) ⇒ Browser
constructor
A new instance of Browser.
-
#javascript? ⇒ Boolean
check if Selenium supports running javascript in the current browser.
-
#local_storage? ⇒ Boolean
check if the browser supports local storage, e.g.
- #navigate ⇒ Object
-
#refresh ⇒ Object
Refreshes the page.
-
#restart_driver(attempts = 6) ⇒ Object
Restarts the underlying-driver in the case an unrecoverable error occurs.
-
#session_id ⇒ Object
Returns the driver session ID.
-
#start_driver(attempts = 6) ⇒ Object
Attempts to create a new selenium driver a given number of times.
Constructor Details
#initialize(driver_for, selenium_url = nil, capabilities = nil) ⇒ Browser
Returns a new instance of Browser.
13 14 15 16 17 18 |
# File 'lib/maze/driver/browser.rb', line 13 def initialize(driver_for, selenium_url=nil, capabilities=nil) capabilities ||= {} @capabilities = capabilities @driver_for = driver_for @selenium_url = selenium_url end |
Instance Attribute Details
#capabilities ⇒ Object (readonly)
Returns the value of attribute capabilities.
11 12 13 |
# File 'lib/maze/driver/browser.rb', line 11 def capabilities @capabilities end |
Instance Method Details
#driver_quit ⇒ Object
Quits the driver
34 35 36 |
# File 'lib/maze/driver/browser.rb', line 34 def driver_quit @driver.quit end |
#find_element(*args) ⇒ Object
20 21 22 |
# File 'lib/maze/driver/browser.rb', line 20 def find_element(*args) @driver.find_element(*args) end |
#javascript? ⇒ Boolean
check if Selenium supports running javascript in the current browser
39 40 41 42 43 |
# File 'lib/maze/driver/browser.rb', line 39 def javascript? @driver.execute_script('return true') rescue Selenium::WebDriver::Error::UnsupportedOperationError false end |
#local_storage? ⇒ Boolean
check if the browser supports local storage, e.g. safari 10 on browserstack does not have working local storage
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/maze/driver/browser.rb', line 47 def local_storage? # Assume we can use local storage if we aren't able to verify by running JavaScript return true unless javascript? @driver.execute_script <<-JAVASCRIPT try { window.localStorage.setItem('__localstorage_test__', 1234) window.localStorage.removeItem('__localstorage_test__') return true } catch (err) { return false } JAVASCRIPT end |
#navigate ⇒ Object
24 25 26 |
# File 'lib/maze/driver/browser.rb', line 24 def navigate @driver.navigate end |
#refresh ⇒ Object
Refreshes the page
29 30 31 |
# File 'lib/maze/driver/browser.rb', line 29 def refresh @driver.navigate.refresh end |
#restart_driver(attempts = 6) ⇒ Object
Restarts the underlying-driver in the case an unrecoverable error occurs
66 67 68 69 70 71 72 |
# File 'lib/maze/driver/browser.rb', line 66 def restart_driver(attempts=6) # Remove the old driver @driver.quit @driver = nil start_driver(attempts) end |
#session_id ⇒ Object
Returns the driver session ID
98 99 100 |
# File 'lib/maze/driver/browser.rb', line 98 def session_id @driver.session_id end |
#start_driver(attempts = 6) ⇒ Object
Attempts to create a new selenium driver a given number of times
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/maze/driver/browser.rb', line 77 def start_driver(attempts=6) timeout = attempts * 10 wait = Maze::Wait.new(interval: 10, timeout: timeout) success = wait.until do begin create_driver(@driver_for, @selenium_url) rescue => error $logger.warn "#{error.class} occurred with message: #{error.}" end @driver end unless success $logger.error "Selenium driver failed to start after #{attempts} attempts in #{timeout} seconds" raise RuntimeError.new("Selenium driver failed to start in #{timeout} seconds") end end |