How to test the response code with Capybara + Selenium

25,442

Solution 1

status_code is not currently supported by the Selenium driver. You will need to write a different test to check the response status code.

Solution 2

As an aside. This line

page.status_code.should = '404'

Should be

page.status_code.should == 404

This worked for me with capybara-webkit.

Solution 3

Either switch to another driver (like rack-test) for that test, or test that the displayed page is the 404 page (should have content 'Not Found' in h1).

As @eugen said, Selenium doesn't support status codes.

Solution 4

Selenium web driver doest not implement status_code and there is no direct way to test response_code with selenium (developer's choice).

To test it I added in my layout/application.html.erb:

<html code="<%= response.try(:code) if defined?(response) %>">[...]</html>

And then in my test:

def no_error?
  response_code = page.first('html')[:code]
  assert (response_code == '200'), "Response code should be 200 : got #{response_code}"
end

Solution 5

Try it out

expect(page).to have_http_status(200)
Share:
25,442
deb
Author by

deb

Updated on January 12, 2020

Comments

  • deb
    deb over 4 years

    I have the following spec:

    it "deletes post", :js => true do 
    ...
    ...
    page.status_code.should = '404'
    
    end 
    

    The page.status_code line is giving me this error:

    Capybara::NotSupportedByDriverError
    

    How do I check the page's status code?