变量未在 cypress 中定义

问题描述 投票:0回答:2

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

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)
cypress alias
2个回答
3
投票

.then()
添加到您的日志中,并将
region
的声明移至顶部。

let region;

cy.get('div.cdk-virtual-scroll-content-wrapper nz-option-item')
    .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
        region = text;
        cy.wrap(region).as('region')
      });    
    })

cy.then(() => {
   cy.log(region)
})

1
投票

如果您想在同一测试中使用该区域的值(相同的

it
块),则可以执行此操作。

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.get('@region').then((region) => {
  cy.get('selector').type(region)
})

如果您使用文本值,在不同的测试(不同的

it
块)甚至不同的测试套件中,您可以这样做:

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
        Cypress.env('region', region)
      })
  })

cy.get('selector').type(Cypress.env('region'))
© www.soinside.com 2019 - 2024. All rights reserved.