Class: Capybara::Lightpanda::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/lightpanda/network.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser) ⇒ Network

Returns a new instance of Network.



8
9
10
11
12
13
14
15
# File 'lib/capybara/lightpanda/network.rb', line 8

def initialize(browser)
  @browser = browser
  @traffic = []
  @traffic_mutex = Mutex.new
  @enabled = false
  @request_handler = nil
  @response_handler = nil
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



6
7
8
# File 'lib/capybara/lightpanda/network.rb', line 6

def browser
  @browser
end

Instance Method Details

#add_headers(headers) ⇒ Object



67
68
69
70
71
# File 'lib/capybara/lightpanda/network.rb', line 67

def add_headers(headers)
  enable
  @extra_headers = (@extra_headers || {}).merge(headers)
  browser.page_command("Network.setExtraHTTPHeaders", headers: @extra_headers)
end

#clearObject



42
43
44
# File 'lib/capybara/lightpanda/network.rb', line 42

def clear
  @traffic_mutex.synchronize { @traffic.clear }
end

#clear_headersObject



73
74
75
76
77
# File 'lib/capybara/lightpanda/network.rb', line 73

def clear_headers
  enable
  @extra_headers = {}
  browser.page_command("Network.setExtraHTTPHeaders", headers: {})
end

#disableObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/capybara/lightpanda/network.rb', line 25

def disable
  return unless @enabled

  # Tell the browser to stop emitting BEFORE unsubscribing locally:
  # otherwise an in-flight Network.responseReceived can race past the
  # already-removed handler and leave a `response: nil` entry in
  # @traffic for the matching request — which then trips
  # wait_for_idle's pending count on a future call.
  browser.command("Network.disable")
  unsubscribe
  @enabled = false
end

#enableObject



17
18
19
20
21
22
23
# File 'lib/capybara/lightpanda/network.rb', line 17

def enable
  return if @enabled

  browser.command("Network.enable")
  subscribe
  @enabled = true
end

#headers=(headers) ⇒ Object

Setting extra headers also lazily enables the Network domain. Without this, headers were silently ignored until the caller separately ran ‘network.enable` (or `wait_for_network_idle`). Cuprite/Ferrum parity.



61
62
63
64
65
# File 'lib/capybara/lightpanda/network.rb', line 61

def headers=(headers)
  enable
  @extra_headers = headers
  browser.page_command("Network.setExtraHTTPHeaders", headers: headers)
end

#idle?(connections = 0) ⇒ Boolean

True when no more than ‘connections` requests are in-flight.

Returns:

  • (Boolean)


86
87
88
# File 'lib/capybara/lightpanda/network.rb', line 86

def idle?(connections = 0)
  pending_connections <= connections
end

#pending_connectionsObject

Count of in-flight requests (those with no response yet recorded). Cheap predicate-friendly accessor (ferrum parity).



81
82
83
# File 'lib/capybara/lightpanda/network.rb', line 81

def pending_connections
  @traffic_mutex.synchronize { @traffic.count { |t| t[:response].nil? } }
end

#resetObject

Wipe local state without sending Network.disable. Called by Browser#reset after Target.disposeBrowserContext, which destroys the subscriptions and the Network domain along with the context —leaving @enabled true would silently no-op the next #enable. Also unsubscribes locally so we don’t rely on the caller having cleared the Subscriber first.



52
53
54
55
56
# File 'lib/capybara/lightpanda/network.rb', line 52

def reset
  unsubscribe
  @traffic_mutex.synchronize { @traffic.clear }
  @enabled = false
end

#trafficObject



38
39
40
# File 'lib/capybara/lightpanda/network.rb', line 38

def traffic
  @traffic_mutex.synchronize { @traffic.dup }
end

#wait_for_idle(timeout: 5, connections: 0) ⇒ Object



90
91
92
93
94
# File 'lib/capybara/lightpanda/network.rb', line 90

def wait_for_idle(timeout: 5, connections: 0)
  wait_for_idle!(timeout: timeout, connections: connections)
rescue TimeoutError
  false
end

#wait_for_idle!(timeout: 5, connections: 0) ⇒ Object

Raising variant of #wait_for_idle (ferrum parity). Returns true on success, raises TimeoutError on timeout so callers that treat the idle wait as a precondition don’t have to remember to check a bool.



99
100
101
102
103
104
105
106
107
# File 'lib/capybara/lightpanda/network.rb', line 99

def wait_for_idle!(timeout: 5, connections: 0) # rubocop:disable Naming/PredicateMethod
  Utils::Wait.until(
    timeout: timeout,
    interval: 0.1,
    message: "Network did not become idle within #{timeout}s " \
             "(pending=#{pending_connections}, allowed=#{connections})"
  ) { idle?(connections) }
  true
end