【问题标题】:Selecting elements and its parent, with all Strings that it got选择元素及其父元素,以及它获得的所有字符串
【发布时间】:2019-11-26 10:07:00
【问题描述】:

我使用 Capybara 编写验收测试。我想确保我的元素在页面上可见。我如何选择元素以及它获得的所有字符串?

页面看起来像这样

<tr>
              <td class="pl-4">ABC</td>
              <td class="text">5000$</td>
              <td>
                <span class="spanclass">
                  SpanClassExample
                </span>

                  <br>
                  <a class="badge badge-secondary" href="//.../">CLASS</a>
              </td>
              <td class="text-right">NAME1</td>
              <td>NAME2</td>
              <td>NAME3</td>
              <td>NAME4</td>
              <td>NAME5</td>
</tr>

目前我使用这种方法

page.all(".text-right", text: 'NAME1')[0].visible?

但我需要找到包含所有字符串的元素

NAME1 NAME2 NAME3 NAME4 NAME5)。

还有这个元素

(span class="spanclass" SpanClassExample /span>)

【问题讨论】:

  • 您能否准确说明您想要获得什么。 'NAME2 NAME3 NAME4 NAME5' 不在类 .text-right 的元素内,因此您声明该元素具有这些字符串是没有意义的。此外,默认情况下,Capybara 只会找到可见元素,因此检查 visible? 也没有意义(visible? 也只返回 true 或 false,所以调用它实际上并不能验证任何内容)

标签: ruby-on-rails ruby rspec capybara selector


【解决方案1】:

我建议在行中添加一个 ID,例如:

<tr id="thing-<%= thing.id %>">

这样你以后可以做:

within("tr#thing-#{thing.id}") do
  expect(page).to have_content thing.name # or whatever you want to look for
end

【讨论】:

    猜你喜欢
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 2011-02-11
    相关资源
    最近更新 更多