无法在 cypress 命令 cy.get() 内设置变量的值以在命令外使用

rea*_*dom 5 javascript cypress

我正在设置一个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
Run Code Online (Sandbox Code Playgroud)

Mr.*_* J. 2

看来您正在努力应对范围。对我有用的是:

    cy.get('.original_object')
      .then($value => {
        const retreivedValue = $value.text()
    cy.get('.test_object')
      .should('contain', retreivedValue)
Run Code Online (Sandbox Code Playgroud)