123456789_123456789_123456789_123456789_123456789_

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

Instance Method Summary

Instance Method Details

#intercept {|request, continue| ... }

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

[ GitHub ]

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

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