[英]Cannot set a variable's value inside cypress command cy.get() to use outside the command
我设置一个pin
变量,在更新它cy.get()
然后尝试使用它后cy.get()
-它不允许我这样做。
我也在这里读到这是不可能的: https : //docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Return-Values 。
我真的需要使用这个变量才能登录:它是一个生成的PIN,我需要在登录时使用它。
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
问题在于同步:函数invoke
返回一个Promise,它以异步方式执行。 代码cy.log(pin)
在invoke
之后和promise的解析之前执行。
试试这个:
cy.get('.pin-field').invoke('text').then(pin => {
cy.log(pin);
})
或者您可以使用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
}
不要忘记,必须在async
函数中关闭具有await
的代码。
好像你正在努力争取范围。 对我有用的是:
cy.get('.original_object')
.then($value => {
const retreivedValue = $value.text()
cy.get('.test_object')
.should('contain', retreivedValue)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.