SelfTechy

A portal for self learning

Category Archives: Robotframework

Robotframework – Working with Date and Time


Robot framework provides keywords for lot of stuffs. Framework has very good documentation. Here I am going to explain how do we handle situations with date and time. The library in Robot framework for handling time is “DateTime”. This library needs to be added to the settings before using the keywords for date and time. In automation we come across problems to get or set date in various formats. Sometimes we need to add or subtract some number of days or hours or minutes to some given date. All these are well handled with different keywords provided by this library.

Keywords:

  1. Add Time To Date – Adds given time to the Date.
  2. Add Time To Time – Adds time to given time and returns result time. Both the arguments are time
  3. Convert Date  – Converts the date between supported date formats
  4. Convert Time – Converts the time between supported time formats
  5. Get Current Date – Returns the current date. We can specify the format with result_format argument
  6. Subtract Date From Date – Subtracts date from the date and returns the resultant date.
  7. Subtract Time From Date – Subtracts the time from the date and returns the resultant date.

Printing Day, Month, Year, Hour, Minute, and Seconds using Get Current Date and Convert Date keywords

*** Settings ***
Library  DateTime


*** Test Cases ***
Manipulate Current Time test
    Manipulate current time
    
*** Keywords ***
Manipulate current time
    ${CurrentDate}=  Get Current Date  result_format=%Y-%m-%d %H:%M:%S.%f

    ${datetime} =	Convert Date  ${CurrentDate}  datetime
    Log  ${datetime.year}	
    Log	 ${datetime.month}
    Log	 ${datetime.day}	
    Log	 ${datetime.hour}
    Log	 ${datetime.minute}
    Log	 ${datetime.second}
    Log	 ${datetime.microsecond}
    

In automated testing we might need to generate date and time values dynamically. For example, add one hour for the current time and generate the new time or add 30 minutes to the current time and get the new time. This can be accomplished using Add Time To Date keyword. Look at the below example:

*** Settings ***
Library  DateTime


*** Test Cases ***
Manipulate Current Time test
    Manipulate current time
    
*** Keywords ***
Manipulate current time
    ${CurrentDate}=  Get Current Date  result_format=%Y-%m-%d %H:%M:%S.%f

    ${newdatetime} =  Add Time To Date  ${CurrentDate}  2 days
    Log  ${newdatetime}
    ${newdatetime} =  Add Time To Date  ${CurrentDate}  2 hours
    Log  ${newdatetime}
    ${newdatetime} =  Add Time To Date  ${CurrentDate}  30 minutes
    Log  ${newdatetime}
    ${newdatetime} =  Add Time To Date  ${CurrentDate}  30 seconds
    Log  ${newdatetime}

Report:

DateAndTimeReport

There is one more way to add time to the current date. Using result_format=%H, get the current hour of the date. Convert that to integer and add required number of minutes to it. But in this way of getting new time there is a glitch. If you add 10 minutes to 55 then this gives us the result as 65 not as 05. We will go through the below code and try to understand.

*** Settings ***
Library  DateTime


*** Test Cases ***
Manipulate Current Time test
    Manipulate current time
    
*** Keywords ***
Manipulate current time
	 ${hour}=   Get Current Date   result_format=%H
	 ${hour}=   Convert To Integer   ${hour}
	 ${min}=   Get Current Date   result_format=%M
	 ${min}=   Convert To Integer   ${min}
	 ${minmodified}=   Evaluate   ${min}+55
	 ${hourmodified}=   Evaluate   ${hour}+2
	 Log  ${hourmodified}
     Log  ${minmodified}

Report:

WrongMinUpdateIf we use the above method then we need to handle the special cases such as hour going beyond 24 and minutes going beyond 60. Hence, I would prefer to use the first one than the second one. I would say using keywords for subtracting time from the date and time are not tough after understanding how to use the keywords for adding the date and time to the given date. Try to work on keywords and comment on this post.