0

The goal is to iterate over a table and create a test dynamically for each row to validate its status. Table example: Table, 1rst column GUID labels, 3rd column status

Aware of Cypress Examples: Dynamic tests, however, provided examples addresses iterating over a static list of base types (string, number) and not gathered children of JQuery<HTMLElement>.

Rows are grouped by the first column labeled with GUID labels. The code below collects rows label contains a specific GUID. The added pseudo injection of test it() does not work nor do I expect it would, but this is what I am trying to accomplish testing each row's status with specific GUID:

it('TEST Iterate rows matching GUID', () => {
  cy.fixture('esign').then($esignStore => {
   
   expect($esignStore).to.have.property('requestUUID').to.be.a('string').not.empty;
    cy.get('table[data-qa="act_ops_log_table"]').within(() => {
      cy.log('Ops Logs table found');
      cy.get('tbody[data-qa="act_ops_log_table_body"]').then(() => {
        cy.get('tr');
        cy.get('td');
        cy.get('td:nth-child(1)').each(($tableCell, $index, $list) => {
          const textEnvelope = $tableCell.text();
          if (textEnvelope.includes($esignStore.requestUUID)) {

            // pseudo-example of dynamically added test: start
            // it(`TEST ${$index}`, () => {
              cy.get('td:nth-child(3)')
                .eq($index)
                .then($field => {
                  const textStatus = $field.text();
                  expect(textStatus, 'succeeded');
                });
            // });
            // pseudo-example of dynamically added test: end

          }
        });
      });
    });
  });
});

Approaches appreciated. Thank you

1 Answer 1

1

You cannot create tests dynamically.

Cypress parses the spec file to find out what tests are present before the Cypress commands even start to run.

The best you can do is add extra logging to mark each tested row.

cy.get('tbody tr', {log:false}).each(($tr, index) => {

  // Log beginning of row test
  cy.then(() => Cypress.log({
    displayName: 'Row test',
    message: `TEST ${index}`,
  }))

  cy.wrap($tr, {log:false}).find('td:nth-child(1)', {log:false}).then($firstCol => {
    const textEnvelope = $firstCol.text();
    if (textEnvelope.includes(esignStore.requestUUID)) {
      cy.wrap($firstCol, {log:false}).siblings({log:false}).eq(1, {log:false})
        .should('have.text', 'succeed')
    } else {
      cy.log(`Row ${index} - not required requestUUID`)
    }
  })
})

enter image description here


Or pre-filtering the required rows so that only those matching requestUUID are tested

cy.get('tbody tr', {log:false}).each(($tr, index) => {
  .filter(`:has(td:nth-child(1):contains(${esignStore.requestUUID}))`, {log:false})
  .each(($tr, index) => {

    // Log beginning of row test
    cy.then(() => Cypress.log({
      displayName: 'Row test',
      message: `TEST ${index}`,
    }))

    cy.wrap($tr, {log:false})
      .find('td:nth-child(3)', {log:false})
      .should('have.text', 'succeed')
  })

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Appreciate your response.
My changes for this problem are too long to be within a comment. Created a new question. Thank you: stackoverflow.com/questions/69095677/…
No problem, but the other way doesn't work either - no Cypress commands run until the number of tests have been established.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.