Class: TestCentricity::WebDriverConnect
- Includes:
- Capybara::DSL
- Defined in:
- lib/testcentricity_web/web_core/webdriver_helper.rb
Instance Attribute Summary collapse
-
#bs_local ⇒ Object
Returns the value of attribute bs_local.
-
#capabilities ⇒ Object
Returns the value of attribute capabilities.
-
#downloads_path ⇒ Object
Returns the value of attribute downloads_path.
-
#drivers ⇒ Object
Returns the value of attribute drivers.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#initialized ⇒ Object
Returns the value of attribute initialized.
Class Method Summary collapse
- .activate_driver(name) ⇒ Object
- .chrome_edge_options(browser) ⇒ Object
-
.close_all_drivers ⇒ Object
Close all browsers and terminate all driver instances.
- .close_tunnel ⇒ Object
- .config_file_uploads ⇒ Object
- .downloads_path ⇒ Object
- .driver_exists?(driver_name) ⇒ Boolean
- .driver_name ⇒ Object
- .firefox_options(browser) ⇒ Object
- .initialize_appium ⇒ Object
- .initialize_browser_size ⇒ Object
- .initialize_browserstack ⇒ Object
-
.initialize_custom_driver ⇒ Object
:nocov:.
- .initialize_downloads ⇒ Object
- .initialize_lambdatest ⇒ Object
- .initialize_local_browser ⇒ Object
- .initialize_remote ⇒ Object
-
.initialize_saucelabs ⇒ Object
:nocov:.
- .initialize_testingbot ⇒ Object
- .initialize_web_driver(options = nil) ⇒ Object
-
.num_drivers ⇒ Integer
Return the number of driver instances.
- .register_remote_driver(browser, options) ⇒ Object
-
.set_domain(url) ⇒ Object
:nocov:.
- .test_context_message ⇒ Object
Instance Attribute Details
#bs_local ⇒ Object
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 |
#capabilities ⇒ Object
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_path ⇒ Object
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 |
#drivers ⇒ Object
Returns the value of attribute drivers.
16 17 18 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 16 def drivers @drivers end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
14 15 16 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 14 def endpoint @endpoint end |
#initialized ⇒ Object
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 |
.chrome_edge_options(browser) ⇒ Object
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 727 def self.(browser) = case browser when :chrome, :chrome_headless Selenium::WebDriver::Chrome::Options.new(exclude_switches: ['enable-automation']) when :edge, :edge_headless Selenium::WebDriver::Edge::Options.new(exclude_switches: ['enable-automation']) else raise "#{browser} is not a valid selector" end prefs = { prompt_for_download: false, directory_upgrade: true, default_directory: @downloads_path } .add_preference(:download, prefs) .add_argument('--force-device-scale-factor=1') .add_argument('--disable-search-engine-choice-screen') .add_argument('--disable-geolocation') .add_argument('--disable-dev-shm-usage') .add_argument('--no-sandbox') .add_argument("--lang=#{ENV['LOCALE']}") if ENV['LOCALE'] if browser == :chrome_headless || browser == :edge_headless Environ.headless = true .add_argument('--headless=new') .add_argument('--disable-gpu') end end |
.close_all_drivers ⇒ Object
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_tunnel ⇒ Object
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 201 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 |
.config_file_uploads ⇒ Object
826 827 828 829 830 831 832 833 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 826 def self.config_file_uploads # configure file_detector for remote uploads selenium = Capybara.page.driver.browser selenium.file_detector = lambda do |args| str = args.first.to_s str if File.exist?(str) end end |
.downloads_path ⇒ Object
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
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 |
.driver_name ⇒ Object
794 795 796 797 798 799 800 801 802 803 804 805 806 807 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 794 def self.driver_name unless Environ.driver_name driver = case Environ.driver when :webdriver :local when :selenium_grid, :grid :remote else Environ.driver end Environ.driver_name = "#{driver}_#{Environ.browser}".downcase.to_sym unless driver.nil? end Environ.driver_name end |
.firefox_options(browser) ⇒ Object
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 756 def self.(browser) profile = Selenium::WebDriver::Firefox::Profile.new profile['browser.download.dir'] = @downloads_path profile['browser.download.folderList'] = 2 profile['browser.download.manager.showWhenStarting'] = false profile['browser.download.manager.closeWhenDone'] = true profile['browser.download.manager.showAlertOnComplete'] = false profile['browser.download.manager.alertOnEXEOpen'] = false profile['browser.download.manager.useWindow'] = false profile['browser.helperApps.alwaysAsk.force'] = false profile['pdfjs.disabled'] = true mime_types = ENV['MIME_TYPES'] || 'images/jpeg, application/pdf, application/octet-stream' profile['browser.helperApps.neverAsk.saveToDisk'] = mime_types profile['intl.accept_languages'] = ENV['LOCALE'] if ENV['LOCALE'] = Selenium::WebDriver::Firefox::Options.new(profile: profile) if browser == :firefox_headless Environ.headless = true .args << '--headless' end end |
.initialize_appium ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 243 def self.initialize_appium Environ.platform = :mobile Environ.device = :simulator # define capabilities caps = if @capabilities.nil? Environ.browser = ENV['APP_BROWSER'] Environ.device_name = ENV['APP_DEVICE'] Environ.device_os = ENV['APP_PLATFORM_NAME'].downcase.to_sym Environ.device_os_version = ENV['APP_VERSION'] caps = { platformName: Environ.device_os, browserName: Environ.browser, 'appium:platformVersion': Environ.device_os_version, 'appium:deviceName': Environ.device_name } caps[:'appium:avd'] = ENV['APP_DEVICE'] if Environ.device_os == :android caps[:'appium:automationName'] = ENV['AUTOMATION_ENGINE'] if ENV['AUTOMATION_ENGINE'] if ENV['UDID'] # :nocov: Environ.device = :device caps[:'appium:udid'] = ENV['UDID'] caps[:'appium:xcodeOrgId'] = ENV['TEAM_ID'] if ENV['TEAM_ID'] caps[:'appium:xcodeSigningId'] = ENV['TEAM_NAME'] if ENV['TEAM_NAME'] # :nocov: else caps[:'appium:orientation'] = Environ.device_orientation.upcase if Environ.device_orientation if Environ.device_os == :ios caps[:'appium:language'] = Environ.language if Environ.language caps[:'appium:locale'] = Environ.locale.gsub('-', '_') if Environ.locale end end caps[:'appium:safariIgnoreFraudWarning'] = ENV['APP_IGNORE_FRAUD_WARNING'] if ENV['APP_IGNORE_FRAUD_WARNING'] caps[:'appium:safariInitialUrl'] = ENV['APP_INITIAL_URL'] if ENV['APP_INITIAL_URL'] caps[:'appium:safariAllowPopups'] = ENV['APP_ALLOW_POPUPS'] if ENV['APP_ALLOW_POPUPS'] caps[:'appium:shutdownOtherSimulators'] = ENV['SHUTDOWN_OTHER_SIMS'] if ENV['SHUTDOWN_OTHER_SIMS'] caps[:'appium:forceSimulatorSoftwareKeyboardPresence'] = ENV['SHOW_SIM_KEYBOARD'] if ENV['SHOW_SIM_KEYBOARD'] caps[:'appium:autoAcceptAlerts'] = ENV['AUTO_ACCEPT_ALERTS'] if ENV['AUTO_ACCEPT_ALERTS'] caps[:'appium:autoDismissAlerts'] = ENV['AUTO_DISMISS_ALERTS'] if ENV['AUTO_DISMISS_ALERTS'] caps[:'appium:isHeadless'] = ENV['HEADLESS'] if ENV['HEADLESS'] caps[:'appium:newCommandTimeout'] = ENV['NEW_COMMAND_TIMEOUT'] if ENV['NEW_COMMAND_TIMEOUT'] caps[:'appium:noReset'] = ENV['APP_NO_RESET'] if ENV['APP_NO_RESET'] caps[:'appium:fullReset'] = ENV['APP_FULL_RESET'] if ENV['APP_FULL_RESET'] caps[:'appium:webkitDebugProxyPort'] = ENV['WEBKIT_DEBUG_PROXY_PORT'] if ENV['WEBKIT_DEBUG_PROXY_PORT'] caps[:'appium:webDriverAgentUrl'] = ENV['WEBDRIVER_AGENT_URL'] if ENV['WEBDRIVER_AGENT_URL'] caps[:'appium:usePrebuiltWDA'] = ENV['USE_PREBUILT_WDA'] if ENV['USE_PREBUILT_WDA'] caps[:'appium:useNewWDA'] = ENV['USE_NEW_WDA'] if ENV['USE_NEW_WDA'] caps[:'appium:chromedriverExecutable'] = ENV['CHROMEDRIVER_EXECUTABLE'] if ENV['CHROMEDRIVER_EXECUTABLE'] # set wdaLocalPort (iOS) or systemPort (Android) if PARALLEL_PORT is true if Environ.parallel # :nocov: if Environ.device_os == :ios caps[:'appium:wdaLocalPort'] = 8100 + ENV['TEST_ENV_NUMBER'].to_i else caps[:'appium:systemPort'] = 8200 + ENV['TEST_ENV_NUMBER'].to_i end # :nocov: else caps[:'appium:wdaLocalPort'] = ENV['WDA_LOCAL_PORT'] if ENV['WDA_LOCAL_PORT'] caps[:'appium:systemPort'] = ENV['SYSTEM_PORT'] if ENV['SYSTEM_PORT'] end caps else Environ.device_os = @capabilities[:platformName] Environ.device_os_version = @capabilities[:'appium:platformVersion'] Environ.device_name = @capabilities[:'appium:deviceName'] @capabilities end # specify endpoint url @endpoint = 'http://127.0.0.1:4723' if @endpoint.nil? register_remote_driver(Environ.browser, caps) end |
.initialize_browser_size ⇒ Object
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_browserstack ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 407 def self.initialize_browserstack # determine browser type Environ.browser = if @capabilities.nil? ENV['BS_BROWSER'] if ENV['BS_BROWSER'] else @capabilities[:browserName] end browser = Environ.browser Environ.grid = :browserstack if ENV['BS_REAL_MOBILE'] || ENV['BS_DEVICE'] Environ.platform = :mobile Environ.device_name = ENV['BS_DEVICE'] Environ.device_os = ENV['BS_OS'] Environ.device = if ENV['BS_REAL_MOBILE'] :device else :simulator end end # specify endpoint url @endpoint = "https://#{ENV['BS_USERNAME']}:#{ENV['BS_AUTHKEY']}@hub-cloud.browserstack.com/wd/hub" if @endpoint.nil? # enable tunneling if specified if ENV['TUNNELING'] @bs_local = BrowserStack::Local.new bs_local_args = {'key' => "#{ENV['BS_AUTHKEY']}"} @bs_local.start(bs_local_args) if @bs_local.isRunning puts 'BrowserStack Local instance has been started' else puts 'BrowserStack Local instance failed to start' end end # define BrowserStack options = if @capabilities.nil? Environ.os = "#{ENV['BS_OS']} #{ENV['BS_OS_VERSION']}" = {} # define the required set of BrowserStack options = { userName: ENV['BS_USERNAME'], accessKey: ENV['BS_AUTHKEY'], sessionName: , os: ENV['BS_OS'], osVersion: ENV['BS_OS_VERSION'] } # define browser specific BrowserStack options case browser.downcase.to_sym when :safari [:enablePopups] = ENV['ALLOW_POPUPS'] if ENV['ALLOW_POPUPS'] [:allowAllCookies] = ENV['ALLOW_COOKIES'] if ENV['ALLOW_COOKIES'] [:safari] = unless .empty? when :ie [:enablePopups] = ENV['ALLOW_POPUPS'] if ENV['ALLOW_POPUPS'] [:ie] = unless .empty? when :edge [:enablePopups] = ENV['ALLOW_POPUPS'] if ENV['ALLOW_POPUPS'] [:edge] = unless .empty? end # define the optional BrowserStack options [:projectName] = ENV['AUTOMATE_PROJECT'] if ENV['AUTOMATE_PROJECT'] [:buildName] = ENV['AUTOMATE_BUILD'] if ENV['AUTOMATE_BUILD'] [:headless] = ENV['HEADLESS'] if ENV['HEADLESS'] [:timezone] = ENV['TIME_ZONE'] if ENV['TIME_ZONE'] [:geoLocation] = ENV['IP_GEOLOCATION'] if ENV['IP_GEOLOCATION'] [:video] = ENV['RECORD_VIDEO'] if ENV['RECORD_VIDEO'] [:debug] = ENV['SCREENSHOTS'] if ENV['SCREENSHOTS'] [:networkLogs] = ENV['NETWORK_LOGS'] if ENV['NETWORK_LOGS'] [:local] = ENV['TUNNELING'] if ENV['TUNNELING'] [:deviceOrientation] = ENV['ORIENTATION'] if ENV['ORIENTATION'] [:appiumLogs] = ENV['APPIUM_LOGS'] if ENV['APPIUM_LOGS'] [:realMobile] = ENV['BS_REAL_MOBILE'] if ENV['BS_REAL_MOBILE'] # define mobile device options if ENV['BS_DEVICE'] [:deviceName] = ENV['BS_DEVICE'] [:appiumVersion] = '2.19.0' { browserName: browser, 'bstack:options': } else # define desktop browser options [:resolution] = ENV['RESOLUTION'] if ENV['RESOLUTION'] [:seleniumVersion] = '4.35.0' { browserName: browser, browserVersion: ENV['BS_VERSION'], 'bstack:options': } end else = @capabilities[:'bstack:options'] Environ.os = "#{[:os]} #{[:osVersion]}" if .key?(:deviceName) Environ.platform = :mobile Environ.device_name = [:deviceName] Environ.device_os = [:osVersion] Environ.device = if .key?(:realMobile) :device else :simulator end Environ.device_orientation = [:deviceOrientation] if .key?(:deviceOrientation) end @capabilities end register_remote_driver(browser, ) # configure file_detector for remote uploads if target is desktop web browser config_file_uploads if Environ.platform == :desktop Environ.tunneling = ENV['TUNNELING'] if ENV['TUNNELING'] end |
.initialize_custom_driver ⇒ Object
:nocov:
718 719 720 721 722 723 724 725 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 718 def self.initialize_custom_driver raise 'User-defined webdriver requires that you provide capabilities' if @capabilities.nil? raise 'User-defined webdriver requires that you provide an endpoint' if @endpoint.nil? Environ.browser = @capabilities[:browserName] Environ.grid = :custom register_remote_driver(Environ.browser, @capabilities) end |
.initialize_downloads ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 220 def self.initialize_downloads @drivers = {} @downloads_path = "#{Dir.pwd}/downloads" # create downloads folder if it doesn't already exist if ENV['DOWNLOADS'] && ENV['DOWNLOADS'].to_bool Dir.mkdir(@downloads_path) unless Dir.exist?(@downloads_path) end if ENV['PARALLEL'] Environ.parallel = true Environ.process_num = ENV['TEST_ENV_NUMBER'] Environ.process_num = '1' if Environ.process_num.blank? if Dir.exist?(@downloads_path) @downloads_path = "#{@downloads_path}/#{Environ.process_num}" Dir.mkdir(@downloads_path) unless Dir.exist?(@downloads_path) end else Environ.parallel = false end @downloads_path = @downloads_path.tr('/', "\\") if OS.windows? @initialized = true end |
.initialize_lambdatest ⇒ Object
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 657 def self.initialize_lambdatest # determine browser type Environ.browser = if @capabilities.nil? ENV['LT_BROWSER'] if ENV['LT_BROWSER'] else @capabilities[:browserName] end browser = Environ.browser Environ.grid = :lambdatest Environ.platform = :desktop Environ.tunneling = ENV['TUNNELING'] if ENV['TUNNELING'] # specify endpoint url @endpoint = "https://#{ENV['LT_USERNAME']}:#{ENV['LT_AUTHKEY']}@hub.lambdatest.com/wd/hub" if @endpoint.nil? # define LambdaTest options = if @capabilities.nil? Environ.os = ENV['LT_OS'] # define the required set of LambdaTest options = { username: ENV['LT_USERNAME'], accessKey: ENV['LT_AUTHKEY'], platformName: ENV['LT_OS'], resolution: ENV['RESOLUTION'], name: , selenium_version: '4.25.0', w3c: true } # define the optional LambdaTest options [:project] = ENV['AUTOMATE_PROJECT'] if ENV['AUTOMATE_PROJECT'] [:build] = ENV['AUTOMATE_BUILD'] if ENV['AUTOMATE_BUILD'] [:headless] = ENV['HEADLESS'] if ENV['HEADLESS'] [:timezone] = ENV['TIME_ZONE'] if ENV['TIME_ZONE'] [:geoLocation] = ENV['GEO_LOCATION'] if ENV['GEO_LOCATION'] [:video] = ENV['RECORD_VIDEO'] if ENV['RECORD_VIDEO'] [:visual] = ENV['SCREENSHOTS'] if ENV['SCREENSHOTS'] [:network] = ENV['NETWORK_LOGS'] if ENV['NETWORK_LOGS'] [:tunnel] = ENV['TUNNELING'] if ENV['TUNNELING'] # define browser specific LambdaTest options case browser.downcase.to_sym when :safari ['safari.popups'] = ENV['ALLOW_POPUPS'] if ENV['ALLOW_POPUPS'] ['safari.cookies'] = ENV['ALLOW_COOKIES'] if ENV['ALLOW_COOKIES'] when :ie ['ie.popups'] = ENV['ALLOW_POPUPS'] if ENV['ALLOW_POPUPS'] when :microsoftedge ['edge.popups'] = ENV['ALLOW_POPUPS'] if ENV['ALLOW_POPUPS'] end { browserName: browser, browserVersion: ENV['LT_VERSION'], 'LT:Options': } else Environ.os = @capabilities[:platform_name] @capabilities end register_remote_driver(browser, ) # configure file_detector for remote uploads config_file_uploads if Environ.platform == :desktop end |
.initialize_local_browser ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 317 def self.initialize_local_browser Environ.os = case when OS.osx? 'OS X' # :nocov: when OS.windows? 'Windows' when OS.linux? 'Linux' else 'unknown' # :nocov: end case Environ.browser when :firefox, :chrome, :safari, :edge, :chrome_headless, :firefox_headless, :edge_headless Environ.platform = :desktop else Environ.platform = :mobile Environ.device_name = Browsers.mobile_device_name(Environ.browser) end Capybara.register_driver driver_name do |app| browser = Environ.browser case browser when :safari Capybara::Selenium::Driver.new(app, browser: browser) when :firefox, :firefox_headless = (browser) Capybara::Selenium::Driver.new(app, browser: :firefox, options: ) when :chrome, :chrome_headless = (browser) Capybara::Selenium::Driver.new(app, browser: :chrome, options: ) when :edge, :edge_headless = (browser) Capybara::Selenium::Driver.new(app, browser: :edge, options: ) else user_agent = Browsers.mobile_device_agent(Environ.browser) = Selenium::WebDriver::Chrome::Options.new(exclude_switches: ['enable-automation']) .add_argument('--disable-dev-shm-usage') .add_argument("--user-agent='#{user_agent}'") .add_argument("--lang=#{ENV['LOCALE']}") if ENV['LOCALE'] Capybara::Selenium::Driver.new(app, browser: :chrome, options: ) end end Capybara.default_driver = Environ.driver_name end |
.initialize_remote ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 364 def self.initialize_remote Environ.grid = :selenium_grid browser = Environ.browser @endpoint = if @endpoint.nil? ENV['REMOTE_ENDPOINT'] if ENV['REMOTE_ENDPOINT'] else 'http://localhost:4444/wd/hub' end case browser # :nocov: when :safari = Selenium::WebDriver::Safari::Options.new # :nocov: when :firefox, :firefox_headless = (browser) when :chrome, :chrome_headless, :edge, :edge_headless = (browser) else Environ.platform = :mobile Environ.device_name = Browsers.mobile_device_name(Environ.browser) user_agent = Browsers.mobile_device_agent(Environ.browser) = Selenium::WebDriver::Chrome::Options.new .add_argument('--disable-dev-shm-usage') .add_argument("--user-agent='#{user_agent}'") .add_argument("--lang=#{ENV['LOCALE']}") if ENV['LOCALE'] end Capybara.register_driver driver_name do |app| Capybara::Selenium::Driver.new(app, browser: :remote, url: @endpoint, options: ).tap do |driver| # configure file_detector for remote uploads driver.browser.file_detector = lambda do |args| str = args.first.to_s str if File.exist?(str) end end end Capybara.current_driver = Environ.driver_name Capybara.default_driver = Environ.driver_name end |
.initialize_saucelabs ⇒ Object
:nocov:
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 584 def self.initialize_saucelabs # determine browser type Environ.browser = if @capabilities.nil? ENV['SL_BROWSER'] if ENV['SL_BROWSER'] else @capabilities[:browserName] end browser = Environ.browser Environ.grid = :saucelabs if ENV['SL_PLATFORM'] Environ.device_name = ENV['SL_DEVICE'] Environ.platform = :mobile Environ.device = :simulator elsif ENV['SL_OS'] Environ.platform = :desktop Environ.os = ENV['SL_OS'] end # specify endpoint url if @endpoint.nil? @endpoint = "https://#{ENV['SL_USERNAME']}:#{ENV['SL_AUTHKEY']}@ondemand.#{ENV['SL_DATA_CENTER']}.saucelabs.com:443/wd/hub" end # define SauceLab options = if @capabilities.nil? # define the required set of SauceLab options = { username: ENV['SL_USERNAME'], access_key: ENV['SL_AUTHKEY'], build: } # define the optional SauceLab options [:name] = ENV['AUTOMATE_PROJECT'] if ENV['AUTOMATE_PROJECT'] [:recordVideo] = ENV['RECORD_VIDEO'] if ENV['RECORD_VIDEO'] [:recordScreenshots] = ENV['SCREENSHOTS'] if ENV['SCREENSHOTS'] # define mobile device options if ENV['SL_PLATFORM'] [:deviceOrientation] = ENV['ORIENTATION'].upcase if ENV['ORIENTATION'] [:appium_version] = '2.1.3' { browserName: browser, platform_name: ENV['SL_PLATFORM'], 'appium:deviceName': ENV['SL_DEVICE'], 'appium:platformVersion': ENV['SL_VERSION'], 'appium:automationName': ENV['AUTOMATION_ENGINE'], 'sauce:options': } else # define desktop browser options [:screenResolution] = ENV['RESOLUTION'] if ENV['RESOLUTION'] { browserName: browser, browser_version: ENV['SL_VERSION'], platform_name: ENV['SL_OS'], 'sauce:options': } end else = @capabilities[:'sauce:options'] Environ.os = @capabilities[:platform_name] if @capabilities.key?(:'appium:deviceName') Environ.platform = :mobile Environ.device_name = @capabilities[:'appium:deviceName'] Environ.device_os = @capabilities[:'appium:platformVersion'] Environ.device = :simulator Environ.device_orientation = [:deviceOrientation] if .key?(:deviceOrientation) end @capabilities end register_remote_driver(browser, ) # configure file_detector for remote uploads config_file_uploads if Environ.platform == :desktop end |
.initialize_testingbot ⇒ Object
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 517 def self.initialize_testingbot # determine browser type Environ.browser = if @capabilities.nil? ENV['TB_BROWSER'] if ENV['TB_BROWSER'] else @capabilities[:browserName] end browser = Environ.browser Environ.grid = :testingbot if ENV['TB_PLATFORM'] Environ.device_os = ENV['TB_PLATFORM'] Environ.device_name = ENV['TB_DEVICE'] Environ.platform = :mobile Environ.device = :simulator else Environ.platform = :desktop end # specify endpoint url if @endpoint.nil? url = ENV['TUNNELING'] ? '@localhost:4445/wd/hub' : '@hub.testingbot.com/wd/hub' @endpoint = "https://#{ENV['TB_USERNAME']}:#{ENV['TB_AUTHKEY']}#{url}" end # define TestingBot options = if @capabilities.nil? Environ.os = ENV['TB_OS'] # define the required set of TestingBot options = { build: } # define the optional TestingBot options [:name] = ENV['AUTOMATE_PROJECT'] if ENV['AUTOMATE_PROJECT'] [:timeZone] = ENV['TIME_ZONE'] if ENV['TIME_ZONE'] ['testingbot.geoCountryCode'] = ENV['GEO_LOCATION'] if ENV['GEO_LOCATION'] [:screenrecorder] = ENV['RECORD_VIDEO'] if ENV['RECORD_VIDEO'] [:screenshot] = ENV['SCREENSHOTS'] if ENV['SCREENSHOTS'] # define mobile device options if ENV['TB_PLATFORM'] [:platform] = ENV['TB_PLATFORM'] [:orientation] = ENV['ORIENTATION'].upcase if ENV['ORIENTATION'] [:deviceName] = ENV['TB_DEVICE'] if ENV['TB_DEVICE'] else # define desktop browser options ['screen-resolution'] = ENV['RESOLUTION'] if ENV['RESOLUTION'] ['selenium-version'] = '4.35.0' end { browserName: browser, browserVersion: ENV['TB_VERSION'], platformName: ENV['TB_OS'], 'tb:options': } else = @capabilities[:'tb:options'] Environ.os = @capabilities[:platformName] if .key?(:deviceName) Environ.platform = :mobile Environ.device_name = [:deviceName] Environ.device_os = @capabilities[:browserVersion] Environ.device_orientation = [:orientation] if .key?(:orientation) Environ.device = :simulator end @capabilities end register_remote_driver(browser, ) # configure file_detector for remote uploads if target is desktop web browser config_file_uploads if Environ.platform == :desktop 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( = nil) @initialized ||= false initialize_downloads unless @initialized @endpoint = nil @capabilities = nil Environ.driver_name = nil Environ.device_orientation = nil if .is_a?(String) Capybara.app_host = elsif .is_a?(Hash) Capybara.app_host = [:app_host] if .key?(:app_host) @endpoint = [:endpoint] if .key?(:endpoint) @capabilities = [:capabilities] if .key?(:capabilities) Environ.driver = [:driver] if .key?(:driver) Environ.driver_name = [:driver_name] if .key?(:driver_name) Environ.device_type = [:device_type] if .key?(:device_type) Environ.browser_size = if .key?(:browser_size) [: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 .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_drivers ⇒ Integer
Return the number of driver instances
138 139 140 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 138 def self.num_drivers @drivers.nil? ? 0 : @drivers.length end |
.register_remote_driver(browser, options) ⇒ Object
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 809 def self.register_remote_driver(browser, ) Capybara.register_driver driver_name do |app| browser = browser.gsub(/\s+/, '_').downcase.to_sym if browser.is_a?(String) capabilities = Selenium::WebDriver::Remote::Capabilities.new() Capybara::Selenium::Driver.new(app, browser: :remote, url: @endpoint, capabilities: capabilities) end Environ.browser = browser Capybara.default_driver = Environ.driver_name Capybara.current_driver = Environ.driver_name Capybara.run_server = false end |
.set_domain(url) ⇒ Object
:nocov:
213 214 215 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 213 def self.set_domain(url) Capybara.app_host = url end |
.test_context_message ⇒ Object
780 781 782 783 784 785 786 787 788 789 790 791 792 |
# File 'lib/testcentricity_web/web_core/webdriver_helper.rb', line 780 def self. = if ENV['TEST_CONTEXT'] "#{Environ.test_environment.to_s.upcase} - #{ENV['TEST_CONTEXT']}" else Environ.test_environment.to_s.upcase end if Environ.parallel thread_num = ENV['TEST_ENV_NUMBER'] thread_num = 1 if thread_num.blank? = "#{} - Thread ##{thread_num}" end end |