1

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.

Yoooo
  • 89
  • 6

1 Answers1

1

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 => { 
Fody
  • 23,754
  • 3
  • 20
  • 37
  • But I already have used `function()` both in `describe` and in `it`. https://paste.pics/8bf28b1fede259712c974f68254ca239 – Yoooo May 03 '22 at 08:31
  • That's not going to work, it's in the same block and the the `cy.checkDB()` command gets it's parameter values before the above `cy.get()` finishes. In that scenario you should use a `.then()` because you are actually using the value straight after it is obtained. – Fody May 03 '22 at 08:42
  • You can still use the alias later in the test, but beware of closure problems like the one you describe above. – Fody May 03 '22 at 08:44
  • I tried to use alias, but it doesn't return text. It returns [object Object]. https://paste.pics/bf2053d3d2385520223d90fdb69c297e – Yoooo May 03 '22 at 08:58
  • Again, not going to work - it's the wrong syntax. See my example above. – Fody May 03 '22 at 09:05
  • Sorry, just I wrote in my question that using `then` doesn't suite, because I can use text only inside `then`. There is not any other possibilities? – Yoooo May 03 '22 at 09:15