Robotframework: Getting date at runtime using Get Current Date, in specific format
Solution 1
Does a keyword Get Current Date exist in standard RF lib? There is a builtin keyword called Get Time instead. Documentation explains how to format output. To use Get Current Date you need to import DateTime library first.
Update: RF script sample which works for me:
*** Settings ***
Library DateTime
*** Test Cases ***
datatimetest
${d}= get time
log {d}
${d}= Get Current Date result_format=%Y-%m-%d
log {d}
${d} = Add Time To Date 2014-05-28 12:05:03.111 7 days
log {d}
Please remember that DateTime is a new library so in case you have old version of Robot Framework, you need to either install library or upgrade RF.
Solution 2
I'm using RF with Python and by default my IDE detects Python DateTime library. Try to use the full path:
Library robot.libraries.DateTime
Related videos on Youtube
LeonH
I'm Leon, a Capgemini software consultant with a degree in Computer Science, specialising in SAP applications. I've also worked at IBM and a few smaller companies. I enjoy learning new technologies, solving problems creatively and have a keen interest in front-end web development.
Updated on July 09, 2022Comments
-
LeonH almost 2 years
I am using the Get Current Date keyword to return the date in the format year-month-day. I use this particular information to make sure that an account's created timestamp is correct in automated tests.
The issue is that the keyword is not recognised, my code should be correct (it should work and it should produce the date in the format I wish.
*** Keywords *** Initialize Test Data ${DATE}= Get Current Date result_format=timestamp ${MYNUM}= faker.Random Int Set Suite Variable ${MYNUM} Set Suite Variable ${DATE}
Why do I get the error
No keyword with name 'Get Current Date' found.
?Thanks in advance.
-
LeonH almost 10 yearsIf you look at the link for Get Current Date it does it exist, and I am using that specific version of RF.. Never the less, if I wanted to use
Get Time
how could I truncate the output so that I have date-month-day ? -
Marcin Kowalczyk almost 10 yearsYes, I realized that after I posted the answer. However it is not builtin keyword. You need to import DateTime library. I've updated my answer already.
-
LeonH almost 10 yearsAccording to the documentation the output of
${DATE}= DateTime.Get Current Date result_format=timestamp
should be (for today at least) 2014-07-23, correct? -
Marcin Kowalczyk almost 10 yearsI don't think you need "DateTime." before your keyword. I'm adding RF script sample to my answer.
-
LeonH almost 10 yearsThank you. I have DateTime. as a prefix because I am using the library with name "DateTime" for documentation/learning purposes when my collegues look at this.
-
LeonH almost 10 yearsI'm afraid that your test script doesn't give me the data in the format I want. Each of your three called keywords produce their result in the same format, I want my data to be returned in the format "year-month-day"
-
Marcin Kowalczyk almost 10 yearsI thought this question was about keyword not being recognised. Anyway, I've edited answer once again, now it contains result formating. It should finally work.
-
LeonH almost 10 yearsMy second paragraph says that what my code should produce (in my eyes) as well as that it should be working :p Never the less, I had just worked out that I could specify the actual format. I'll select your answer now, thank you!
-
Rupali almost 4 yearsI tried above mentioned answer, but it not showing me current date. thought it is not giving any error too. I installed DateTime library in settings