Protractor: Checking data is sorted by date
Get the list of all birth dates using map()
, convert the list of strings to the list of dates and compare with a sorted version of the same array:
element.all(by.id('birth-date')).map(function (elm) {
return elm.getText().then(function (text) {
return new Date(text);
});
}).then(function (birthDates) {
// get a copy of the array and sort it by date (reversed)
var sortedBirthDates = birthDates.slice();
sortedBirthDates = sortedBirthDates.sort(function(date1, date2) {
return date2.getTime() - date1.getTime()
});
expect(birthDates).toEqual(sortedBirthDates);
});
I maked a small research of this topic. My code is:
element.all(by.id('birth-date')).map(function (elm) {
return elm.getText();
}).then(function (birthDates) {
var sortedbirthDates = birthDates.slice();
sortedbirthDates = sortedbirthDates.sort(function compareNumbers(a, b) {
if (a > b) {
return 1;
} });
expect(birthDates).toEqual(sortedbirthDates);
});
Here we independent of value type. We can use it for number, string and date.