Skip to main content

Network

page.network

traffic Array<Network::Exchange>

Returns all information about network traffic as Network::Exchange instance which in general is a wrapper around request, response and error.

page.go_to("https://github.com/")
page.network.traffic # => [#<Ferrum::Network::Exchange, ...]

request : Network::Request

Page request of the main frame.

page.go_to("https://github.com/")
page.network.request # => #<Ferrum::Network::Request...

response : Network::Response

Page response of the main frame.

page.go_to("https://github.com/")
page.network.response # => #<Ferrum::Network::Response...

status : Integer

Contains the status code of the main page response (e.g., 200 for a success). This is just a shortcut for response.status.

page.go_to("https://github.com/")
page.network.status # => 200

wait_for_idle(**options) : Boolean

Waits for network idle, returns true in case of success and false if there are still connections.

  • options Hash
    • :connections Integer how many connections are allowed for network to be idling, 0 by default
    • :duration Float sleep for given amount of time and check again, 0.05 by default
    • :timeout Float during what time we try to check idle, browser.timeout by default
page.go_to("https://example.com/")
page.at_xpath("//a[text() = 'No UI changes button']").click
page.network.wait_for_idle # => true

wait_for_idle!(**options)

Waits for network idle or raises Ferrum::TimeoutError error. Accepts same arguments as wait_for_idle.

page.go_to("https://example.com/")
page.at_xpath("//a[text() = 'No UI changes button']").click
page.network.wait_for_idle! # might raise an error

clear(type)

Clear page's cache or collected traffic.

  • type Symbol it is either :traffic or :cache
traffic = page.network.traffic # => []
page.go_to("https://github.com/")
traffic.size # => 51
page.network.clear(:traffic)
traffic.size # => 0

on(name, &block) : Integer

Subscribes to a CDP event, or to one of the following pseudo-events. Returns a subscription id you can pass to off to unsubscribe.

  • :request an intercepted request, see intercept below. Requires network.intercept to be set up first.
  • :response a request's response once it's fully loaded, see below. Works without intercept.
  • :auth a proxy/basic auth challenge, see authorize below. Requires network.intercept to be set up first.
  • :dialog a JS dialog (alert/confirm/prompt), see Dialogs.
  • anything else is passed straight through as a raw CDP event name, e.g. page.on("Page.frameNavigated") { |params| ... }.
on(:response, &block)

Subscribes to a callback fired once a request's response has fully loaded, i.e. after Chrome's Network.loadingFinished event, so exchange.response.body is always available. Doesn't require intercept to be set up. The block receives the Network::Exchange for that request; it's never yielded for requests that fail to load (use traffic to inspect those instead).

page.on(:response) do |exchange|
puts "#{exchange.response.status} #{exchange.url}"
end
page.go_to("https://github.com/")

off(name, id) : void

Unsubscribes a listener previously registered via on, given the subscription id on returned.

  • name Symbol | String the same event name that was passed to on, e.g. :request, :response, :auth
  • id Integer the subscription id returned by on
id = page.on(:response) do |exchange|
puts "#{exchange.response.status} #{exchange.url}"
end
page.go_to("https://github.com/")
page.off(:response, id)

intercept(**options)

Set request interception for given options. This method is only sets request interception, you should use on callback to catch requests and abort or continue them.

  • options Hash
    • :pattern String * by default
    • :resource_type Symbol one of the resource types
browser = Ferrum::Browser.new
page = browser.create_page
page.network.intercept
page.on(:request) do |request|
if request.match?(/bla-bla/)
request.abort
elsif request.match?(/lorem/)
request.respond(body: "Lorem ipsum")
else
request.continue
end
end
page.go_to("https://google.com")

authorize(**options, &block)

If site or proxy uses authorization you can provide credentials using this method.

  • options Hash
    • :type Symbol :server | :proxy site or proxy authorization
    • :user String
    • :password String
  • &block accepts authenticated request, which you must subsequently allow or deny, if you don't care about unwanted requests just call request.continue.
page.network.authorize(user: "login", password: "pass") { |req| req.continue }
page.go_to("http://example.com/authenticated")
puts page.network.status # => 200
puts page.body # => Welcome, authenticated client

Since Chrome implements authorize using request interception you must continue or abort authorized requests. If you already have code that uses interception you can use authorize without block, but if not you are obliged to pass block, so this is version doesn't pass block and can work just fine:

browser = Ferrum::Browser.new
page = browser.create_page
page.network.intercept
page.on(:request) do |request|
if request.resource_type == "Image"
request.abort
else
request.continue
end
end

page.network.authorize(user: "login", password: "pass", type: :proxy)

page.go_to("https://google.com")

You used to call authorize method without block, but since it's implemented using request interception there could be a collision with another part of your code that also uses request interception, so that authorize allows the request while your code denies but it's too late. The block is mandatory now.

emulate_network_conditions(**options)

Activates emulation of network conditions.

  • options Hash
    • :offline Boolean emulate internet disconnection, false by default
    • :latency Integer minimum latency from request sent to response headers received (ms), 0 by default
    • :download_throughput Integer maximal aggregated download throughput (bytes/sec), -1 by default, disables download throttling
    • :upload_throughput Integer maximal aggregated upload throughput (bytes/sec), -1 by default, disables download throttling
    • :connection_type String connection type if known, one of: none, cellular2g, cellular3g, cellular4g, bluetooth, ethernet, wifi, wimax, other. nil by default
page.network.emulate_network_conditions(connection_type: "cellular2g")
page.go_to("https://github.com/")

offline_mode

Activates offline mode for a page.

page.network.offline_mode
page.go_to("https://github.com/") # => Ferrum::StatusError (Request to https://github.com/ failed(net::ERR_INTERNET_DISCONNECTED))

cache(disable: Boolean)

Toggles ignoring cache for each request. If true, cache will not be used.

page.network.cache(disable: true)