Robotframework: Getting date at runtime using Get Current Date, in specific format

28,800

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

robot.libraries.DateTime.

Share:
28,800

Related videos on Youtube

LeonH
Author by

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, 2022

Comments

  • LeonH
    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
    LeonH almost 10 years
    If 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
    Marcin Kowalczyk almost 10 years
    Yes, 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
    LeonH almost 10 years
    According 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
    Marcin Kowalczyk almost 10 years
    I don't think you need "DateTime." before your keyword. I'm adding RF script sample to my answer.
  • LeonH
    LeonH almost 10 years
    Thank 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
    LeonH almost 10 years
    I'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
    Marcin Kowalczyk almost 10 years
    I 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
    LeonH almost 10 years
    My 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
    Rupali almost 4 years
    I 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