RobotFramework - get current Date

Viewed 24082

I need to test that the current date is displayed on my device, The date on the device is in format Monday, September 9, 2018 but when i try to test it i can only use the format Monday, 09, 2018 which would fail the test as there is no 0 in the Month date.

    page should contain element  ${DATE}
    ${DATE_MESSAGE}  get current date  result_format=%A, %d, %Y
    element should contain text  ${DATE}  ${DATE_MESSAGE}

How can change the format in robotframework to verify THis format Monday, September 9, 2018.

ERROR should have contained text 'Monday, 09, 2018' but its text was 'Monday, September 9, 2018'.

3 Answers

This can be done much more easy

${date}=      Get Current Date      UTC      exclude_millis=yes
${convert}=      Convert Date      ${date}      result_format=%a %B %d %H:%M:%S UTC %Y
Log      ${convert}      console=yes

(%a = f.e. Mon
%A = f.e. Monday
%B = f.e. May
%H:%M:%S - f.e. 13:04:09
UTC = f.e. part of string needed
%Y = 2019)

The output on the console of the string above will give:

Fri May 10 10:12:31 UTC 2019

If you want to add certain amount of days use:

${date}=      Get Current Date      UTC      exclude_millis=yes
${plus14}=      Add Time To Date      ${date}      14 days
${future}      Convert Date      ${plus14}      result_format=%a %B %d %H:%M:%S UTC %Y
Log      ${future}      console=yes

The output will be:

Fri May 24 10:12:31 UTC 2019

Something like that should work:

${now}    Evaluate    '{dt.day}/{dt.month}/{dt.year}'.format(dt=datetime.datetime.now())    modules=datetime

After update:

${now}    Evaluate  '{dt:%A}, {dt:%B} {dt.day}, {dt.year}'.format(dt=datetime.datetime.now())    modules=datetime

Robot Framework has an extensive DateTime library built-in (since version 2.8.5).

If you are able to import that library, documented here ( http://robotframework.org/robotframework/latest/libraries/DateTime.html ). You should be able to use the Get Current Date keyword.

This keyword has a parameter that allows you to use Python's date formation format to get the date/time format you need.

Related