Module: BrowserSetup

Defined in:
lib/test_utils/browser_tests/browser_setup.rb

Instance Method Summary collapse

Instance Method Details

#chrome_driver_pathObject

Sets Chromedriver path



95
96
97
98
99
100
101
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 95

def chrome_driver_path
  case os
    when :win32 then File.join(File.dirname(__FILE__), "../../../resources/drivers/chrome/#{os}/chromedriver.exe")
    when :mac, :linux64 then File.join(File.dirname(__FILE__), "../../../resources/drivers/chrome/#{os}/chromedriver")
    else fail 'Invalid OS'
  end
end

#close_browser(scenario = nil) ⇒ Object

Closes the browser Takes screenshot and prints relevant information before closing



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 123

def close_browser(scenario = nil)
  puts 'closing browser'
  if !scenario.nil? && !@browser.nil?
    if @current_page.nil?
      file_name = "img-#{DateTime.now.strftime("%Y%m%d_%H%M%S")}.png"
      @browser.screenshot.save file_name
      embed(file_name, 'image/png')
    else
      file_name = "#{@current_page.class}-#{DateTime.now.strftime("%Y%m%d_%H%M%S")}.png"
      @current_page.save_screenshot(file_name)
      embed(file_name, 'image/png')
    end
    if scenario.failed?
      print_session_storage
      print_console_errors
    end
  end
  @browser.close unless @browser.nil?
end

#mobile?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 103

def mobile?
  bs_mobile?(ENV['BS']) || platform == :mobile
end

#osObject

Get the system OS



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 108

def os
  @os ||= (
  host_os = RbConfig::CONFIG['host_os']
  case host_os
    when /mswin|msys|mingw|cygwin|bccwin|wince|emc/ then :win32
    when /darwin|mac os/ then :mac
    when /linux/ then :linux64
    when /solaris|bsd/ then :unix
    else raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
  end
  )
end

Prints errors in the console



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 178

def print_console_errors
  logs = @browser.driver.manage.logs.get(:browser)
  if logs.length > 0
    puts "\nConsole errors (#{logs.length}):"
    logs.each do |error|
      t = error.timestamp
      time = Time.at(t.to_s[0..-4].to_i)
      puts "- #{error.message} (#{error.level}) (#{time})"
    end
  else
    puts 'No Console errors found.'
  end
end

Prints all sessionStorage data



167
168
169
170
171
172
173
174
175
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 167

def print_session_storage
  storage = get_all_session_storage
  if storage.nil? || storage.empty?
    puts 'Session storage: Empty.'
  else
    puts "Session storage items (#{storage.length}):"
    storage.each { |key, value| puts " - #{key} => #{value}" }
  end
end

#quit_browserObject

Takes screenshot and prints relevant information before closing



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 144

def quit_browser()
  puts 'quitting browser session'
  # First, try to quit the browser the normal way
  begin
    Timeout::timeout(2) { @browser.close }
  rescue Timeout::Error
    # Well, that didn't work. Bring in the artillery!
    browser_pid = @browser.driver.instance_variable_get(:@bridge).instance_variable_get(:@service).instance_variable_get(:@process).pid
    begin
      ::Process.kill('KILL', browser_pid)
    ensure
      @browser.close
    end
  end
end

#setup_browser_stack(client) ⇒ Object

Set up browser stack



78
79
80
81
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 78

def setup_browser_stack(client)
  client.timeout = 120 # Page load timeout
  @browser = Watir::Browser.new(:remote, :url => bs_remote_url, :desired_capabilities => bs_caps, :http_client => client)
end

#setup_chrome(client) ⇒ Object

Set up Chrome



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 50

def setup_chrome(client)
  times_retried = 0
  max_retries = 3
  begin
    Selenium::WebDriver::Chrome.driver_path = chrome_driver_path
    @browser = Watir::Browser.new(:chrome, :http_client => client, :switches => %w[--no-sandbox])
  rescue Net::ReadTimeout => error
    if times_retried < max_retries
      puts '### ERROR: Net::ReadTimeout on Browser Initialize (Watir::Browser.new) ###'
      puts '### RETRYING ... ###'
      sleep 2
      times_retried += 1
      retry
    else
      puts '### ERROR: Net::ReadTimeout on Browser Initialize (Watir::Browser.new) ###'
      p error
      fail "Browser failed to initialize after #{(times_retried + 1)} attempts. ###"
    end
  end
end

#setup_chrome_mobile(client) ⇒ Object



71
72
73
74
75
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 71

def setup_chrome_mobile(client)
  Selenium::WebDriver::Chrome.driver_path = chrome_driver_path
  driver = Webdriver::UserAgent.driver(:browser => :chrome, :agent => :iphone, :orientation => :portrait)
  @browser = Watir::Browser.new(driver, :http_client => client)
end

#setup_firefox(client) ⇒ Object

Set up firefox



43
44
45
46
47
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 43

def setup_firefox(client)
  trusted_profile = Selenium::WebDriver::Firefox::Profile.new
  trusted_profile.native_events = true
  @browser = Watir::Browser.new(:firefox, :profile => trusted_profile, :http_client => client)
end

#start_browserObject

Starts a Browser Checks ‘run_test_in_browser_stack?’ to determine if it should run in BrowserStack Checks ‘headless’ to determine if it should start a headless display Otherwise, starts a local browser, checks ‘browser’ to determine which browser to use



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 19

def start_browser
  client = Selenium::WebDriver::Remote::Http::Default.new
  if run_test_in_browser_stack?
    setup_browser_stack(client)
  else
    start_headless_display if headless == :true
    # Local test run
    client.timeout = 120 # Page load timeout
    case platform
      when :firefox
        setup_firefox(client)
      when :chrome
        setup_chrome(client)
      when :mobile
        setup_chrome_mobile(client)
      else fail 'Invalid browser'
    end
  end

  @browser.driver.manage.timeouts.implicit_wait = 5
  @browser.driver.manage.window.resize_to(1440, 900) unless mobile?
end

#start_headless_displayObject

Starts a headless display



84
85
86
87
88
89
90
91
92
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 84

def start_headless_display
  require 'headless'
  display_number = ENV['DISPLAY_PORT'].to_i || 99
  headless = Headless.new(display: display_number)
  headless.start
  at_exit do
    headless.destroy
  end
end

#take_screenshotObject

Save screenshot



161
162
163
164
# File 'lib/test_utils/browser_tests/browser_setup.rb', line 161

def take_screenshot
  file_name = "img-#{DateTime.now.strftime("%Y%m%d_%H%M%S")}.png"
  @browser.screenshot.save file_name
end