Home > Software engineering >  How to use within() function with contains() function incypress
How to use within() function with contains() function incypress

Time:01-20

I have a datepicker calendar as below [![enter image description here][1]][1] To select a particular date I am using this code

cy.get('.fa-calendar').click();
cy.get('[label="select-year"]').select(2023);
cy.get('[label="select-month"]').select('Jan');

Now to select the date I am using below approach which is working fine

cy.get('.ngb-dp-month>ng-star-inserted:nth-of-type(1)').within(()=>{
cy.get('.ngb-dp-day').contains('10').click();
})

but I don't wanted to use nth-of-type(1) because it will cause issue in future I tried this approach whic didn't work

cy.contains('January 2023').within(()=>{
    cy.get('.ngb-dp-day').contains('10').click();
    });

Can you folks help me resolving the issue also any other alternative is also welcome [1]: https://i.stack.imgur.com/38XyO.jpg

CodePudding user response:

Basically, you just need to combine the two different ways you tried: cy.get('.ngb-dp-month') and cy.contains('January 2023')

This should work

cy.contains('.ngb-dp-month', 'January 2023').within(()=> {
  cy.contains('.ngb-dp-day', '10')
    .click()
})
  • Related