Class: Capybara::Lightpanda::Network
- Inherits:
-
Object
- Object
- Capybara::Lightpanda::Network
- Defined in:
- lib/capybara/lightpanda/network.rb
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
Instance Method Summary collapse
- #add_headers(headers) ⇒ Object
- #clear ⇒ Object
- #clear_headers ⇒ Object
- #disable ⇒ Object
- #enable ⇒ Object
-
#headers=(headers) ⇒ Object
Setting extra headers also lazily enables the Network domain.
-
#idle?(connections = 0) ⇒ Boolean
True when no more than ‘connections` requests are in-flight.
-
#initialize(browser) ⇒ Network
constructor
A new instance of Network.
-
#pending_connections ⇒ Object
Count of in-flight requests (those with no response yet recorded).
-
#reset ⇒ Object
Wipe local state without sending Network.disable.
- #traffic ⇒ Object
- #wait_for_idle(timeout: 5, connections: 0) ⇒ Object
-
#wait_for_idle!(timeout: 5, connections: 0) ⇒ Object
Raising variant of #wait_for_idle (ferrum parity).
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
#browser ⇒ Object (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 |
#clear ⇒ Object
42 43 44 |
# File 'lib/capybara/lightpanda/network.rb', line 42 def clear @traffic_mutex.synchronize { @traffic.clear } end |
#clear_headers ⇒ Object
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 |
#disable ⇒ Object
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 |
#enable ⇒ Object
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.
86 87 88 |
# File 'lib/capybara/lightpanda/network.rb', line 86 def idle?(connections = 0) pending_connections <= connections end |
#pending_connections ⇒ Object
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 |
#reset ⇒ Object
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 |
#traffic ⇒ Object
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 |