123456789_123456789_123456789_123456789_123456789_

Module: Selenium::WebDriver::DriverExtensions::HasAuthentication Private

Instance Method Summary

Instance Method Details

#auth_handlers (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver_extensions/has_authentication.rb', line 57

def auth_handlers
  @auth_handlers ||= []
end

#authenticate(request_id, url) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver_extensions/has_authentication.rb', line 61

def authenticate(request_id, url)
  credentials = auth_handlers.find do |handler|
    url.match?(handler[:uri])
  end

  if credentials
    devtools.fetch.continue_with_auth(
      request_id: request_id,
      auth_challenge_response: {
        response: 'ProvideCredentials',
        username: credentials[:username],
        password: credentials[:password]
      }
    )
  else
    devtools.fetch.continue_with_auth(
      request_id: request_id,
      auth_challenge_response: {
        response: 'CancelAuth'
      }
    )
  end
end

#register(username:, password:, uri: //)

Registers basic authentication handler which is automatically used whenever browser gets an authentication required response. This currently relies on ::Selenium::WebDriver::DevTools so is only supported in ::Selenium::WebDriver::Chromium browsers.

Examples:

Authenticate any request

driver.register(username: 'admin', password: '123456')

Authenticate based on URL

driver.register(username: 'admin1', password: '123456', uri: /mysite1\.com/)
driver.register(username: 'admin2', password: '123456', uri: /mysite2\.com/)

Parameters:

  • username (String)
  • password (String)
  • uri (Regexp)

    to associate the credentials with

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver_extensions/has_authentication.rb', line 42

def register(username:, password:, uri: //)
  auth_handlers << {username: username, password: password, uri: uri}

  devtools.network.set_cache_disabled(cache_disabled: true)
  devtools.fetch.on(:auth_required) do |params|
    authenticate(params['requestId'], params.dig('request', 'url'))
  end
  devtools.fetch.on(:request_paused) do |params|
    devtools.fetch.continue_request(request_id: params['requestId'])
  end
  devtools.fetch.enable(handle_auth_requests: true)
end