Yoooo
Yoooo

Reputation: 101

How to use text from html-element in others non-cypres functions?

I need to get text from html element to use it in other checking.

But this checking must be done elsewhere later, not inside function of text getting.

That's why such variant as

> cy.get('#at__title').invoke('text').then((storedValue) => {
>    storedValue
> })

doesn't suite, because I can use text only inside then.

Also I tried to use

> cy.get('#at__title').invoke('text').as('element_text')
> this.element_text

but console log shows this.element_text as undefined.

Upvotes: 1

Views: 54

Answers (1)

Fody
Fody

Reputation: 31904

Using an alias is correct cy.get('#at__title').invoke('text').as('element_text') but to access the value like this

this.element_text

requires the test to not be an arrow function.

Use this format

it('test something', function() { 
  this.element_text

Or use the longer syntax for accessing an alias

cy.get('@element_text').then(element_text => { 

Upvotes: 1

Related Questions