Reputation: 24885
I have a simple form consisting of a text field and a submit button, used for searching. I want to test in Capybara, that if the text field is empty, nothing heppens upon clicking on the submit button or pressing enter. How do I do that?
Upvotes: 0
Views: 1005
Reputation: 5529
I know this is old, but for the sake of providing an answer, the Capybara session matchers work well to make sure the page hasn't changed:
visit register_path
within 'form.register' do
fill_in 'Username', with: username
fill_in 'Password', with: password
find_button('Register').click
end
expect(page.has_current_path?(register_path)).to be_truthy
You can also use another method to compare it against the url/path that was visited at the start of the test, even though it's a bit redundant compared to the above:
current_page = page.current_url # can use current_path too
expect(page.current_url).to eq(current_page)
If you want to test validations specifically, you may also want to check that validations are being checked instead of trying to target the error Testing HTML5 Form Validations when using simple_form Rails.
Upvotes: 1
Reputation: 135
Hm, good question. I would maybe think of what I want my app to behave like. Usually you want something to happen. Otherwise the User would not know if something is wrong with the app or if he made a mistake.
Think in terms of the User. Why would I interact with your form if I expected it to do nothing.
scenario 'As a User When I submit an empty form Then it tells me what I can do useful' do
visit '/path_to_your_page'
click_button 'Your submit button'
expect(page).to have_content 'Please enter something I want'
end
Ah, now I get your idea. You mean like google for example. When I enter nothing and press enter nothing happes. And you want to write a test for that case.
Hm, I actually don't know how google for example does that. I could imagine that they only allow to submit requests if at least one character is entered. I guess it is done with javascript in frontend.
Upvotes: 1