admin管理员组文章数量:1326076
I'm setting a pin
variable, updating it in cy.get()
and then trying to use it after the cy.get()
- it doesn't allow me to do this.
I've also read here that this is not possible: .html#Return-Values.
I really need to use this variable in order to be able to login: it's a generated PIN and I need to use it when logging in.
var pin = ""
cy.get('.pin-field').invoke('text').then((text1) => {
pin = text1; //assign text1 value to global pin variable, does not work
cy.log(text1) // this works and logs the value of text1
})
cy.log(pin) //this just logs an empty
I'm setting a pin
variable, updating it in cy.get()
and then trying to use it after the cy.get()
- it doesn't allow me to do this.
I've also read here that this is not possible: https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Return-Values.
I really need to use this variable in order to be able to login: it's a generated PIN and I need to use it when logging in.
var pin = ""
cy.get('.pin-field').invoke('text').then((text1) => {
pin = text1; //assign text1 value to global pin variable, does not work
cy.log(text1) // this works and logs the value of text1
})
cy.log(pin) //this just logs an empty
Share
Improve this question
edited Mar 8, 2019 at 12:36
ttulka
10.9k7 gold badges44 silver badges56 bronze badges
asked Mar 8, 2019 at 12:11
reachfreedomreachfreedom
811 silver badge6 bronze badges
2 Answers
Reset to default 3The problem is in synchronization: The function invoke
returns a Promise, which is executed in async manner. The code cy.log(pin)
is executed just right after the invoke
is called and before the promise is resolved.
Try this:
cy.get('.pin-field').invoke('text').then(pin => {
cy.log(pin);
})
Or you can simulate synchronous behavior with async/await
:
async function login(cy) {
const pin = await cy.get('.pin-field').invoke('text'); // call it synchron
cy.log(pin); // this code executes when 'invoke` returned
}
Don't forget, the code with await
must be closed in an async
function.
Seems like you are struggling with the scope. What works for me is this:
cy.get('.original_object')
.then($value => {
const retreivedValue = $value.text()
cy.get('.test_object')
.should('contain', retreivedValue)
本文标签:
版权声明:本文标题:javascript - Cannot set a variable's value inside cypress command cy.get() to use outside the command - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742195191a2430948.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论