RobotFramework - get current Date

天大地大妈咪最大 提交于 2019-12-08 01:57:25

问题


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'.


回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/52254674/robotframework-get-current-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!