I’m very new in Cypress and I’m trying to take a value from an element to use after in the test, but I can’t manage to get the value. Someone said that I need to use async await, but it is not working or maybe I’m doing something wrong. Thanks in advance!
JavaScript
x
11
11
1
it.only('should access Time Worked section and insert same Staff Complement value, but negative as Flexitime', function () {
2
let timeValue = 0;
3
cy.get('[data-tag="staff-complement-input"] > div > span').invoke('text').then(text => +text).then(($val) => {
4
// $val = 420
5
timeValue = $val;
6
cy.log(timeValue) //420
7
})
8
cy.log(timeValue) // 0
9
// need timeValue to be 420
10
})
11
Advertisement
Answer
You can use aliases and save the value and use it later.
JavaScript
1
9
1
cy.get('[data-tag="staff-complement-input"] > div > span')
2
.invoke("text")
3
.then((text) => +text)
4
.as("someNum")
5
6
cy.get("@someNum").then((someNum) => {
7
cy.log(someNum) //420
8
})
9
One point to remember is that cypress clears aliases after every test. So the above will only work if you are doing everything under one it()
block.