从if语句中传递可变值

发布于 2025-02-08 04:47:42 字数 613 浏览 1 评论 0原文

如果条件还可以,我只需要通过按钮变量传递true即可。然后,如果按钮变量值为真,则需要传递测试用例。有人可以帮我解决这个问题吗?这就是我到目前为止所得到的。

cy.get('@checkSkipButton').then(checkSkipButton => {
    if (checkSkipButton) {
        cy.log("NOT CLICKABLE")
        cy.then(button = true)
        //cy.log(button)
        //return button;
        
        //cy.log(button)
        //return button
    
            }else{
                cy.log("CLICKABLE")
                button = False
                //return this
            } 

        
        }    )
        cy.log("value is"+button)
        button.should(have,value=true)
        
    }

I just need to pass true for the button variable if the if condition is ok. Then i need to pass the test case if the button variable value is true. Can someone help me to fix this. This is what i have got so far.

cy.get('@checkSkipButton').then(checkSkipButton => {
    if (checkSkipButton) {
        cy.log("NOT CLICKABLE")
        cy.then(button = true)
        //cy.log(button)
        //return button;
        
        //cy.log(button)
        //return button
    
            }else{
                cy.log("CLICKABLE")
                button = False
                //return this
            } 

        
        }    )
        cy.log("value is"+button)
        button.should(have,value=true)
        
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜柠檬 2025-02-15 04:47:42

该代码接近工作,但是false而不是false,然后返回按钮让您链接.shold() /代码>测试。

cy.get('@checkSkipButton').then(checkSkipButton => {
  let button;
  if (checkSkipButton) {
    cy.log("NOT CLICKABLE")
    button = true;
  } else {
    cy.log("CLICKABLE")
    button = false;
  } 
  return button       // passes to should
}
.should('eq', true)

The code is close to working, but false instead of False and return the button to let you chain the .should() test.

cy.get('@checkSkipButton').then(checkSkipButton => {
  let button;
  if (checkSkipButton) {
    cy.log("NOT CLICKABLE")
    button = true;
  } else {
    cy.log("CLICKABLE")
    button = false;
  } 
  return button       // passes to should
}
.should('eq', true)
莫言歌 2025-02-15 04:47:42

我认为这是相同的逻辑

cy.get('@checkSkipButton')
  .then(checkSkipButton => !!checkSkipButton)  // convert truthy to true/false 
  .then(value => cy.log("value is" + value))
  .should('eq', true)

I think this is the same logic

cy.get('@checkSkipButton')
  .then(checkSkipButton => !!checkSkipButton)  // convert truthy to true/false 
  .then(value => cy.log("value is" + value))
  .should('eq', true)
街道布景 2025-02-15 04:47:42

如果要全球使用按钮,则必须使用then()来等待其值

let button;    // undefined now and also later in the test if not wrapped in .then()

cy.get('@checkSkipButton').then((checkSkipButton) => {
  if (checkSkipButton) {
    cy.log('NOT CLICKABLE')
    button = true;
  } else {
    cy.log('CLICKABLE')
    button = false;
  }
})

cy.wrap(button).should('equal', true) // Error: expected undefined to equal true
                                      // neither true nor false, but original value (undefined)
                              
// Allow above cy.get() to process
cy.then(() => {
  cy.log('Value is: ' + button);        
  cy.wrap(button).should('equal', true) // passes
})

If you want to use button globally, you must wait for it's value to be set by using .then()

let button;    // undefined now and also later in the test if not wrapped in .then()

cy.get('@checkSkipButton').then((checkSkipButton) => {
  if (checkSkipButton) {
    cy.log('NOT CLICKABLE')
    button = true;
  } else {
    cy.log('CLICKABLE')
    button = false;
  }
})

cy.wrap(button).should('equal', true) // Error: expected undefined to equal true
                                      // neither true nor false, but original value (undefined)
                              
// Allow above cy.get() to process
cy.then(() => {
  cy.log('Value is: ' + button);        
  cy.wrap(button).should('equal', true) // passes
})
妄想挽回 2025-02-15 04:47:42

您非常接近,上面的情况会很好地进行一些修改。

let button //Declare it globally so that it can be accessed outside if
cy.get('@checkSkipButton')
  .then((checkSkipButton) => {
    if (checkSkipButton) {
      cy.log('NOT CLICKABLE')
      button = true //instead of cy.then directly update the value of button
    } else {
      cy.log('CLICKABLE')
      button = false
    }
  })
  .then(() => {
    cy.log('Value is: ' + button) //prints the button value
    cy.wrap(button).should('equal', true) //Asserts the button value
  })

You were very close, the above will work fine with some minor changes.

let button //Declare it globally so that it can be accessed outside if
cy.get('@checkSkipButton')
  .then((checkSkipButton) => {
    if (checkSkipButton) {
      cy.log('NOT CLICKABLE')
      button = true //instead of cy.then directly update the value of button
    } else {
      cy.log('CLICKABLE')
      button = false
    }
  })
  .then(() => {
    cy.log('Value is: ' + button) //prints the button value
    cy.wrap(button).should('equal', true) //Asserts the button value
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文