6

I need to check the data returned is sorted by date. This is how I'm writing it:

it('should be sorted by date', function() {
    element.all(by.repeater('users in group.users')).then(
        function(users) {
            var lastUser = users[0].element(by.id('birth-date')).getText();
            for (var i = 1; i < users.length; ++i) {
                var currentUser = users[i].element(by.id('birth-date')).getText();
                expect(moment(currentApplication).format('MMM d, YYYY HH:mm')).toBeGreaterThan(moment(lastApplication).format('MMM d, YYYY HH:mm'));
                lastUser = currentUser;
            }
        }
    )
})

This returns:

Expected 'Jan 1, 2015 00:00' to be greater than 'Jan 1, 2015 00:00'.

What am I doing wrong? currentUser and lastUser seem to be objects instead of text...but I'm not sure why.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Jason
  • 1,787
  • 4
  • 29
  • 46

3 Answers3

8

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);
});
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I'm getting this: TypeError: Object [object Object] has no method 'all' – Jason Jan 05 '15 at 15:39
  • @Jason oops, typo, fixed :) – alecxe Jan 05 '15 at 15:42
  • Thanks. I'm new, so syntax is important for me :) One more, getting this now: TypeError: Object [object Object] has no method 'sort' – Jason Jan 05 '15 at 15:44
  • Also, is there a reverse sort? – Jason Jan 05 '15 at 15:44
  • @Jason yup, `reverse()`. Could you print out the value of `birthDates` before the `expect` line? thanks. – alecxe Jan 05 '15 at 15:46
  • Using the selenium server at http://localhost:4444/wd/hub [launcher] Running 1 instances of WebDriver { then: [Function: then], cancel: [Function: cancel], isPending: [Function: isPending] } F. – Jason Jan 05 '15 at 15:47
  • Almost, but not quite. No more errors, but .reverse() returns no failures, AND .sort() also returns no failures. One of these should fail :) – Jason Jan 05 '15 at 15:51
  • @Jason what is the value of `birthDates` before the `expect()` call? – alecxe Jan 05 '15 at 15:53
  • I think this is my bug. It's all showing 'Invalid Date'. I think my format is odd. I'm using 'Sep 30, 2014 10:45' – Jason Jan 05 '15 at 15:54
  • 1
    OMG! I tried to sort my array by standard protractors methods and it was sooooooo awful! Thank you very much.=) – Лилия Сапурина Jul 24 '15 at 11:12
2

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.

0
TS:
1) strArr = (elems) => elems.map(async elem => await elem.getText());
2) strToNum = (strArr) => strArr.map(Date.parse);
3) const ordering = {
      isASC: (data: number[]) => data.every((item, i, arr) => !i || arr[i-1] <= item),
      isDESC: (data: number[]) => data.every((item, i, arr) => !i || arr[i-1] >= item)
    }
OR
const ordering = {
      isASC: (data: number[]) => data.slice(1).every((item, i) => data[i] <= item),
      isDESC: (data: number[]) => data.slice(1).every((item, i) => data[i] >= item)
}
Anatoliy_Z
  • 129
  • 1
  • 4