水豚在页面上找不到选择器

Capybara can't find selector on page

我正在尝试测试 bootstrap 导航栏中是否存在某些表单字段,但无法找到 Capybara 选择器的正确组合来找到它们。

我在 Capybara 中尝试了以下选项:

  should have_selector('id', text: 'email')
  should have_selector('name', text: 'email')
  should have_selector('div.input.id', text: 'email')
  should have_selector('div.input.placeholder', text: 'email')

而且我总是收到错误消息:

       Failure/Error: expect (should have_selector('div.input.placeholder', text: 'email'))
       expected to find visible css "div.input.placeholder" with text "email" but there were no matches

我对水豚还很陌生,这可能是我的问题,但我查看了文档,这似乎应该可行。

我猜这与它被隐藏有关,但当您转到该页面时用户可以看到它,所以我不确定如何让它对水豚可见。

这里是 HTML 代码:

    <div class="navbar-collapse collapse">
      <form class="navbar-form navbar-right" role="form" action="/sessions" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="klUhJ9epE/Sr/N0okPq5WMWi2XJuQXiAPmdg/9Qf2d8TBX+htbIdUPRh01YPWqMTRin8vTlVG/ECUtvKNczc+A==" />
        <div class="form-group">
          <input type="text" name="email" id="email" class="form-control" placeholder="Email" />
        </div>
        <div class="form-group">
          <input type="password" name="password" id="password" class="form-control" placeholder="Password" />
        </div>
        <input type="submit" name="commit" value="Sign in" class="btn btn-primary" />
      </form>
    </div><!--/.navbar-collapse -->

这是实际页面的视图:

div.input.placeholder 是一个 CSS select 或者 select 一个带有 类 'input' 的 div 元素和'placeholder' - 我假设这不是您实际要检查的内容。

如果您正在尝试测试您会做的属性值

have_selector('input[id="email"][placeholder="Email"]')

或更好,因为您使用的是 CSS

have_css('input[id="email"][placeholder="Email"]')

或者你可以使用 Capybaras built-in :field select 或者键入并执行

have_field(id: 'email', placeholder: 'Email')

但实际上 - 像这样检查属性对于功能测试来说是多余的。只需填写字段

fill_in('Email', with: 'my_email@blah.com')

确认相关字段存在,可通过标签或字符串占位符定位'Email',可填写。