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
- #click_element(id) ⇒ Object
-
#driver_quit ⇒ Object
Quits the driver.
-
#fail_driver ⇒ Object
Marks the driver as failed.
-
#failed? ⇒ Boolean
Whether the driver has known to have failed (it may still have failed and us not know yet).
- #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.
- #set_implicit_wait(timeout_seconds) ⇒ Object
-
#start_driver(attempts = 6) ⇒ Object
Attempts to create a new selenium driver a given number of times.
- #wait_for_element(id) ⇒ Object
Constructor Details
#initialize(driver_for, selenium_url = nil, capabilities = nil) ⇒ Browser
Returns a new instance of Browser.
14 15 16 17 18 19 20 |
# File 'lib/maze/driver/browser.rb', line 14 def initialize(driver_for, selenium_url=nil, capabilities=nil) capabilities ||= {} @failed = false @capabilities = capabilities @driver_for = driver_for @selenium_url = selenium_url end |
Instance Attribute Details
#capabilities ⇒ Object (readonly)
Returns the value of attribute capabilities.
12 13 14 |
# File 'lib/maze/driver/browser.rb', line 12 def capabilities @capabilities end |
Instance Method Details
#click_element(id) ⇒ Object
48 49 50 |
# File 'lib/maze/driver/browser.rb', line 48 def click_element(id) @driver.find_element(id: id).click end |
#driver_quit ⇒ Object
Quits the driver
62 63 64 |
# File 'lib/maze/driver/browser.rb', line 62 def driver_quit @driver.quit end |
#fail_driver ⇒ Object
Marks the driver as failed
28 29 30 31 |
# File 'lib/maze/driver/browser.rb', line 28 def fail_driver $logger.error 'Selenium driver failed, remaining scenarios will be skipped' @failed = true end |
#failed? ⇒ Boolean
Whether the driver has known to have failed (it may still have failed and us not know yet)
23 24 25 |
# File 'lib/maze/driver/browser.rb', line 23 def failed? @failed end |
#find_element(*args) ⇒ Object
40 41 42 |
# File 'lib/maze/driver/browser.rb', line 40 def find_element(*args) @driver.find_element(*args) end |
#javascript? ⇒ Boolean
check if Selenium supports running javascript in the current browser
67 68 69 70 71 |
# File 'lib/maze/driver/browser.rb', line 67 def javascript? !Maze.config.browser.nil? && @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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/maze/driver/browser.rb', line 75 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
52 53 54 |
# File 'lib/maze/driver/browser.rb', line 52 def navigate @driver.navigate end |
#refresh ⇒ Object
Refreshes the page
57 58 59 |
# File 'lib/maze/driver/browser.rb', line 57 def refresh @driver.navigate.refresh end |
#restart_driver(attempts = 6) ⇒ Object
Restarts the underlying-driver in the case an unrecoverable error occurs
94 95 96 97 98 99 100 |
# File 'lib/maze/driver/browser.rb', line 94 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
126 127 128 |
# File 'lib/maze/driver/browser.rb', line 126 def session_id @driver.session_id end |
#set_implicit_wait(timeout_seconds) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/maze/driver/browser.rb', line 33 def set_implicit_wait(timeout_seconds) @driver.manage.timeouts.implicit_wait = timeout_seconds rescue # Not all browsers support setting implicit wait $logger.warn 'Failed to set implicit wait on Selenium driver' end |
#start_driver(attempts = 6) ⇒ Object
Attempts to create a new selenium driver a given number of times
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/maze/driver/browser.rb', line 105 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 |
#wait_for_element(id) ⇒ Object
44 45 46 |
# File 'lib/maze/driver/browser.rb', line 44 def wait_for_element(id) @driver.find_element(id: id) end |