Class: Ferrum::Network
- Inherits:
-
Object
- Object
- Ferrum::Network
- Defined in:
- lib/ferrum/network.rb,
lib/ferrum/network/error.rb,
lib/ferrum/network/request.rb,
lib/ferrum/network/exchange.rb,
lib/ferrum/network/response.rb,
lib/ferrum/network/auth_request.rb,
lib/ferrum/network/request_params.rb,
lib/ferrum/network/intercepted_request.rb
Defined Under Namespace
Modules: RequestParams Classes: AuthRequest, Error, Exchange, InterceptedRequest, Request, Response
Constant Summary collapse
- CLEAR_TYPE =
%i[traffic cache].freeze
- AUTHORIZE_TYPE =
%i[server proxy].freeze
- REQUEST_STAGES =
%i[Request Response].freeze
- RESOURCE_TYPES =
%i[Document Stylesheet Image Media Font Script TextTrack XHR Fetch Prefetch EventSource WebSocket Manifest SignedExchange Ping CSPViolationReport Preflight Other].freeze
- AUTHORIZE_BLOCK_MISSING =
"Block is missing, call `authorize(...) { |r| r.continue } " \ "or subscribe to `on(:request)` events before calling it"
- AUTHORIZE_TYPE_WRONG =
":type should be in #{AUTHORIZE_TYPE}"
- ALLOWED_CONNECTION_TYPE =
%w[none cellular2g cellular3g cellular4g bluetooth ethernet wifi wimax other].freeze
Instance Attribute Summary collapse
-
#traffic ⇒ Array<Exchange>
readonly
Network traffic.
Instance Method Summary collapse
-
#authorize(user:, password:, type: :server) {|request| ... } ⇒ Object
Sets HTTP Basic-Auth credentials.
- #authorized_response(ids, request_id, username, password) ⇒ Object
- #blacklist=(patterns) ⇒ Object (also: #blocklist=)
- #build_exchange(id) ⇒ Object
-
#cache(disable:) ⇒ Object
Toggles ignoring cache for each request.
-
#clear(type) ⇒ true
Clear browser’s cache or collected traffic.
-
#emulate_network_conditions(offline: false, latency: 0, download_throughput: -1,, upload_throughput: -1,, connection_type: nil) ⇒ Object
Activates emulation of network conditions.
- #finished_connections ⇒ Object
- #idle?(connections = 0) ⇒ Boolean
-
#initialize(page) ⇒ Network
constructor
A new instance of Network.
-
#intercept(pattern: "*", resource_type: nil, request_stage: nil, handle_auth_requests: true) ⇒ Object
Set request interception for given options.
-
#offline_mode ⇒ Object
Activates offline mode for a page.
- #pending_connections ⇒ Object
-
#request ⇒ Request
Page request of the main frame.
-
#response ⇒ Response?
Page response of the main frame.
- #select(request_id) ⇒ Object
-
#status ⇒ Integer?
Contains the status code of the main page response (e.g., 200 for a success).
- #subscribe ⇒ Object
- #total_connections ⇒ Object
-
#wait_for_idle(connections: 0, duration: 0.05, timeout: @page.timeout) ⇒ Object
Waits for network idle or raises TimeoutError error.
- #whitelist=(patterns) ⇒ Object (also: #allowlist=)
Constructor Details
#initialize(page) ⇒ Network
Returns a new instance of Network.
35 36 37 38 39 40 41 |
# File 'lib/ferrum/network.rb', line 35 def initialize(page) @page = page @traffic = [] @exchange = nil @blacklist = nil @whitelist = nil end |
Instance Attribute Details
#traffic ⇒ Array<Exchange> (readonly)
Network traffic.
33 34 35 |
# File 'lib/ferrum/network.rb', line 33 def traffic @traffic end |
Instance Method Details
#authorize(user:, password:, type: :server) {|request| ... } ⇒ Object
Sets HTTP Basic-Auth credentials.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/ferrum/network.rb', line 232 def (user:, password:, type: :server, &block) raise ArgumentError, AUTHORIZE_TYPE_WRONG unless AUTHORIZE_TYPE.include?(type) raise ArgumentError, AUTHORIZE_BLOCK_MISSING if !block_given? && !@page.subscribed?("Fetch.requestPaused") @authorized_ids ||= {} @authorized_ids[type] ||= [] intercept @page.on(:request, &block) @page.on(:auth) do |request, index, total| if request.auth_challenge?(type) response = (@authorized_ids[type], request.request_id, user, password) @authorized_ids[type] << request.request_id request.continue(authChallengeResponse: response) elsif index + 1 < total next # There are other callbacks that can handle this else request.abort end end end |
#authorized_response(ids, request_id, username, password) ⇒ Object
267 268 269 270 271 272 273 274 275 |
# File 'lib/ferrum/network.rb', line 267 def (ids, request_id, username, password) if ids.include?(request_id) { response: "CancelAuth" } elsif username && password { response: "ProvideCredentials", username: username, password: password } end end |
#blacklist=(patterns) ⇒ Object Also known as: blocklist=
155 156 157 158 |
# File 'lib/ferrum/network.rb', line 155 def blacklist=(patterns) @blacklist = Array(patterns) blacklist_subscribe end |
#build_exchange(id) ⇒ Object
281 282 283 |
# File 'lib/ferrum/network.rb', line 281 def build_exchange(id) Network::Exchange.new(@page, id).tap { |e| @traffic << e } end |
#cache(disable:) ⇒ Object
Toggles ignoring cache for each request. If true, cache will not be used.
349 350 351 |
# File 'lib/ferrum/network.rb', line 349 def cache(disable:) @page.command("Network.setCacheDisabled", cacheDisabled: disable) end |
#clear(type) ⇒ true
Clear browser’s cache or collected traffic.
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/ferrum/network.rb', line 143 def clear(type) raise ArgumentError, ":type should be in #{CLEAR_TYPE}" unless CLEAR_TYPE.include?(type) if type == :traffic @traffic.clear else @page.command("Network.clearBrowserCache") end true end |
#emulate_network_conditions(offline: false, latency: 0, download_throughput: -1,, upload_throughput: -1,, connection_type: nil) ⇒ Object
Activates emulation of network conditions.
316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/ferrum/network.rb', line 316 def emulate_network_conditions(offline: false, latency: 0, download_throughput: -1, upload_throughput: -1, connection_type: nil) params = { offline: offline, latency: latency, downloadThroughput: download_throughput, uploadThroughput: upload_throughput } params[:connectionType] = connection_type if connection_type && ALLOWED_CONNECTION_TYPE.include?(connection_type) @page.command("Network.emulateNetworkConditions", **params) true end |
#finished_connections ⇒ Object
80 81 82 |
# File 'lib/ferrum/network.rb', line 80 def finished_connections @traffic.count(&:finished?) end |
#idle?(connections = 0) ⇒ Boolean
72 73 74 |
# File 'lib/ferrum/network.rb', line 72 def idle?(connections = 0) pending_connections <= connections end |
#intercept(pattern: "*", resource_type: nil, request_stage: nil, handle_auth_requests: true) ⇒ Object
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.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/ferrum/network.rb', line 191 def intercept(pattern: "*", resource_type: nil, request_stage: nil, handle_auth_requests: true) pattern = { urlPattern: pattern } if resource_type && RESOURCE_TYPES.none?(resource_type.to_sym) raise ArgumentError, "Unknown resource type '#{resource_type}' must be #{RESOURCE_TYPES.join(' | ')}" end if request_stage && REQUEST_STAGES.none?(request_stage.to_sym) raise ArgumentError, "Unknown request stage '#{request_stage}' must be #{REQUEST_STAGES.join(' | ')}" end pattern[:resourceType] = resource_type if resource_type pattern[:requestStage] = request_stage if request_stage @page.command("Fetch.enable", patterns: [pattern], handleAuthRequests: handle_auth_requests) end |
#offline_mode ⇒ Object
Activates offline mode for a page.
339 340 341 |
# File 'lib/ferrum/network.rb', line 339 def offline_mode emulate_network_conditions(offline: true, latency: 0, download_throughput: 0, upload_throughput: 0) end |
#pending_connections ⇒ Object
84 85 86 |
# File 'lib/ferrum/network.rb', line 84 def pending_connections total_connections - finished_connections end |
#request ⇒ Request
Page request of the main frame.
97 98 99 |
# File 'lib/ferrum/network.rb', line 97 def request @exchange&.request end |
#response ⇒ Response?
Page response of the main frame.
110 111 112 |
# File 'lib/ferrum/network.rb', line 110 def response @exchange&.response end |
#select(request_id) ⇒ Object
277 278 279 |
# File 'lib/ferrum/network.rb', line 277 def select(request_id) @traffic.select { |e| e.id == request_id } end |
#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`.
124 125 126 |
# File 'lib/ferrum/network.rb', line 124 def status response&.status end |
#subscribe ⇒ Object
259 260 261 262 263 264 265 |
# File 'lib/ferrum/network.rb', line 259 def subscribe subscribe_request_will_be_sent subscribe_response_received subscribe_loading_finished subscribe_loading_failed subscribe_log_entry_added end |
#total_connections ⇒ Object
76 77 78 |
# File 'lib/ferrum/network.rb', line 76 def total_connections @traffic.size end |
#wait_for_idle(connections: 0, duration: 0.05, timeout: @page.timeout) ⇒ Object
Waits for network idle or raises TimeoutError error.
62 63 64 65 66 67 68 69 70 |
# File 'lib/ferrum/network.rb', line 62 def wait_for_idle(connections: 0, duration: 0.05, timeout: @page.timeout) start = Utils::ElapsedTime.monotonic_time until idle?(connections) raise TimeoutError if Utils::ElapsedTime.timeout?(start, timeout) sleep(duration) end end |
#whitelist=(patterns) ⇒ Object Also known as: allowlist=
161 162 163 164 |
# File 'lib/ferrum/network.rb', line 161 def whitelist=(patterns) @whitelist = Array(patterns) whitelist_subscribe end |