I have a protractor test case where I have to compare the data in a HTML table on the UI with the cucumber datatable. Since the cucumber datatable is a 2D array, I thought I would read the ui cell values into a 2D array and just use the chai assertions to assert it against cucumber datatable. Below is a function that another stack overflow helper helped me to write. Problem is when I print the array out, I see that the elements are pushed in random order and not the order in which they appear in the DOM; therefore it doesn't match the cucumber data able when asserted. Is there a way I can push elements in the order in which they appear in the DOM of the page? Could somone please post some ideas?
getCellValues(): Promise<string[][]> {
return new Promise<string[][]>(function (resolve, reject) {
let allValues = [];
let table = avgtable; //avgtable is the css locator for the tbody
// take all 'tr' child elements and iterate them using 'each()'
table.all(by.css('tr')).each((el)=> {
let subArr = [];
// take a row and process each element
el.all(by.css('td')).each((subEl)=> {
//console.log("subEl text: " + subEl.toString());
// get text of one row element
subEl.getText().then((text) => {
// at the point we receive the text add it to sub array.
subArr.push(text);
//console.log("subarr text: " + subArr.toString());
});
}).then(function () {
allValues.push(subArr);
//console.log("arr text: " + allValues.toString());
});
}).then(function() {
resolve(allValues);
});
});
}
Here is the calling step definition
Then(/^the bricklet data should match the following data$/, function(table) {
return funddetails.getCellValues().then((values: string[][]) => {
return browser.sleep(5000).then(()=> {
return expect(values).to.equal(table.raw());
});
});
}) ;
Some references: 1) Iterate through a HTML table to get Cells using a Typescript in protractor-typescript framework 2) Iterate through HTML rows and columns of a table using webdriver using javascript(Protractor non-angular)