Class: Maze::Driver::Browser

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

Overview

Handles browser automation fundamentals

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#capabilitiesObject (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



41
42
43
44
45
46
47
48
49
# File 'lib/maze/driver/browser.rb', line 41

def click_element(id)
  element = @driver.find_element(id: id)

  if $browser.mobile?
    element.click
  else
    @driver.action.move_to(element).click.perform
  end
end

#driver_quitObject

Quits the driver



61
62
63
# File 'lib/maze/driver/browser.rb', line 61

def driver_quit
  @driver.quit
end

#fail_driverObject

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)

Returns:

  • (Boolean)


23
24
25
# File 'lib/maze/driver/browser.rb', line 23

def failed?
  @failed
end

#find_element(*args) ⇒ Object



33
34
35
# File 'lib/maze/driver/browser.rb', line 33

def find_element(*args)
  @driver.find_element(*args)
end

#javascript?Boolean

check if Selenium supports running javascript in the current browser

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/maze/driver/browser.rb', line 66

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

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/maze/driver/browser.rb', line 74

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


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

def navigate
  @driver.navigate
end

#refreshObject

Refreshes the page



56
57
58
# File 'lib/maze/driver/browser.rb', line 56

def refresh
  @driver.navigate.refresh
end

#restart_driver(attempts = 6) ⇒ Object

Restarts the underlying-driver in the case an unrecoverable error occurs

Parameters:

  • attempts (Integer) (defaults to: 6)

    The number of times we should retry a failed attempt (defaults to 6)



93
94
95
96
97
98
99
# File 'lib/maze/driver/browser.rb', line 93

def restart_driver(attempts=6)
  # Remove the old driver
  @driver.quit
  @driver = nil

  start_driver(attempts)
end

#session_idObject

Returns the driver session ID



125
126
127
# File 'lib/maze/driver/browser.rb', line 125

def session_id
  @driver.session_id
end

#start_driver(attempts = 6) ⇒ Object

Attempts to create a new selenium driver a given number of times

Parameters:

  • attempts (Integer) (defaults to: 6)

    The number of times we should retry a failed attempt (defaults to 6)



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/maze/driver/browser.rb', line 104

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.message}"
    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



37
38
39
# File 'lib/maze/driver/browser.rb', line 37

def wait_for_element(id)
  @driver.find_element(id: id)
end