Add my own VariableResolver

Hello!
Is there any possibility to add my own variables resolver? Right now I do that using internal api, but it’s not a good way I think. I would like to have my own resolver to do something like that:
Run Some Keyword ${my_prefix_and_argument}

I want to have my callback in python library to resolve all variables with prefix “my_prefix_”. Such callback does some calculating (may be read any file, calculate hash, etc.) using postfix “and_argument” and returns any result. Could anybody help me? ))

Hi Sergey,

I’m not sure what you are trying to achieve, but the simple answer is if you can write python you can get RF to do just about anything:

http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#keyword-names

Create a python keyword library that has a function like this:

def resolve_cust_variable(varname):
    # do something to resolve the variable
    return var_result

then in robot framework call your keyword like this:

my test case
    ${myvar}=    Resolve Cust Variable    my_prefix_and_argument
    Run Some Keyword    ${myvar}

Refer the link above to the documentation for more details on how this works.

Hope that helps,

Dave.

Hi, I thought about that. But I would like to have lesser code in tests.

Hi Sergey,

I’m not sure what you mean by having less code in your tests? With this there is no code in the test just in the custom keyword.

If you mean you want to avoid including python files? there is another option and that is use the Evaluate function, but this will only be useful if you can resolve your custom variable with a single line of python, here is an example I gave where a single line of python calls to native python functions, but in my view that would mean having more code in your tests, I think the custom keyword is cleaner and more readable.

Beyond that you should refer to Extending Robot Framework in the user guide if you want to build a custom VariableResolver library, I’m not sure what you’ll need to do to make that work, i’m guessing a custom listener to catch variables in your format and a library that processes the variable and return the value, but this is a lot of coding to solve a problem that could just as easily be solved with a simple python keyword.

I don’t know of any other options, so I hope one of these helps you,

Dave.

Hi Dave,
Yes, I think to create a listener would be a good solution, but there is not any callback to catch resolving variables.

In some of my suites, I have found this trick useful:
During Suite setup, I do this:
${date} Evaluate lambda f="%Y-%m-%d", **r:(datetime.date.today() + datetime.timedelta(**r)).strftime(f) modules=datetime
Set suite variable ${date}

Then, when I need a date, I can do this:
Input text //input[contains(@id, “${field}”)] ${date("%m/%d/%Y", days=-3)}

1 Like