Reputation: 12814
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?
Upvotes: 36
Views: 27736
Reputation: 788
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
Upvotes: 0
Reputation: 1653
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
Upvotes: 0
Reputation: 51
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
Upvotes: 4
Reputation: 6431
As an aside. This line
page.status_code.should = '404'
Should be
page.status_code.should == 404
This worked for me with capybara-webkit.
Upvotes: 48
Reputation: 14179
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.
Upvotes: 3
Reputation: 9226
status_code
is not currently supported by the Selenium driver. You will need to write a different test to check the response status code.
Upvotes: 24