【问题标题】:variable is not defined in cypress赛普拉斯中未定义变量
【发布时间】:2022-07-22 18:27:21
【问题描述】:

我正在使用此代码进行随机选择。但在这段代码的最后,我想保存变量以在测试用例中进一步使用。我这样做了,但我得到了错误

cy.get('div.cdk-virtual-scroll-content-wrapper nz-option-item') // we get the select/option by finding the select by class
    .then(listing => {        
      const randomNumber = getRandomInt(0, listing.length-1); //generate a rendom number between 0 and length-1. In this case 0,1,2
      cy.get('div.cdk-virtual-scroll-content-wrapper nz-option-item').eq(randomNumber).then(($select) => {              //choose an option randomly
        const text = $select.text()       //get the option's text. For ex. "A"
        cy.get('div.cdk-virtual-scroll-content-wrapper').contains(text).click()       // select the option on UI
        let region = text;
        cy.wrap(region).as('region')
      });    
    })
    cy.log(region)

【问题讨论】:

  • 你不能在cy.wrap 之后立即调用cy.log - 不管怎样,将来cy.get 都会调用它。

标签: cypress


【解决方案1】:

你可以这样做:

cy.get('div.cdk-virtual-scroll-content-wrapper nz-option-item') // we get the select/option by finding the select by class
  .then((listing) => {
    const randomNumber = getRandomInt(0, listing.length - 1) //generate a rendom number between 0 and length-1. In this case 0,1,2
    cy.get('div.cdk-virtual-scroll-content-wrapper nz-option-item')
      .eq(randomNumber)
      .then(($select) => {
        //choose an option randomly
        const text = $select.text() //get the option's text. For ex. "A"
        cy.get('div.cdk-virtual-scroll-content-wrapper').contains(text).click() // select the option on UI
        cy.wrap(text).as('region')
      })
  })

cy.get('@region').then((region) => {
  cy.log(region) //logs region
})

【讨论】:

    猜你喜欢
    • 2022-11-07
    • 2020-03-17
    • 2023-01-16
    • 2022-11-17
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多