Stathis Trigas
Stathis Trigas

Reputation: 1

Dynamically set a variable in variables section of robot framework

I want to set the current date as a variable in variables section. The problem is that in variables section I cannot call any robot keywords. Is their a way to do this using python?

for example:

my .robot looks like this:

Variables***
${current_date}    2021-9-2

and I would like to set the date dynamically with something like this:

Variables***
${current_date}    date.today()

Upvotes: 0

Views: 1869

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386342

As of robot framework 3.2 you can use inline python evaluation, which lets you put any python code inside ${{ and }}

Example:

*** Variables ***
${current_date}  ${{ datetime.datetime.today().strftime('%Y-%m-%d') }}

Upvotes: 2

Jiri Janous
Jiri Janous

Reputation: 1242

If you add variable in your testcase it is just a string. Setting variable dynamically can be done with variable file. This example is taken from the documentation. All you have to do, is crate variable file and then add this file to your testcase.

import os
import random
import time

USER = os.getlogin()                # current login name
RANDOM_INT = random.randint(0, 10)  # random integer in range [0,10]
CURRENT_TIME = time.asctime()       # timestamp like 'Thu Apr  6 12:45:21 2006'
if time.localtime()[3] > 12:
    AFTERNOON = True
else:
    AFTERNOON = False

Upvotes: 1

Related Questions