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.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()

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

  • 2
    The various options are all covered in docs.cypress.io/guides/core-concepts/variables-and-aliases
    – jonrsharpe
    Commented Oct 2, 2021 at 10:42
  • That fixed my issue, thanks a lot.
    – Rao Thota
    Commented Oct 2, 2021 at 11:15
  • Just note that this question is about Javascript's closures. It is better asked in Stack Overflow rather than SQA Stack Exchange. Commented Oct 3, 2021 at 16:28
  • Can a value be returned from then to be used by another function.
    – user53774
    Commented Jul 19, 2022 at 12:21

1 Answer 1


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:

  .then($today => {
      .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 you have read our privacy policy.

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