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
# File 'lib/capybara/lightpanda/network.rb', line 8

def initialize(browser)
  @browser = browser
  @traffic = []
  @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



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

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

#clearObject



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

def clear
  @traffic.clear
end

#clear_headersObject



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

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

#disableObject



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

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



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

def enable
  return if @enabled

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

#headers=(headers) ⇒ Object



57
58
59
60
# File 'lib/capybara/lightpanda/network.rb', line 57

def headers=(headers)
  @extra_headers = headers
  browser.page_command("Network.setExtraHTTPHeaders", headers: headers)
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.



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

def reset
  unsubscribe
  @traffic.clear
  @enabled = false
end

#trafficObject



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

def traffic
  @traffic.dup
end

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

rubocop:disable Naming/PredicateMethod



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/capybara/lightpanda/network.rb', line 72

def wait_for_idle(timeout: 5, connections: 0) # rubocop:disable Naming/PredicateMethod
  started_at = Time.now

  while Time.now - started_at < timeout
    pending = @traffic.count { |t| t[:response].nil? }
    return true if pending <= connections

    sleep 0.1
  end

  false
end