Reputation: 72
I am using protractor for my E2E testcases. In my case, I am having an array and I want to sort it
Ex.
Arraylist.push(elements[1].getText());
Arraylist.push(elements[2].getText());
Arraylist.push(elements[0].getText());
element[n]
is returning a text value which is getting inserted into the ArrayList.
Now I want to Sort this Array and test that the elements are in the correct order.
Do someone has idea, How can we do that?
Upvotes: 1
Views: 3083
Reputation: 27012
To do this, you should be aware that all the elements and return values of getText()
are promises. Therefore sorting the array, and comparing the sorted with the unsorted, has to be done with the resolved results of these promises.
var unsorted = elements.map(function(element) {
return element.getText();
});
var sorted = unsorted.then(function(texts) {
// Make sure to copy the array to not sort the original
return texts.slice(0).sort();
});
var equals = protractor.promise.all([unsorted, sorted]).then(function(texts) {
var unsorted = texts[0];
var sorted = texts[1];
var allEqual = (unsorted.length === sorted.length);
sorted.forEach(function(sortedItem, i) {
allEqual = allEqual && (sortedItem === unsorted[i]);
});
return allEqual;
});
expect(equals).toBe(true);
Upvotes: 2
Reputation: 4351
not sure what the connection to E2E testing or protractor is, but well ... Arraylist.sort() or use lodash/underscorejs _.sortBy(Arraylist, sortingValue)
Upvotes: 0