Module: Selenium::WebDriver::DriverExtensions::HasNetworkInterception Private

Defined in:
lib/selenium/webdriver/common/driver_extensions/has_network_interception.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#intercept(&block) {|request, continue| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Intercepts requests coming from browser allowing to either pass them through like proxy or provide a stubbed response instead.

Examples:

Log requests and pass through

driver.intercept do |request, &continue|
  puts "#{request.method} #{request.url}"
  continue.call(request)
end

Stub requests for images

driver.intercept do |request, &continue|
  if request.url.match?(/\.png$/)
    request.url = 'https://upload.wikimedia.org/wikipedia/commons/d/d5/Selenium_Logo.png'
  end
  continue.call(request)
end

Log responses and pass through

driver.intercept do |request, &continue|
  continue.call(request) do |response|
    puts "#{response.code} #{response.body}"
  end
end

Mutate specific response

driver.intercept do |request, &continue|
  continue.call(request) do |response|
    response.body << 'Added by Selenium!' if request.url.include?('/myurl')
  end
end

Parameters:

  • block (Proc)

    which is called when request is intercepted

Yield Parameters:

  • request (DevTools::Request)
  • continue (Proc)

    block which proceeds with the request and optionally yields response



62
63
64
65
# File 'lib/selenium/webdriver/common/driver_extensions/has_network_interception.rb', line 62

def intercept(&block)
  @interceptor ||= DevTools::NetworkInterceptor.new(devtools)
  @interceptor.intercept(&block)
end