0

I am trying to get a div content from a page in order to use it later in the test process . This is an extract of the structure of the page:

<div class="ulvVbt">
    <div class="_3UBePl _8RD9KV _1XLymx c9bfgn _1I79CZ">Ihre Bestellnummer</div>
    <div class="_3UBePl t9q4wB">01235456789</div>
</div>

As you can see, the classes are encrypted, therefore I had to use the following to get the text with Cypress:

getOrderId() : string {
    cy.findByText('Ihre Bestellnummer').siblings('div').invoke('text').then((text) => {
        return text;
    });
    return "Content not found";
}

Unfortunately, this always returns "undefined". To check if the construct in general would work, I used the .should function which is working fine (of course it's failing as I do not know the expected value at that point):

cy.findByText('Ihre Bestellnummer').siblings('div').should('have.text', 'something')

So, how can I read and store the text of the div container in order to use it later.

Thanks for any inputs.

Andy

andyRandy
  • 93
  • 1
  • 3
  • 10
  • you're returning "Content not found" immediately after invoking the cy.findByText command, without waiting for the asynchronous invoke('text') callback to execute. As a result, the function returns before the text value is retrieved, hence the "undefined" return value. – Murat Ishenbaev Jun 19 '23 at 20:41

0 Answers0