最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Protractor: Checking data is sorted by date - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jun 24, 2016 at 15:22 alecxe 474k127 gold badges1.1k silver badges1.2k bronze badges asked Jan 5, 2015 at 14:53 JasonJason 1,8174 gold badges32 silver badges47 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

Get the list of all birth dates using map(), convert the list of strings to the list of dates and pare 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 pareNumbers(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.

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)
}
发布评论

评论列表(0)

  1. 暂无评论