Class: TestCentricity::WebDriverConnect

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/testcentricity_web/web_core/webdriver_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#bs_localObject

Returns the value of attribute bs_local.



11
12
13
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 11

def bs_local
  @bs_local
end

#capabilitiesObject

Returns the value of attribute capabilities.



15
16
17
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 15

def capabilities
  @capabilities
end

#downloads_pathObject

Returns the value of attribute downloads_path.



13
14
15
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 13

def downloads_path
  @downloads_path
end

#driversObject

Returns the value of attribute drivers.



16
17
18
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 16

def drivers
  @drivers
end

#endpointObject

Returns the value of attribute endpoint.



14
15
16
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 14

def endpoint
  @endpoint
end

#initializedObject

Returns the value of attribute initialized.



12
13
14
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 12

def initialized
  @initialized
end

Class Method Details

.activate_driver(name) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 122

def self.activate_driver(name)
  name = name.gsub(/\s+/, '_').downcase.to_sym if name.is_a?(String)
  return if Environ.driver_name == name

  driver_state = @drivers[name]
  raise "Could not find a driver named '#{name}'" if driver_state.nil?

  Environ.restore_driver_state(driver_state)
  Capybara.current_driver = name
  Capybara.default_driver = name
  Environ.driver_name = name
end

.close_all_driversObject

Close all browsers and terminate all driver instances



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 143

def self.close_all_drivers
  return if @drivers.nil?
  @drivers.each do |key, _value|
    Environ.restore_driver_state(@drivers[key])
    Capybara.current_driver = key
    Capybara.default_driver = key
    Capybara.page.driver.quit
    Capybara.current_session.quit
    @drivers.delete(key)
  end
  Capybara.reset_sessions!
  Environ.session_state = :quit
  Capybara.current_driver = nil
  @drivers = {}
  @initialized = false
end

.close_tunnelObject



207
208
209
210
211
212
213
214
215
216
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 207

def self.close_tunnel
  unless @bs_local.nil?
    @bs_local.stop
    if @bs_local.isRunning
      raise 'BrowserStack Local instance could not be stopped'
    else
      puts 'BrowserStack Local instance has been stopped'
    end
  end
end

.downloads_pathObject



160
161
162
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 160

def self.downloads_path
  @downloads_path
end

.driver_exists?(driver_name) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 112

def self.driver_exists?(driver_name)
  if @drivers.nil?
    false
  else
    driver_name = driver_name.gsub(/\s+/, '_').downcase.to_sym if driver_name.is_a?(String)
    driver_state = @drivers[driver_name]
    !driver_state.nil?
  end
end

.initialize_browser_sizeObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 164

def self.initialize_browser_size
  # tile browser windows if BROWSER_TILE environment variable is true and running in multiple parallel threads
  if ENV['BROWSER_TILE'] && (Environ.parallel || @drivers.length > 1)
    thread = if Environ.parallel
               ENV['TEST_ENV_NUMBER'].to_i
             else
               @drivers.length
             end
    if thread > 1
      Browsers.set_browser_window_position(100 * thread - 1, 100 * thread - 1)
      sleep(1)
    end
  else
    Browsers.set_browser_window_position(10, 10)
    sleep(1)
  end

  browser = Environ.browser.to_s
  if Environ.is_desktop?
    case
    when ENV['BROWSER_SIZE'] == 'max'
      Browsers.maximize_browser
    when ENV['BROWSER_SIZE']
      Browsers.set_browser_window_size(ENV['BROWSER_SIZE'])
    when Environ.browser_size == 'max'
      Browsers.maximize_browser
    when Environ.browser_size
      Browsers.set_browser_window_size(Environ.browser_size)
    else
      Browsers.set_browser_window_size(Browsers.browser_size(browser, Environ.device_orientation))
    end
  elsif Environ.is_mobile? && !Environ.is_device?
    Browsers.set_browser_window_size(Browsers.browser_size(browser, Environ.device_orientation))
  end
  Environ.session_state = :running
