0
   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

Above is my error. I'm struggling to find a way to verify the date and time logged in when a user signs in. I have tried to validate that the selector/xpath/css exist, but it doesn't seem to find a match. Below is some ways I tried to proceed to verify, but it's not happy. Any ideas?

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

Here is part of the page source from the web application I'm trying to validate.

<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>[email protected]</dd>
  </dl>
  <dl>
    <dt>Signed in at</dt>
    <dd>April 26, 2021 08:25</dd>
  </dl>
</li>
3
  • better way is to assign some class to <dl> tag of date field. i.e. <dl class="sign-in-at"> use the class in xpath while verifying the content. Is it possible for you to add class? Commented Apr 29, 2021 at 3:08
  • 1
    in above example date is 5th child. so expect(page).to have_selector("li:nth-child(1) > dl:nth-child(5) > dd") this should work Commented Apr 29, 2021 at 3:11
  • 1
    Yup, @SampatBadhe is right. Just open your browser console and play with selectors $(".visitors li:nth-child(1) > dl:nth-child(5) > dd").innerText gives April 26, 2021 08:25" Commented May 1, 2021 at 21:15

1 Answer 1

0

When you say "verify the date and time logged in" it's unclear whether you're trying to verify a specific date/time or just that one exists. It's also unclear whether you're trying to verify the order of the fields shown, or just verify that the date time field is there somewhere. Assuming you're just trying to verify that a field with a date time exists it could be something like

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}$/)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.