LeonH
LeonH

Reputation: 1037

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

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.

Upvotes: 4

Views: 35361

Answers (3)

DDay
DDay

Reputation: 708

You can also format the time as you like. See this answer. It offers format suggestions. You can use Python DateTime functions as in

   ${now}    Evaluate    '{dt.day}/{dt.month}/{dt.year}'.format(dt=datetime.datetime.now())    modules=datetime
   Log   ${now}
   Log to Console   now time 1: ${now}
   ${now}    Evaluate  '{dt:%A}, {dt:%B} {dt.day}, {dt.year}'.format(dt=datetime.datetime.now())    modules=datetime
   Log   ${now}
   Log to Console   now time 2: ${now}

Upvotes: 0

Yakov Gusev
Yakov Gusev

Reputation: 9

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.

Upvotes: 1

Marcin Kowalczyk
Marcin Kowalczyk

Reputation: 649

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.

Upvotes: 8

Related Questions