end

.initialize_web_driver(options = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 18

def self.initialize_web_driver(options = nil)
  @initialized ||= false
  initialize_downloads unless @initialized
  @endpoint = nil
  @capabilities = nil
  Environ.driver_name = nil
  Environ.device_orientation = nil
  if options.is_a?(String)
    Capybara.app_host = options
  elsif options.is_a?(Hash)
    Capybara.app_host = options[:app_host] if options.key?(:app_host)
    @endpoint = options[:endpoint] if options.key?(:endpoint)
    @capabilities = options[:capabilities] if options.key?(:capabilities)
    Environ.driver = options[:driver] if options.key?(:driver)
    Environ.driver_name = options[:driver_name] if options.key?(:driver_name)
    Environ.device_type = options[:device_type] if options.key?(:device_type)
    Environ.browser_size = if options.key?(:browser_size)
      options[:browser_size]
    else
      ENV['BROWSER_SIZE'] if ENV['BROWSER_SIZE']
    end
  end
  # determine browser type and driver
  if @capabilities.nil?
    Environ.driver = ENV['DRIVER'].downcase.to_sym if ENV['DRIVER']
    Environ.browser = ENV['WEB_BROWSER'] if ENV['WEB_BROWSER']
    Environ.device_orientation = ENV['ORIENTATION'] if ENV['ORIENTATION']
  else
    if @capabilities[:browserName]
      Environ.browser = @capabilities[:browserName]
    else
      raise 'Missing :browserName in @capabilities'
    end
    Environ.device_orientation = @capabilities[:orientation] if @capabilities[:orientation]
    Environ.device_orientation = @capabilities[:'appium:orientation'] if @capabilities[:'appium:orientation']
  end
  Environ.browser = Environ.browser.downcase.to_sym if Environ.browser.is_a?(String)
  Environ.driver = :webdriver if Environ.driver.nil?
  Environ.device_type = ENV['DEVICE_TYPE'] if ENV['DEVICE_TYPE']
  # assume that we're testing within a local desktop web browser
  unless Environ.driver == :custom
    Environ.platform    = :desktop
    Environ.headless    = false
    Environ.device      = :web
    Environ.device_name = 'browser'
  end

  context = case Environ.driver
            when :appium
              initialize_appium
              "#{Environ.device_os} #{Environ.device_type} #{Environ.device} on Appium"
            when :webdriver
              initialize_local_browser
              'local browser instance'
            when :selenium_grid, :grid
              initialize_remote
              'Selenium Grid'
            when :browserstack
              initialize_browserstack
              'Browserstack cloud service'
            when :testingbot
              initialize_testingbot
              'TestingBot cloud service'
              # :nocov:
            when :saucelabs
              initialize_saucelabs
              'Sauce Labs cloud service'
            when :lambdatest
              initialize_lambdatest
              'LambdaTest cloud service'
              # :nocov:
            when :custom
              raise 'User-defined webdriver requires that you define options' if options.nil?

              initialize_custom_driver
              'custom user-defined webdriver'
            else
              raise "#{Environ.driver} is not a supported driver"
            end

  # set browser window size only if testing with a desktop web browser
  unless Environ.is_device? || Environ.is_simulator? || Environ.driver == :appium
    initialize_browser_size
  end

  # set Capybara's server port if PARALLEL_PORT is true
  Capybara.server_port = 9887 + ENV['TEST_ENV_NUMBER'].to_i if Environ.parallel

  Environ.session_state = :running
  puts "Using #{Environ.browser} browser via #{context}"
  # add the new driver to the driver queue
  @drivers[Environ.driver_name] = Environ.driver_state
end

.num_driversInteger

Return the number of driver instances

Returns:

  • (Integer)


138
139
140
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 138

def self.num_drivers
  @drivers.nil? ? 0 : @drivers.length
end

.set_domain(url) ⇒ Object

:nocov:



203
204
205
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 203

def self.set_domain(url)
  Capybara.app_host = url
end