Search code examples
rspeccapybaracapybara-webkit

Capybara-webkit unexpected behavior with url routes, returns blank page


I encountered an issue using Capybara-webkit. A simple example code like this that should show the sign in page fails:

feature 'user signs in' do
  scenario 'sign in successfully', js: true do
    user = create(:user)

    visit new_user_session_url
    expect(page).to have_text 'Sign in'
  end
end

It gives a failure message:

Failure/Error: expect(page).to have_text 'Sign in'
    expected to find text "Sign in" in ""

save_and_open_page shows a blank page with empty <body></body>. Everything works fine if js: true is removed.

I know this spec doesn't require js to be set. But the specs that depend on javascript also fail. So this is just a sample spec.

Anybody with an explanation why Capybara-webkit behaves like this? Is it normal, an issue or there is something I have not set correctly?

[Note] The app uses subdomains. Could it be that Capybara-webkit doesn't play well with subdomains?


Solution

  • The problem came from my own mistake. In my controller, I am checking for the subdomain that comes with the request.url using request.subdomains. This returns array of subdomains present before the host in the url.

    In my test, I stub ActionDispatch::Request to accept :subdomains method and respond with the provided subdomain. Then, it can proceed with the verification in the controller.

    Because there are places where I set the subdomain attribute in a route url on the fly, I was having different values for request.subdomain (the new one set in the url) and request.subdomains (the old one before setting the one in the url). So I decided to go with the new one and just split it with .split('.') since that is what I need to work with.

    After changing that, I forgot to update the stub in the spec to change the request method to subdomain.

    After changing it, everything works fine.

    I hope this helps anyone facing this same problem. At least it will remind them to update all stubs they have if any.

    [Update] Apparently, the reason why I was getting different values for the 2 subdomain method was because of the stub in the spec.