Skip to content
Advertisement

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.

Advertisement

Answer

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;
  });
};
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement