Git Product home page Git Product logo

capybara-chrome_response_headers's Introduction

Capybara::ChromeResponseHeaders

Allows you to get HTTP status_code, response_headers, etc. when using Capybara::Selenium::Driver just like you can when using Rack::Test.

The Capybara docshttps://github.com/teamcapybara/capybara) say:

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.

But with this gem, you can have it for your Chrome Selenium driver, too!

Installation

Add this line to your application's Gemfile:

gem 'capybara-chrome_response_headers'

Usage

Inspect status_code or response_headers in your tests to get the HTTP status code or headers, respectively, for the current URL (the web page that you last visited or were redirected to).

You can access the response information for other assets using response_for_url.

Example:

      it do
        visit '/test/test_redirect?to=/test/basic_page%3Fmy_param=1'
        expect_uri '/test/basic_page?my_param=1'

        # Since it automatically follows redirects, you can't check if it was a redirect using this
        expect(status_code).to eq 200

        expect(response_headers["Content-Type"]).to eq "text/html; charset=utf-8"
        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"]

        # Could do the same for asset paths, like response_for_url["#{current_host_with_port}/asset/logo.png"]
        expect(response_for_url["#{current_host_with_port}/users/sign_in"].status).to eq 200
        expect(response_for_url["#{current_host_with_port}/users/sign_in"].headers).to be_a Hash
      end

Tracing

To 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:

Capybara::ChromeResponseHeaders.trace_requests  = true
Capybara::ChromeResponseHeaders.trace_responses = true

The output for responses is something like this:

Response for http://127.0.0.1:40359/users/sign_in: 200 OK
Response for http://127.0.0.1:40359/admin: 200 OK

By default, it tries to filter out requests for assets. (Capybara::ChromeResponseHeaders.ignore_urls is set to /\.(js|css|png|gif|jpg)$/.)

You can turn off this filtering by setting Capybara::ChromeResponseHeaders.ignore_urls = nil, or set it to a different Regexp pattern to ignore.

If 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):

      dev_tools.on "Network.requestWillBeSent" do |arg|
        request = OpenStruct.new(arg["request"])
        puts "Requesting #{request.url}"
      end

      dev_tools.on "Network.responseReceived" do |arg|
        response = OpenStruct.new(arg["response"])
        puts %(Response for #{response.url}"
      end

Why?

Because it can be useful to be able to check the HTTP status code in tests. Many people have wanted that ability. And the WebDriver maintainers have no intention of adding this feature. See, for example:

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/TylerRick/capybara-chrome_response_headers.

capybara-chrome_response_headers's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

capybara-chrome_response_headers's Issues

Net::ReadTimeout when using with selenium/node-chrome

I have a setup with capybara connecting via selenium/hub and selenium/node-chrome docker containers to a local puma.

Just including this gem in my Gemfile causes my capybara specs to fail with Net::ReadTimeout with #<TCPSocket:(closed)>.

How do I configure the ChromeRemote client to connect to the chrome node on the docker network? I would have hoped that this gem would apply the settings that are configured for the Selenium webdriver (under options[:url]).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.