1
  cy.get('div:nth-child(1) > div:nth-child(1) > div.MuiGrid-root.MuiGrid-item.MuiGrid-grid-xs-6 > div > span.MuiChip-label').then(($today) => {
    
    const today=$today.text()
    cy.log("today")
    cy.log(today.charAt(0))
  })

  cy.get('div:nth-child(1) > div:nth-child(2) > div.MuiGrid-root.MuiGrid-item.MuiGrid-grid-xs-6 > div > span.MuiChip-label').then(($tomorrow,today) => {
    const tom=$tomorrow.text()
    cy.log("tomorrow")
    cy.log(tom.charAt(0))
  })

I want to add today.charAt(0) and tom.CharAt(0), how can I do this in second then block?

4

1 Answer 1

3

It's mentioned in the documentation mentioned in the comment section, I recommend reading it. Specifically, you can read this part about closures.

What you can do is to put the other cy.get() inside the first one:

cy
  .get('...')
  .then($today => {
    
    cy
      .get('...')
      .then($tomorrow => {
        // you have access to both $today and $tomorrow
    });
});

I shortened the code (e.g. didn't use the long selectors) in order to focus on the main point.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.