0

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()

2 Answers 2

2

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') }}
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

The variables will be evaluated only once, on importing the file, and not changed afterwards. E.g. they are dynamic in their initialization, but not afterwards.
@TodorMinakov I think the OP wants dynamic initialization though.
Well it's ambiguous :) Your answer is good, I left the comment mainly as a hint for someone else coming to the question and expecting the variable to be fully dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.