self.configure!

in qa/qa/runtime/browser.rb [27:219]


      def self.configure! 
        return if QA::Runtime::Env.dry_run
        return if @configured

        RSpec.configure do |config|
          config.define_derived_metadata(file_path: %r{/qa/specs/features/}) do |metadata|
            metadata[:type] = :feature
          end

          config.append_after(:each) do |example|
            if example.metadata[:screenshot]
              screenshot = example.metadata[:screenshot][:image] || example.metadata[:screenshot][:html]
              example.metadata[:stdout] = %([[ATTACHMENT|
            end
          end
        end

        Capybara.server_port = 9887 + ENV['TEST_ENV_NUMBER'].to_i
        Capybara.register_driver QA::Runtime::Env.browser do |app|
          webdriver_options = Selenium::WebDriver::Options.send(QA::Runtime::Env.browser)

          case QA::Runtime::Env.browser
          when :chrome
            
            chrome_options = { args: %w[no-sandbox] }

            
            chrome_options[:args] << 'headless=new' if QA::Runtime::Env.webdriver_headless?

            
            chrome_options[:args] << 'disable-dev-shm-usage' if QA::Runtime::Env.disable_dev_shm?

            chrome_options[:args] << 'disable-search-engine-choice-screen'

            
            chrome_options[:prefs] = {} if chrome_options[:prefs].nil?

            
            chrome_options[:prefs]['profile.content_settings.exceptions.clipboard'] = {
              '*': { setting: 1 }
            }

            
            Runtime::Scenario.attributes[:gitlab_address].tap do |address|
              next unless address.start_with?('http://')

              chrome_options[:args] << "unsafely-treat-insecure-origin-as-secure=#{address}"
            end

            
            
            
            unless QA::Runtime::Env.remote_grid
              chrome_options[:prefs] = {} unless chrome_options[:prefs]

              chrome_options[:prefs].merge!({
                'download.default_directory' => File.expand_path(QA::Runtime::Env.chrome_default_download_path),
                'download.prompt_for_download' => false
              })
            end

            
            
            unless QA::Runtime::Env.user_agent.blank?
              chrome_options[:args] << "user-agent=#{QA::Runtime::Env.user_agent}"
            end

            if QA::Runtime::Env.remote_mobile_device_name
              webdriver_options.platform_name = 'Android'
              webdriver_options.add_option('appium:automationName', 'UiAutomator2')
              webdriver_options.add_option('appium:deviceName', QA::Runtime::Env.remote_mobile_device_name)
              webdriver_options.add_option('appium:platformVersion', 'latest')
            else
              chrome_options[:args] << "window-size=#{DEFAULT_WINDOW_SIZE}"
            end

            
            
            
            if QA::Runtime::Env.slack_workspace
              slack_default_preference = {
                'protocol_handler' => {
                  'allowed_origin_protocol_pairs' => {
                    "https://#{QA::Runtime::Env.slack_workspace}.slack.com" => {
                      'slack' => true
                    }
                  }
                }
              }

              default_profile = File.join("#{chrome_profile_location}/Default")
              FileUtils.mkdir_p(default_profile)
              preferences = slack_default_preference

              
              
              if File.exist?("#{default_profile}/Preferences")
                begin
                  preferences = JSON.parse(File.read("#{default_profile}/Preferences"))
                  preferences.deep_merge!(slack_default_preference)
                rescue JSON::ParserError => _
                end
              end

              File.write("#{default_profile}/Preferences", preferences.to_json)
              append_chrome_profile_to_capabilities(chrome_options)
            end

            
            
            append_chrome_profile_to_capabilities(chrome_options) if QA::Runtime::Env.reuse_chrome_profile?

            webdriver_options.args = chrome_options[:args]
            webdriver_options.prefs = chrome_options[:prefs]
            webdriver_options.accept_insecure_certs = true if QA::Runtime::Env.accept_insecure_certs?
            
            
            webdriver_options.logging_prefs = {
              browser: 'ALL',
              client: 'ALL',
              driver: 'ALL',
              server: 'ALL'
            }

          when :safari
            if QA::Runtime::Env.remote_mobile_device_name
              webdriver_options.platform_name = 'iOS'
              webdriver_options.add_option('appium:automationName', 'XCUITest')
              webdriver_options.add_option('appium:deviceName', QA::Runtime::Env.remote_mobile_device_name)
              webdriver_options.add_option('appium:platformVersion', 'latest')
            end
          when :firefox
            webdriver_options.accept_insecure_certs = true if QA::Runtime::Env.accept_insecure_certs?
            webdriver_options.args << "--headless" if QA::Runtime::Env.webdriver_headless?
          when :edge
            webdriver_options.args << "--window-size=#{DEFAULT_WINDOW_SIZE}"
            webdriver_options.args << "headless" if QA::Runtime::Env.webdriver_headless?
          end

          selenium_options = {
            browser: QA::Runtime::Env.browser,
            clear_local_storage: true
          }

          if QA::Runtime::Env.remote_grid
            selenium_options[:browser] = :remote
            selenium_options[:url] = QA::Runtime::Env.remote_grid
            webdriver_options.browser_version = QA::Runtime::Env.selenoid_browser_version
          end

          if QA::Runtime::Env.remote_tunnel_id
            webdriver_options.add_option('sauce:options', {
              tunnelIdentifier: QA::Runtime::Env.remote_tunnel_id
            })
          end

          if QA::Runtime::Env.record_video?
            webdriver_options.add_option('selenoid:options', {
              enableVideo: true
            })
          end

          Capybara::Selenium::Driver.new(app, options: webdriver_options, **selenium_options)
        end

        
        Capybara::Screenshot.prune_strategy = :keep_last_run

        
        Capybara::Screenshot.register_driver(QA::Runtime::Env.browser) do |driver, path|
          QA::Runtime::Logger.info("Saving screenshot..")
          driver.browser.save_screenshot(path)
        end

        Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
          ::File.join("failure_screenshots", example.full_description.downcase.parameterize(separator: "_")[0..79])
        end

        Capybara.configure do |config|
          config.default_driver = QA::Runtime::Env.browser
          config.javascript_driver = QA::Runtime::Env.browser
          config.default_max_wait_time = CAPYBARA_MAX_WAIT_TIME
          
          config.save_path = ::File.expand_path('../../tmp', __dir__)

          
          
          config.default_normalize_ws = true
        end

        @configured = true
      end