0
getQuery()
{
    let countOfElements = 0;
    let query=""
     cy.get("elementid").then($elements => {
      countOfElements = $elements.length;
      console.log(countOfElements)
      for (let i = 0; i < countOfElements; i++) {
        cy.get("elementid").eq(i).then( $value => {
          const textValue = $value.text()
          query =query + " " + textValue
        })
      }
      cy.log(query)
    });
    return query
}

I am trying to get the text inside one element which scattered in multiple spans.my logic is follows

1: from the parent path get the total number of spans

2: Get the text from each span and append each other by looping through all the spans

Problem 1: return query is always coming as empty, how I can store the value and return the concatenated query?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Deepak
  • 11
  • 3
  • Convert `getQuery` to an `async` method and await in all the places. The reason you're getting an empty string is because you return `query` before all the promises are resolved. Does [this](https://stackoverflow.com/questions/49980311/cypress-io-how-to-handle-async-code) help? – 0xts Aug 02 '23 at 08:05
  • Thanks @0xts . If possible could u share any refernce for the usage or sample code snippet..? – Deepak Aug 02 '23 at 09:16
  • It's almost the same question https://stackoverflow.com/questions/76815840/cypress-cannot-return-value-in-a-method-if-each-is-used?noredirect=1#comment135421811_76815840 – jabaa Aug 02 '23 at 10:01

0 Answers0