Sayantan Ghosh
Sayantan Ghosh

Reputation: 338

How to get all data from a row as an object in cypress?

So, my web page has a table structure with multiple rows. I want to create a function which gets all the values from a row and creates an object with the header as the keys and the values. The kind of output I want:

header1 : value1
header2 : value2

This is what I have tried:

export const getRowObject = (rowIndex) => {
  return cy.get(`[role='cell'][data-rowindex='${rowIndex}']`).then((values) => {
    let rowObject;
    values.map((i, elem) => {
      if (!rowObject) {
        rowObject = {};
      }
      rowObject[headers[i]] = Cypress.$(elem).text();
    });
  });
};

This is returning me an object with the index as key and the HTMLdivElements as the values. enter image description here

Any help regarding this would be highly appreciated.

Upvotes: 1

Views: 1445

Answers (1)

user12697177
user12697177

Reputation:

You are 90% there, just add an inner return

export const getRowObject = (rowIndex) => {
  return cy.get(`[role='cell'][data-rowindex='${rowIndex}']`).then((values) => {
    let rowObject = {};
    values.map((i, elem) => {
      rowObject[headers[i]] = Cypress.$(elem).text();
    });
    return rowObject;
  });
};

Upvotes: 1

Related Questions