0

I am currently writing an automation test where I firstly call a method to retrieve a date value from a database and then call a method to drive Selenium to obtain a date value from the UI and then assertEquals them to ensure they match. I actually get these date values concatenated with some other values into one string value and assert that which adds another thing to bear in mind.

In my database method the date is pulled out in its raw form which look like 2018-06-30 00:00:00.000 and this is a Date data type.

So I then convert this Date into string and reformat is as follows:

        SimpleDateFormat formatter = new SimpleDateFormat("d MMMM yyyy");
        String strPolicyExpiry = formatter.format(strPolicyExpiryUnformatted);

Which then makes the date look like 30 June 2018.

But my UI element pulls back a string of 30th June 2018 which in turn fails the assertEquals as the UI has the 'th' there'

I've tried removing the 'st, nd, rd, th' from the UI string with regex but this doesnt look easy. So now I'm trying to change the database side to add in 'st, nd, rd, th' but again it all seems a bit messy and long winded.

I'm hoping there is an easy way around this as it must be a common problem for automation testers. Any ideas?

Matt
  • 501
  • 1
  • 12
  • 26
  • 1
    `dateStr.replace("th", "");` or see https://stackoverflow.com/a/1950004/2310289 – Scary Wombat Jun 19 '18 at 01:35
  • Take a look at: https://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-ordinal – Thiago Procaci Jun 19 '18 at 01:38
  • What in the UI is formatting it as `30th`? Is that some library? Why not use the same library to format it for the comparison? Is it a JS library on the client side? – David Conrad Jun 19 '18 at 01:51
  • @ScaryWombat Thanks for the reply. But a straight string replace wont work will it. Example being that `dateStr.replace("st", "");` will make "August" become "Augu". – Matt Jun 19 '18 at 01:52
  • Thanks @ThiagoProcaci. I did see that post. I haven't tried it yet but I'm sure it will work. But this was an example of it looking quite a long winded approach. Happy to go this way but was curious if there was an easier way it which doesn't look like there is. – Matt Jun 19 '18 at 01:54
  • Play around with regex101.com - something like `\d+(st|th)` will work – Scary Wombat Jun 19 '18 at 01:56
  • @DavidConrad Client side is in .net but there must be a similar library in java. I'll speak to the Dev. – Matt Jun 19 '18 at 01:56
  • 1
    Hack: `dateStr.replaceAll("^([0-9]+)(?:st|nd|rd|th) ", "$1 ")` Also, `SimpleDateFormat` should be swapped for `DateTimeFormatter` wherever possible. – David Conrad Jun 19 '18 at 01:58
  • you could use this method here https://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-ordinal – Simon N Jun 19 '18 at 04:47

0 Answers0