Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is possible to create new variable in suite/test set up - Robot Framework?

Is it possible to initialize a variable in a suite or test setup based on the return value of a keyword? I've tried this sort of syntax and it didn't work:

*** Settings ***
| Suite Setup | ${A variable}= | Set Variable | A String

I know I can call keywords like "Set Suite Variable" but they don't allow me to set the variable to the result of another keyword. I used "Set Variable" in this example, but I want to be able to call any keyword here.

like image 868
Minh Nguyen-Phuong-Hoang Avatar asked Jan 06 '15 04:01

Minh Nguyen-Phuong-Hoang


2 Answers

Strictly speaking, no, it's not possible. Within a suite or test setup you can only call keywords, you cannot set variables to the result of other keywords directly within the setup statement .

That being said, it's easy to create a custom setup keyword that does what you want. For example:

*** Settings ***
| Suite Setup | Custom suite setup

*** Keywords ***
| Custom suite setup
| | ${A Variable}= | Set Variable | A String
| | Set suite variable | ${A Variable}

The above has the same effect as if robot supported setting variable from keywords directly in the setup. And, of course, you can call any keyword, not just Set Variable.

like image 155
Bryan Oakley Avatar answered Nov 06 '22 20:11

Bryan Oakley


To expand on Bryan's answer and add clarification for those of you not specifically interested in creating a suite variable based on the results of a keyword, there are other ways to initialize "global" variables at the start of a Robot Framework test.

The easiest way is to put them under a Variables header.

*** Variables ***
${this_string}  This String
${that_int}     5

An alternative way to do that is to put the same variables in a Resource .txt file. Once it's called under *** Settings ***, the variables can be used freely. Assuming you have your variables in a file called VarList.txt, the following code will initialize them:

*** Settings ***
Resource    VarList.txt

Should you be using a Resource file with existing keywords and internal variables, this will also work for that.

This all assumes you want static variables. Set Suite Variable and Set Global Variable can both be used with keywords like Bryan said. Set Suite Variable works well for scripts with multiple test Suites, while Set Global Variable should be used extra sparingly in that case. In a single-Suite script, however, the differences are all but negligible, though best practice would be to stick with Set Suite Variable unless you really want it to be global, just on the off-chance you decide to add that Suite to a script running multiple Suites.

like image 20
Brandon Olson Avatar answered Nov 06 '22 19:11

Brandon Olson