0

I am trying to figure out how to verify the sorting of time and/or a date using java and selenium. Here is my situation... So if I receive an email today, my Inbox displays the time the message was received. However if I received an email yesterday, the Inbox would display yesterday's date. I am trying to verify that in descending order, the emails with a time would appear first, followed by emails with dates.

Here is my code which works if there is only dates. Can someone please help me with figuring out how to verify the time as well?

  protected void validateDateDescendingOrder(String className) {
    // create a list
    List<WebElement> messageElements = driver.findElements(By.cssSelector("div.scrollable-panel div." + className));
    List<String> messageList = new ArrayList<String>();
    for (WebElement element : messageElements) {
      messageList.add(element.getText());
    }

    // create a new list and sort
    List<String> sortedmessageList = new ArrayList<String>();
    sortedmessageList.addAll(messageList);
    Collections.sort(sortedmessageList, new Comparator<String>() {
      DateFormat f = new SimpleDateFormat("MM/dd/yy");

      @Override
      public int compare(String o1, String o2) {
        try {
          return f.parse(o2).compareTo(f.parse(o1));
        } catch (ParseException e) {
          throw new IllegalArgumentException(e);
        }
      }
    });

    // compare the original list order with the sorted list to make sure they match
    assertEquals(sortedmessageList, messageList);
  }
SiKing
  • 10,003
  • 10
  • 39
  • 90
kc29
  • 31
  • 2
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 08 '18 at 21:24

3 Answers3

1

hope this will help to you using java 8

List<String> date = Arrays.asList("2015-11-09", "2015-11-11", "2015-11-08", "2015-11-08");
List<LocalDate> convetedSortedDate = date.stream().map(LocalDate::parse).sorted().collect(Collectors.toList());

//or

 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy '@'hh:mm a");
    Collections.sort(datestring, (s1, s2) -> LocalDateTime.parse(s1, formatter).
            compareTo(LocalDateTime.parse(s2, formatter)));
Faiz Akram
  • 559
  • 4
  • 10
0
  1. Change DateFormat f = new SimpleDateFormat("MM/dd/yy"); format to also include Time
  2. If an exception is thrown in the Parse section, do concatenation between Date.Now and the Time; then do the Parse again. (Maybe the date in this case is 0/0/0 or something and you need to identify if this the case)
  3. Do the sort
0

All you are missing is the parser. There is a very useful library - Apache-lang - that has many interesting helper methods.

In my case, I have am also comparing dates, but the date strings have several different formats. Using the above library, I can do something like this:

import org.apache.commons.lang3.time.DateUtils;

private Date magicParseDate(final String date) {
try {
    return DateUtils.parseDate(date, "yyyyMMdd-S", "yyyy-MM-dd HH:mm:ss", "MM/dd/yyyy HH:mm:ss z");
} catch (ParseException ignore) {
    // empty values
    return new Date(0);
}
}

Then in your compare function just call magicParseDate().

SiKing
  • 10,003
  • 10
  • 39
  • 90
  • `magicParse` is not a very descriptive name for what that method (or probably any method) does... ;) – JeffC Aug 08 '18 at 19:05