0

I have the following system test (Rails backend, React/Redux frontend single-page application):

test "can sign up user properly" do
    visit '/'
    within ('.signup-form-box') do
       fill_in 'Username', with:'userp'
       fill_in "Email Address", with: "t!"
       fill_in "Password", with: "12345679"
       click_on "Sign Up"
    end
    assert page.current_path ==  '/books'
end

Its supposed to lead the user to the '/books' page after a successful signup. However this test fails. When i change the assertion to :

 assert page.current_path ==  '/'

it passes. What is the proper way to check this sort of thing?

sco
  • 367
  • 3
  • 14
  • Does it work in development using an actual browser? – Bill Doughty May 24 '18 at 21:28
  • Yes works fine when I try it manually – sco May 24 '18 at 21:32
  • Have your tried adding a wait before `assert page.current_path == '/books'`? Since you're testing in an actual browser, you may need to allow for real delays incurred in the redirection. Trying adding `sleep 5` before your assertion. – Bill Doughty May 24 '18 at 21:37

1 Answers1

0

click_on is not guaranteed to wait for any actions triggered by it because Capybara has no way of knowing what, if any, actions are going to be triggered. Specifically for this reason, Capybara has waiting behavior built-in to its finders, matchers, and assertions to allow for pages to catch up to the test. In this case you're using a generic assert against a Capybara returned thing rather than the Capybara provided matcher. Instead of assert page.current_path == '/books' you should be using

assert_current_path '/books'

which will keep checking the path for a while to see if/when it changes. If that doesn't fix your issue then it would imply your login/signup is failing, which should be obvious to confirm by looking at the page (html or screenshot).

Note: I would assume your test should fail since putting t! as an email address should fail validation.

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • So, I tried with your suggestion but when I look at the browser and what the test does, it enters the login information, signs up successfully goes to `/books` as expected and then at the very end REVERTS back to the root page `/` When I do it manually myself, everything works, so it's something with the way I am writing the test. For some reason it goes back to `/` at the very end which is why the test fails – sco May 25 '18 at 02:09
  • @GeorgeMNajm What error exactly are you getting? What you describe doesn't make any sense, unless the login is failing (or you have a JS driven redirect in your /books page) or you've done something silly like set `Capybara.default_max_wait_time` to a very small number. Check your `test.log` and add the relevant contents (from the POST sent when you click Sign Up and results) to your question. – Thomas Walpole May 25 '18 at 02:31