fasswas
fasswas

Reputation: 1

Cypress, read out ALL elements of table but only one exact value of multiple <div>-Tags

I want to read out ALL elements of a table, but only one value of multiple -Tags. I achieved this by using following Code:

HTML-Snipet:

<td data-cy="project.name" aria-label="project.name: ASBbwHwB0UC4Ud30CJw9">
    <div>
        <b>00_AA_Testprojekt_1635314756540_BEARBEITET</b>
    </div>
    <div>
        <span class="ng-star-inserted">Von 13.01.2024</span>
        <!---->
        &nbsp;
        <span class="ng-star-inserted"> Bis 02.11.2028</span>
        <!---->
    </div>
</td>

Cypress-Code:

cy.get('[data-cy="projectList"]')
     .get('[data-cy="project.name"]')  // I'm using my own marker for my table
    .parent('tr')
    .within(() => {
        cy.get('div').eq(0).then( (tmp) => { cy.log('div 1 = ' + tmp.text())})
        // cy.get('div').eq(1).then( (tmp) => { cy.log('div 2 = ' + tmp.text())})
        // cy.get('div').eq(2).then( (tmp) => { cy.log('div 3 = ' + tmp.text())})
    })

But now the problem is, that only one (the first) element/ row of the Table is read out. How can I catch ALL rows of the table? I tried using .each(elemt, index, content), but it doesn't work as it should.

Upvotes: 0

Views: 723

Answers (1)

Alapan Das
Alapan Das

Reputation: 18624

You can do something like this:

cy.get('[data-cy="projectList"]')
  .find('[data-cy="project.name"]')
  .each(($ele) => {
    cy.wrap($ele)
      .parent('tr')
      .within(() => {
        cy.get('div')
          .eq(0)
          .then((tmp) => {
            cy.log('div 1 = ' + tmp.text())
          })
      })
  })

Upvotes: 1

Related Questions