{"id":20061634,"url":"https://github.com/tylerrick/capybara-chrome_response_headers","last_synced_at":"2025-05-05T16:30:43.641Z","repository":{"id":59151911,"uuid":"162657886","full_name":"TylerRick/capybara-chrome_response_headers","owner":"TylerRick","description":"Allows you to get HTTP status_code, response_headers, etc. when using Capybara::Selenium::Driver just like you can when using Rack::Test","archived":false,"fork":false,"pushed_at":"2018-12-21T18:14:52.000Z","size":9,"stargazers_count":10,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-02T09:12:56.745Z","etag":null,"topics":["capybara","capybara-library","chrome","chrome-devtools-protocol","chrome-remote","crmux","ruby","selenium-webdriver"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TylerRick.png","metadata":{"files":{"readme":"Readme.md","changelog":"Changelog.md","contributing":null,"funding":null,"license":"License","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-21T02:56:51.000Z","updated_at":"2024-09-09T10:40:53.000Z","dependencies_parsed_at":"2022-09-13T11:01:04.155Z","dependency_job_id":null,"html_url":"https://github.com/TylerRick/capybara-chrome_response_headers","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TylerRick%2Fcapybara-chrome_response_headers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TylerRick%2Fcapybara-chrome_response_headers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TylerRick%2Fcapybara-chrome_response_headers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TylerRick%2Fcapybara-chrome_response_headers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TylerRick","download_url":"https://codeload.github.com/TylerRick/capybara-chrome_response_headers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252533696,"owners_count":21763640,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["capybara","capybara-library","chrome","chrome-devtools-protocol","chrome-remote","crmux","ruby","selenium-webdriver"],"created_at":"2024-11-13T13:21:02.019Z","updated_at":"2025-05-05T16:30:43.401Z","avatar_url":"https://github.com/TylerRick.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Capybara::ChromeResponseHeaders\n\nAllows you to get HTTP status_code, response_headers, etc. when using Capybara::Selenium::Driver\njust like you can when using Rack::Test.\n\nThe [Capybara docs]()https://github.com/teamcapybara/capybara) say:\n\n\u003e Some drivers allow access to response headers and HTTP status code, but this kind of functionality is not provided by some drivers, such as Selenium.\n\nBut with this gem, you can have it for your Chrome Selenium driver, too!\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'capybara-chrome_response_headers'\n```\n\n## Usage\n\nInspect `status_code` or `response_headers` in your tests to get the HTTP status code or headers,\nrespectively, for the current URL (the web page that you last visited or were redirected to).\n\nYou can access the `response` information for other assets using `response_for_url`.\n\nExample:\n\n```ruby\n      it do\n        visit '/test/test_redirect?to=/test/basic_page%3Fmy_param=1'\n        expect_uri '/test/basic_page?my_param=1'\n\n        # Since it automatically follows redirects, you can't check if it was a redirect using this\n        expect(status_code).to eq 200\n\n        expect(response_headers[\"Content-Type\"]).to eq \"text/html; charset=utf-8\"\n        expect(response_headers.keys).to include *[\"Content-Type\", \"ETag\", \"Cache-Control\", \"Set-Cookie\", \"X-Meta-Request-Version\", \"X-Request-Id\", \"X-Runtime\", \"X-Frame-Options\", \"X-Content-Type-Options\", \"X-XSS-Protection\", \"Referrer-Policy\", \"Content-Security-Policy\", \"Transfer-Encoding\"]\n\n        # Could do the same for asset paths, like response_for_url[\"#{current_host_with_port}/asset/logo.png\"]\n        expect(response_for_url[\"#{current_host_with_port}/users/sign_in\"].status).to eq 200\n        expect(response_for_url[\"#{current_host_with_port}/users/sign_in\"].headers).to be_a Hash\n      end\n```\n\n## Tracing\n\nTo have it show a trace of all requests and/or responses that are made from the web pages you are interacting with, enable these settings, respectively:\n\n```\nCapybara::ChromeResponseHeaders.trace_requests  = true\nCapybara::ChromeResponseHeaders.trace_responses = true\n```\n\nThe output for responses is something like this:\n```\nResponse for http://127.0.0.1:40359/users/sign_in: 200 OK\nResponse for http://127.0.0.1:40359/admin: 200 OK\n```\n\nBy default, it tries to filter out requests for assets. (`Capybara::ChromeResponseHeaders.ignore_urls` is set to `/\\.(js|css|png|gif|jpg)$/`.)\n\nYou can turn off this filtering by setting `Capybara::ChromeResponseHeaders.ignore_urls = nil`, or\nset it to a different Regexp pattern to ignore.\n\nIf you want other behavior to happen on each request or response, you should be able to add it via `dev_tools` (a [ChromeRemote]((https://github.com/cavalle/chrome_remote) instance):\n\n```ruby\n      dev_tools.on \"Network.requestWillBeSent\" do |arg|\n        request = OpenStruct.new(arg[\"request\"])\n        puts \"Requesting #{request.url}\"\n      end\n\n      dev_tools.on \"Network.responseReceived\" do |arg|\n        response = OpenStruct.new(arg[\"response\"])\n        puts %(Response for #{response.url}\"\n      end\n```\n\n## Why?\n\nBecause it can be useful to be able to check the HTTP status code in tests.  Many people have wanted\nthat ability. And the WebDriver maintainers have no intention of adding this feature. See, for\nexample:\n\n- https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/141\n- https://github.com/SeleniumHQ/selenium/issues/4976\n- https://github.com/SeleniumHQ/selenium/issues/5194\n- https://groups.google.com/forum/#!msg/selenium-users/mlwxk0jKYtM/gc1ZBwZIRuEJ;context-place=searchin/selenium-users/get$20HTTP$20status$20code%7Csort:date\n- https://stackoverflow.com/questions/6509628/how-to-get-http-response-code-using-selenium-webdriver\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/TylerRick/capybara-chrome_response_headers.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftylerrick%2Fcapybara-chrome_response_headers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftylerrick%2Fcapybara-chrome_response_headers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftylerrick%2Fcapybara-chrome_response_headers/lists"}