Date change with if/else statement


#1

Hi champs,

I am wondering if it is possible to select a today’s date only if the current one is from the past, i.e. kantu should select today’s date if the one provided is not equal to today’s date. Below are few screenshots to clarify the idea.

image
Here everything is fine, the date in the field is equal to the one on the right, kantu should leave it as it is.

image
Here everything is correct, the date is in the future, therefore Kantu should also leave it as it is.

image
Here the date is not equal to today’s date and it isn’t the future date - it is a past date, so I would like to automate Kantu to click on the date which is highlighted in red which will trigger the date to change to today’s date.

Please advise whether it is possible and how to do so.

Thanks!


#2

This task is possible. It consists of 3 parts:

  1. Extract the date from the website. For this, you use storeText (extract date from html text) or storeValue (get date from input box). This is described in more detail in this get data from salesforce post.

  2. Compare the dates - this part is a bit tricky and requires storeEval and Javascript, see below.

  3. If it is a past date, click on the link. This should be easy to solve it, you only need to find the right locator for the link to click on.

So the open, tricky topic is number, compare the dates.

For dates in the US format mm/dd/yyyy the compare logic in Javascript is:
var dateString1 = "${d1}"; var dateParts1 = dateString1.split("/"); var dateObject1 = new Date(dateParts1[2], dateParts1[0] - 1, dateParts1[1]); var dateString2 = "${d2}"; var dateParts2 = dateString2.split("/"); var dateObject2 = new Date(dateParts2[2], dateParts2[0] - 1, dateParts2[1]); dateObject2 > dateObject1

For dates in the EU format dd/mm/yyyy the compare logic in Javascript is this (same as above, just day and month exchanged):
var dateString1 = "${d1}"; var dateParts1 = dateString1.split("/"); var dateObject1 = new Date(dateParts1[2], dateParts1[1] - 1, dateParts1[0]); var dateString2 = "${d2}"; var dateParts2 = dateString2.split("/"); var dateObject2 = new Date(dateParts2[2], dateParts2[1] - 1, dateParts2[0]); dateObject2 > dateObject1

What this snippet does is to split the string with / and put day, month and year in an array. Then recombine the string in the yyyy-mm-dd format that the Javascript date function needs. It does this twice, once for the first date ${d1} and then for the second date ${d2}, and then compare the dates with dateObject2 > dateObject1

Complete test macro for part 2 date comparison:

{
  "CreationDate": "2018-5-29",
  "Commands": [
    {
      "Command": "store",
      "Target": "05/25/2018",
      "Value": "d1"
    },
    {
      "Command": "store",
      "Target": "05/29/2018",
      "Value": "d2"
    },
    {
      "Command": "echo",
      "Target": "d1=${d1}",
      "Value": "blue"
    },
    {
      "Command": "echo",
      "Target": "d2=${d2}",
      "Value": "blue"
    },
    {
      "Command": "storeEval",
      "Target": "var dateString1 = \"${d1}\"; var dateParts1 = dateString1.split(\"/\"); var dateObject1 = new Date(dateParts1[2], dateParts1[0] - 1, dateParts1[1]); var dateString2 = \"${d2}\"; var dateParts2 = dateString2.split(\"/\"); var dateObject2 = new Date(dateParts2[2], dateParts2[0] - 1, dateParts2[1]); dateObject2  > dateObject1",
      "Value": "IsD2Later"
    },
    {
      "Command": "echo",
      "Target": "Is Date 2 Later => ${IsD2Later}",
      "Value": "green"
    },
    {
      "Command": "if",
      "Target": "${IsD2Later}",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "do something, D2 is later ",
      "Value": "orange"
    },
    {
      "Command": "else",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "D2 earlier, do something else",
      "Value": "red"
    },
    {
      "Command": "endif",
      "Target": "",
      "Value": ""
    }
  ]
}

Screenshot:


#3

Thank you!
Very clear and precise explanation, everything works as expected. You are the best mate.
Cheers