skip to Main Content

I am trying to pass multiple condition in single if but code is ignoring it.

cy.get("locator").each($el) => {
if($el.hasClass("flatpiker-day ") && $el.text() == day)
   {
   //some code
   return false;
   }
 }

2

Answers


  1. The condition $el.hasClass('flatpiker-day') could be .find('.'flatpiker-day') and the condition $el.text() == day could be .contains(day), so a good test would be

    cy.get('locator')
      .contains('.flatpiker-day', day)
      .then($el => ... one element received here
    

    But please note, if you are testing a calendar, the display can sometimes show the day twice.

    You would have to give detail of the DOM to resolve that.

    Login or Signup to reply.
  2. If this is the HTML of the calendar

    <div class="dayContainer">
    <span class="flatpickr-day prevMonthDay" aria-label="September 25, 2022" tabindex="- 1">25</span> 
    <span class="flatpickr-day prevMonthDay" aria-label="September 26, 2022" tabindex="- 1">26</span>
    <span class="flatpickr-day prevMonthDay" aria-label="September 27, 2022" tabindex="- 1">27</span> 
    <span class="flatpickr-day prevMonthDay" aria-label="September 28, 2022" tabindex="- 1">28</span> 
    <span class="flatpickr-day prevMonthDay" aria-label="September 29, 2022" tabindex="- 1">29</span> 
    <span class="flatpickr-day prevMonthDay" aria-label="September 30, 2022" tabindex="- 1">30</span> 
      ...
    

    then you can avoid double-up of day === 30 by using the aria-label, which has the full date in it’s value.

    cy.get('div.dayContainer')
      .find('span[aria-label="October 30, 2022"]')
      .click()
    

    In general, aria attributes often provide a good way to select an element – see testing-library for a full discussion.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search