33

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?

deb
  • 11,136
  • 19
  • 62
  • 83
  • related: https://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/KdMvhjcjm8E – tokland Oct 12 '12 at 10:29
  • Possible duplicate of [How to get HTTP Response Code using Selenium WebDriver with Java?](https://stackoverflow.com/questions/6509628/how-to-get-http-response-code-using-selenium-webdriver-with-java) – David Moles Jun 06 '18 at 21:42

6 Answers6

47

As an aside. This line

page.status_code.should = '404'

Should be

page.status_code.should == 404

This worked for me with capybara-webkit.

Apie
  • 6,091
  • 6
  • 23
  • 24
  • 11
    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 '16 at 02:43
19

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

eugen
  • 8,170
  • 11
  • 54
  • 63
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
Oxyless
  • 41
  • 2
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.

Leonid Shevtsov
  • 13,290
  • 8
  • 48
  • 80
2

Try it out

expect(page).to have_http_status(200)
Ingo Karkat
  • 154,018
  • 15
  • 205
  • 275
Ajay
  • 97
  • 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. – Vince Bowdren Nov 30 '16 at 13:33
  • 1
    I get `expected # to respond to 'has_http_status?' (RSpec::Expectations::ExpectationNotMetError)` – lafeber Jun 17 '19 at 13:43
  • Note: This isn't provided by `Capybara`, but by `RSpec`. https://relishapp.com/rspec/rspec-rails/docs/matchers/have-http-status-matcher – XtraSimplicity Jul 09 '19 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

G. I. Joe
  • 1,332
  • 14
  • 21