I am using cypress and I am handling a web table.and I have extracted the values out of it and converted it into an array but what has happend is the spacing is inconsistent. Now I want to have values with consistent spaces eg: Actual Output :
["1234random9-Dec-2022", "15:16:51", "User66","privateNot", "Scheduled","" ]
Expected Output:
[ "1234" // id// ,"random"//file_name//,
"9-Dec-2022"//date//,"15:16:51"//time//,"User66"//username//, "private"//visiblity//, "NotScheduled" // Type of job]
Code:
describe('Webtable test', () => {
it('Whole table data', function() {
cy
.get('#query-table tbody tr')
.should('have.length',10);
cy.get('#query-table tbody tr:eq(1) td').should('have.length',7);
cy.get('#query-table tbody tr').each(($el, index, $list) =>{
cy.wrap($el).find('td').each(($el2, index2, $list2)=>{
const list=$list2.text().replace(/[\s,] /g, ' ')
cy.log("List",list.split(" "))
const text=list.split(" ")
cy.log(text)
})
})
});
})
;
Please help me
CodePudding user response:
From the above comments ...
@AvdhutJoshi ... "I have extracted the values out of it and converted it into an array" ... One should have a look into how the OP exactly extracts the data. Maybe already there is the source of failure?"
The OP already iterates the
td
collection. Why does the OP then not just programmatically build the array of cell/column values by extracting each from the current td-element ... here$el2
... but instead tries to achieve it by what seems to be a splitting and replacing approach of the entire row's text content which of cause will fail.
The next provided example code takes the OP's attempt and changes it according to the suggestions of the second quoted comment. It relies on the validity of cellItem.text()
for I only could guess from the OP's code whether to extract text content like that.
describe('Webtable test', () => {
it('Whole table data', () => {
cy
.get('#query-table tbody tr')
.should('have.length', 10);
cy
.get('#query-table tbody tr:eq(1) td')
.should('have.length', 7);
cy
.get('#query-table tbody tr')
.each(rowItem => {
const cellContentList = [];
cy
.wrap(rowItem)
.find('td')
.each(cellItem =>
cellContentList.push(
cellItem.text().trim()
)
);
console.log({ cellContentList });
});
});
});