测试自动化 Ruby、RSpec、Capybara:日期/时间选择器错误

时间:2021-04-29 00:24:12

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

   Failure/Error: expect(page).to have_selector("li:nth-child(1) > dl:nth-child(6) > dd")
   expected to find css "li:nth-child(1) > dl:nth-child(6) > dd" but there were no matches

以上是我的错误。我正在努力寻找一种方法来验证用户登录时登录的日期和时间。我试图验证选择器/xpath/css 是否存在,但它似乎没有找到匹配项。以下是我尝试进行验证的一些方法,但并不满意。有什么想法吗?

  expect(page).to have_selector("li:nth-child(1) > dl:nth-child(6) > dd")
  expect(page).to have_xpath("/html/body/main/ul[@class='visitors']/li[1]/dl[6]/dd")

这是我要验证的 Web 应用程序的部分页面源代码。

<ul class='visitors'>
<li>
  <dl>
    <dt>Title</dt>
    <dd></dd>
  </dl>
  <dl>
    <dt>First name</dt>
    <dd>John</dd>
  </dl>
  <dl>
    <dt>Last name</dt>
    <dd>Doe</dd>
  </dl>
  <dl>
    <dt>Email</dt>
    <dd>johndoe@gmail.com</dd>
  </dl>
  <dl>
    <dt>Signed in at</dt>
    <dd>April 26, 2021 08:25</dd>
  </dl>
</li>

1 个答案:

答案 0 :(得分:0)

当您说“验证登录的日期和时间”时,不清楚您是要验证特定日期/时间还是仅存在该日期/时间。还不清楚您是要验证显示的字段的顺序,还是只是验证日期时间字段是否在某处。假设您只是想验证具有日期时间的字段是否存在,它可能类似于

signed_in = find('.visitors dt', text: "Signed in at")
expect(signed_in).to have_sibling('dd', text: /^[A-Z][a-z]+ \d+, \d{4} \d{2}:\d{2}$/)
相关问题