36

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?

2

7 Answers 7

48

As an aside. This line

page.status_code.should = '404'

Should be

page.status_code.should == 404

This worked for me with capybara-webkit.

1
  • 14
    another capybara-webkit user is here but expect(page.status_code).to be(200) returns Capybara::NotSupportedByDriverError. Do you have any idea why?
    – scaryguy
    Sep 10, 2016 at 2:43
22

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

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.

3

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
2

Try it out

expect(page).to have_http_status(200)
3
  • 7
    Hi Ajay; your code might be correct, but with some context it would make a better answer; for example, you could explain how and why this proposed change would resolve the questioner's problem, perhaps including a link to the relevant documentation. That would make it more useful to them, and also more useful to other site readers who are looking for solutions to similar problems. Nov 30, 2016 at 13:33
  • 1
    I get expected #<Capybara::Session:0x00007fabb8a439e8> to respond to 'has_http_status?' (RSpec::Expectations::ExpectationNotMetError)
    – lafeber
    Jun 17, 2019 at 13:43
  • Note: This isn't provided by Capybara, but by RSpec. relishapp.com/rspec/rspec-rails/docs/matchers/… Jul 9, 2019 at 21:42
0

Use js to make a request and get status as below:

js_script = <<JSS
xhr = new XMLHttpRequest();
xhr.open('GET', '#{src}', true);
xhr.send();
JSS
actual.execute_script(js_script)
status = actual.evaluate_script('xhr.status') # get js variable value

Check https://gist.github.com/yovasx2/1c767114f2e003474a546c89ab4f90db for more details

0
expect(page).to have_http_status(:ok)

is an acceptable variation to Ajay's answer, if you prefer the status symbol over the code value, for readability.

References:

For all available status codes / symbols:

Rack::Utils::SYMBOL_TO_STATUS_CODE
       # or 
Rack::Utils::HTTP_STATUS_CODES

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.