Selenium annoyingly does not provide any header or HTTP status data, so I wrote this middleware to insert an HTML comment containing an HTTP status code for use with Capybara.
module Vydia module Middleware class InjectHeadersForSelenium def initialize(app) @app = app end def call(env) @status, @headers, @response = @app.call(env) if @headers["Content-Type"] && @headers["Content-Type"].include?("text/html") @prepend = "<!-- X-Status-Code=#{@status} -->\n" @headers = @headers.merge( "Content-Length" => (@headers["Content-Length"].to_i + @prepend.size).to_s ) end [@status, @headers, self] end def each(&block) if @prepend yield(@prepend) @prepend = nil end @response.each(&block) end end end end
In tests, you can get the status code as follows:
def get_http_status begin page.driver.status_code rescue Capybara::NotSupportedByDriverError matches = //.match(page.body) matches && matches[1] && matches[1].to_i end end get_http_status
animatedgif
source share