柏树在每个内部获取元素

发布于 2025-02-12 18:13:52 字数 841 浏览 0 评论 0原文

我有此代码

        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)
            })

        })

我需要此行才能从特定 list 中返回。现在,它返回所有列表中的所有卡。

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

我尝试使用文字,但它返回[对象对象]

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

I have this code

        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)
            })

        })

I need this line to return only cards from the specific list. Right now it returns all cards from all lists.

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

I tried playing with literals, but it return [object object]

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

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

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

发布评论

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

评论(2

痕至 2025-02-19 18:13:52

您之所以从所有列表中获取所有卡的原因是由于 cy.get() 从root 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)
})

The reason you have been getting all the cards from all list is due to the cy.get() searching from the root DOM element. In order to limit your search to a previous DOM element you'll want to use either .within() or .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)
})
雅心素梦 2025-02-19 18:13:52

因此,第一个每个在列表上循环,我假设在列表中您要访问卡片,因此您可以做这样的事情:

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)
      })
  })
})

So the first each, is looping over the list and I am assuming that within the list you want to access the cards, So you can do something like this:

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)
      })
  })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文