Module: Capybara::ChromeResponseHeaders::DriverExtensions

Defined in:
lib/capybara/chrome_response_headers/driver_extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#listener_threadObject (readonly)

Returns the value of attribute listener_thread.



4
5
6
# File 'lib/capybara/chrome_response_headers/driver_extensions.rb', line 4

def listener_thread
  @listener_thread
end

#listening_to_network_trafficObject (readonly)

Returns the value of attribute listening_to_network_traffic.



3
4
5
# File 'lib/capybara/chrome_response_headers/driver_extensions.rb', line 3

def listening_to_network_traffic
  @listening_to_network_traffic
end

#request_for_urlObject (readonly)

Returns the value of attribute request_for_url.



6
7
8
# File 'lib/capybara/chrome_response_headers/driver_extensions.rb', line 6

def request_for_url
  @request_for_url
end

#response_for_urlObject (readonly)

Returns the value of attribute response_for_url.



7
8
9
# File 'lib/capybara/chrome_response_headers/driver_extensions.rb', line 7

def response_for_url
  @response_for_url
end

Instance Method Details

#browserObject



15
16
17
18
19
20
21
# File 'lib/capybara/chrome_response_headers/driver_extensions.rb', line 15

def browser
  super.tap do |browser|
    if Capybara::ChromeDevTools.enabled and !@listening_to_network_traffic
      listen_to_network_traffic
    end
  end
end

#initializeObject



9
10
11
12
13
# File 'lib/capybara/chrome_response_headers/driver_extensions.rb', line 9

def initialize(*)
  super
  @request_for_url  = {}
  @response_for_url = {}
end

#listen_to_network_trafficObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/capybara/chrome_response_headers/driver_extensions.rb', line 23

def listen_to_network_traffic
  @listening_to_network_traffic = true
  chrome = dev_tools
  chrome.send_cmd "Network.enable"

  chrome.on "Network.requestWillBeSent" do |arg|
    request = OpenStruct.new(arg["request"])
    next if Capybara::ChromeResponseHeaders.ignore_urls && request.url.match(Capybara::ChromeResponseHeaders.ignore_urls)

    @request_for_url[request.url] = request
    puts "Requesting #{request.url}" if Capybara::ChromeResponseHeaders.trace_requests
  end

  chrome.on "Network.responseReceived" do |arg|
    response = OpenStruct.new(arg["response"])
    next if Capybara::ChromeResponseHeaders.ignore_urls && response.url.match(Capybara::ChromeResponseHeaders.ignore_urls)

    # TODO: Use/return a Rack::Response like Rack::Test does
    @response_for_url[response.url] = response
    puts %(Response for #{response.url}: #{response.status} #{response.statusText}) if Capybara::ChromeResponseHeaders.trace_responses
  end

  @listener_thread = Thread.new do
    chrome.listen
    @listening_to_network_traffic = false
  end
end

#requestObject



51
52
53
54
# File 'lib/capybara/chrome_response_headers/driver_extensions.rb', line 51

def request
  #@request_for_url[current_url] or byebug
  @request_for_url[current_url]
end

#responseObject



56
57
58
59
# File 'lib/capybara/chrome_response_headers/driver_extensions.rb', line 56

def response
  #@response_for_url[current_url] or byebug
  @response_for_url[current_url]
end

#response_headersObject



61
62
63
# File 'lib/capybara/chrome_response_headers/driver_extensions.rb', line 61

def response_headers
  response&.headers
end

#status_codeObject



65
66
67
# File 'lib/capybara/chrome_response_headers/driver_extensions.rb', line 65

def status_code
  response&.status
end

#status_textObject



69
70
71
# File 'lib/capybara/chrome_response_headers/driver_extensions.rb', line 69

def status_text
  response&.statusText
end