【问题标题】:Capybara can't find element if JS is present如果存在 JS,Capybara 找不到元素
【发布时间】:2023-03-09 04:24:01
【问题描述】:

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

所以我在表单中有这个按钮,一些 Javasrcipt 重定向到用户之前的页面。

如果我在 Capybara 能够找到 #subscription 的场景中删除 :js 但由于路径(没有 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>

【问题讨论】:

  • ID="subscription" 的项目在哪里?
  • 在这个link_to &lt;%= link_to "Inscription", new_user_registration_path, class: "dropdown-item", id:"subscription" %&gt;
  • 哦,我明白了...您正在使用下拉菜单,因此第一个操作应该是单击下拉菜单按钮。但在这种情况下,我直接去new_user_registration_path
  • 因为用JS,按钮是隐藏的,所以Capybara找不到那个ID的可见项。

标签: ruby-on-rails rspec capybara


【解决方案1】:

我直接跳到注册页面,因为下拉菜单中的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

【讨论】:

  • 是的,这就是我的第一个,我想测试“漫长的道路”。顺便说一句,这使工作,不是预期的,但我会这样做
  • “工作不是预期的”是什么意思?你也可以给父
    一个 ID 并首先点击它,如果你想检查很长的路。让我改写答案。
  • 谢谢,但像这样找不到#dropdown
  • 什么通常会触发下拉菜单?我无法从您的代码中分辨出....是悬停,还是单击,还是...?无论如何,您应该告诉 Capybara 采取与启用 JS 时用户会采取的完全相同的操作,因为它会尽可能地表现得像实际用户一样。
  • @johan 您没有回答问题 - 悬停或点击会触发下拉菜单吗?
猜你喜欢
相关资源
最近更新 更多
热门标签