如果存在JS,则水豚找不到元素

时间:2019-05-09 13:02:51

标签: ruby-on-rails rspec capybara

我想测试用户在注册后可以跳过shipping_address表格。

所以我在表单中有此按钮,带有一些Javasrcipt,可将其重定向到用户之前的页面。

在这种情况下,如果我删除了:js,则Capybara能够找到#subscription,但由于路径而导致测试失败(无js无重定向)。 如果我放:js,则找不到#subscription 您能给我提示吗?

shipping_addresses / new.html.erb

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

这是测试

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: "benoit@example.com"
    fill_in "user[email_confirmation]", with: "benoit@example.com"
    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

这是我的导航栏中的下拉列表

<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>   

编辑更多我的导航栏

    <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>

1 个答案:

答案 0 :(得分:2)

我直接跳到注册页面,因为该按钮在下拉菜单中对于Capybara不可见:

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: "benoit@example.com"
    fill_in "user[email_confirmation]", with: "benoit@example.com"
    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

或者,您也可以通过为周围的DIV提供一个ID(我认为...在一定程度上取决于CSS框架)来首先单击箭头。

<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>  

并执行以下操作:

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: "benoit@example.com"
    fill_in "user[email_confirmation]", with: "benoit@example.com"
    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