繁体   English   中英

赛普拉斯在每个内部获取元素

[英]Cypress get element inside each

我有这个代码

        cy.visit('http://localhost:3000/')
        cy.get('.list').each((list, index, lists) => {
            cy.contains('Learn').click()
            cy.url().should('include', '/localhost')

            cy.get(`[data-testid="card-input-${index}"]`)
                .type('Learn Cypress')
                .should('have.value', 'Learn Cypress')
            cy.get(`[data-testid="btnAdd-${index}"]`).click()


            cy.get(".card").each((card, index, cards) => {
                cy.get(cards).should('have.length', 4)
            })

        })

我需要这一行来仅返回特定列表中卡片 现在它返回所有列表中的所有卡片。

 cy.get(cards).should('have.length', 4)

我试着玩文字,但它返回 [object object]

 cy.get(`${cards}>card`).should('have.length', 4)

所以第一个each循环遍历列表,我假设你想在列表中访问卡片,所以你可以这样做:

cy.visit('http://localhost:3000/')
cy.get('.list').each((list, index, lists) => {
  cy.contains('Learn').click()
  cy.url().should('include', '/localhost')

  cy.get(`[data-testid="card-input-${index}"]`)
    .type('Learn Cypress')
    .should('have.value', 'Learn Cypress')
  cy.get(`[data-testid="btnAdd-${index}"]`).click()

  cy.wrap(list).within(() => {
    cy.get('.card').should('have.length', 4)
    //or
    cy.get('.card').its('length').should('eq', 4)
    //or
    cy.get('.card')
      .its('length')
      .then((len) => {
        cy.get('.card').should('have.length', len)
      })
  })
})

您从所有列表中获取所有卡片的原因是cy.get()从根 DOM 元素进行搜索。 为了将您的搜索限制在之前的 DOM 元素中,您需要使用.within().find()

cy.visit('http://localhost:3000/')
cy.get('.list').each((list, index) => {

  cy.contains('Learn').click()
  cy.url().should('include', '/localhost')
  cy.get(`[data-testid="card-input-${index}"]`)
    .type('Learn Cypress')
    .should('have.value', 'Learn Cypress')
  cy.get(`[data-testid="btnAdd-${index}"]`).click()

  cy.wrap(list)
    .find('.card')
    .should('have.length', 4)
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM