Search code examples
cypress

Im trying to iterate a table and extract a value if it has found a value next to it in the table with cypress


I'm trying to iterate a table and extract its value into a variable and use it in another field

The elements are as below

  <tbody>
   <tr class>
     <td class="text-left">Transaction Type </td>
     <td class="text-left">0 </td>
   </tr>
   <tr class>
     <td class="text-left">STAN </td>
     <td class="text-left">009466 </td>
   </tr>

I want to extract the numeric value when it finds the STAN value, and am using this code

  cy.get('tbody tr').each(($el) => {
    cy.wrap($el).within(() => {
      cy.get('td').eq(1).should('contain.text', 'STAN') // contains() doesn't work
    })
  })

I have used the following script but it is failing and i know its not finished. Please could someone help me find the STAN and extract its value to a variable.


Solution

  • So after some work this is the answer that works:

    cy.get('table tbody tr td').each(ele => {
     if(ele.text()==="STAN"){
       let value  = ele.next().text();
       cy.get('.text-primary > .q-breadcrumbs__el').click();
       cy.wait(2000);
       cy.get('div[class="q-pa-md"]').find("input:eq(5)").type(value);
       }
    });