0

I want to test that a user can skip the shipping_address form after his registration.

So I have this button in the form, with some Javasrcipt that redirect to the page the user was before.

If I remove the :jsin the scenario Capybara is able to find #subscription but the test fails because of the path (no js no redirection). If I put the:jsit can't find the #subscription Would you please give me hints for this?

shipping_addresses/new.html.erb

<%= link_to 'Remplir plus tard', 'javascript:history.go(-1);', class: "btn btn-secondary btn-block" %> 

Here is the test

scenario "skipping the address form for now", :js do visit root_path find('#subscription').click choose "Monsieur" fill_in "user[first_name]", with: "Benoit" fill_in "user[last_name]", with: "Durant" fill_in "user[birth_date]", with: "08/05/1981" fill_in "user[email]", with: "[email protected]" fill_in "user[email_confirmation]", with: "[email protected]" fill_in "user[password]", with: "password" fill_in "user[password_confirmation]", with: "password" click_on "Valider mon inscription" expect(page).to have_content("REMPLISSEZ VOTRE ADRESSE DE LIVRAISON") click_on "Remplir plus tard" expect(current_path).to eq root_path end 

This is the dropdown in my navbar

<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownMenuLink"> <%= link_to "Connection", new_user_session_path, class: "dropdown-item" %> <%= link_to "Inscription", new_user_registration_path, class: "dropdown-item", id:"subscription" %> </div> 

edit more of my navbar

 <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav ml-auto"> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <% if current_user %> <%= current_user.first_name %> <% else %> Mon Compte <% end %> </a> <% if current_user %> <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownMenuLink"> #Just removed the links to clear the code </div> <% else %> <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownMenuLink"> <%= link_to "Connection", new_user_session_path, class: "dropdown-item" %> <%= link_to "Inscription", new_user_registration_path, class: "dropdown-item", id:"subscription" %> </div> <% end %> </li> </ul> </div> 
4
  • Where's the item with ID="subscription"? Commented May 9, 2019 at 13:39
  • it's in this link_to <%= link_to "Inscription", new_user_registration_path, class: "dropdown-item", id:"subscription" %> Commented May 9, 2019 at 13:40
  • Oh I see it... you are using a drop-down menu, so the first action should be to click on the drop down menu button. But in this case, I'd just directly go to new_user_registration_path Commented May 9, 2019 at 13:40
  • It's because with JS, the button is hidden, so Capybara can't find a visible item with that ID. Commented May 9, 2019 at 13:41

1 Answer 1

2

I'd just jump to the registration page directly, because the button is invisible for Capybara in the drop-down menu:

scenario "skipping the address form for now", :js do visit new_user_registration_path choose "Monsieur" fill_in "user[first_name]", with: "Benoit" fill_in "user[last_name]", with: "Durant" fill_in "user[birth_date]", with: "08/05/1981" fill_in "user[email]", with: "[email protected]" fill_in "user[email_confirmation]", with: "[email protected]" fill_in "user[password]", with: "password" fill_in "user[password_confirmation]", with: "password" click_on "Valider mon inscription" expect(page).to have_content("REMPLISSEZ VOTRE ADRESSE DE LIVRAISON") click_on "Remplir plus tard" expect(current_path).to eq root_path end 

Alternatively you could also click on the arrow first, by giving the surrounding DIV an ID (I think... depends a bit on the CSS framework).

<div id="dropdown" class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownMenuLink"> <%= link_to "Connection", new_user_session_path, class: "dropdown-item" %> <%= link_to "Inscription", new_user_registration_path, class: "dropdown-item", id:"subscription" %> </div> 

And do the following:

scenario "skipping the address form for now", :js do visit root_path find('#dropdown').click find('#subscription').click choose "Monsieur" fill_in "user[first_name]", with: "Benoit" fill_in "user[last_name]", with: "Durant" fill_in "user[birth_date]", with: "08/05/1981" fill_in "user[email]", with: "[email protected]" fill_in "user[email_confirmation]", with: "[email protected]" fill_in "user[password]", with: "password" fill_in "user[password_confirmation]", with: "password" click_on "Valider mon inscription" expect(page).to have_content("REMPLISSEZ VOTRE ADRESSE DE LIVRAISON") click_on "Remplir plus tard" expect(current_path).to eq root_path end 
Sign up to request clarification or add additional context in comments.

10 Comments

yes that's what I had first, I wanted to the test the "long way". Btw this makes the job, not what expected but I will do this
What do you mean with the "job not what expected"? You can also give the parent <div> an ID and click on that first, if you prefer to check the long way. Let me rephrase the answer.
thanks but like this it doesn't find the #dropdown
What normally triggers the dropdown? I can't tell from your code.... is it a hover, or click, or...? Anyway, you should tell Capybara to take that exact same action as a user would take when you enable JS, because it tries to behave as much as an actual user as it is capable of.
@johan You didn’t answer the question - does a hover or click trigger the dropdown?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.