簡體   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