Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get occurrences of text in cypress test

Is there a way to get the number of occurrences of a certain text in an unordered list without using any Javascript in Cypress?

I have something like this:

<ul>
    <li>
        14 May, 2018
    </li>

    <li>
        14 May, 2018
    </li>

    <li>
        23 Aug, 2018
    </li>

    <li>
        14 May, 2018
    </li>
</ul>

I want to count the occurrences of 14 May, 2018. I can not use contain() as it would only get the first element.

I did not find anything in the docs.

like image 604
aitchkhan Avatar asked Sep 19 '25 05:09

aitchkhan


1 Answers

This was rather too easy.

cy.get(`ul`)
   .get('li:contains(14 May, 2018)')
   .should('have.length', 3);
like image 83
aitchkhan Avatar answered Sep 20 '25 19:09

aitchkhan