2015-01-05 6 views
5

Ho bisogno di controllare che i dati restituiti siano ordinati per data. Questo è il modo che sto scrivendo:Goniometro: il controllo dei dati è ordinato per data

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; 
      } 
     } 
    ) 
}) 

Questo restituisce:

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

Che cosa sto facendo di sbagliato? currentUser e lastUser sembrano essere oggetti anziché testo ... ma non sono sicuro del perché.

risposta

7

Ottenere l'elenco di tutte le date di nascita utilizzando map(), convertire la lista di stringhe alla lista delle date e con una versione ordinata dello stesso 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); 
}); 
+0

Mi sto prendendo questo: TypeError: Object [oggetto Object] non ha alcun metodo 'all' – Jason

+0

@Jason oops, typo, fixed :) – alecxe

+0

Grazie. Sono nuovo, quindi la sintassi è importante per me :) Un altro, ottenendo questo ora: TypeError: Object [oggetto Object] non ha alcun metodo 'sort' – Jason

1

ho resa a una piccola ricerca di questo argomento . Il mio codice è:

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);               
    }); 

Qui siamo indipendenti dal tipo di valore. Possiamo usarlo per numero, stringa e data.

Problemi correlati