How to auto +1 increment at text - robotframework

I wanted to append fixed text and +1 behind it.
I have tried to add variable into the text but seems it is invalid.
Test Case
Send Item
[Arguments] ${RECEIVER} ${CLASSIFICATION}
Add item
Choose Mail Recipient ${RECEIVER}
${TIME} Get Time
${TCNUMBER}= ${TC007-00}+1
Add desc [RegressionTestCase][${TCNUMBER}][${CLASSIFICATION}][${SENSITIVITY}] [${TIME}]
error:
No keyword with name '${TC007-00}+1' found.
The expected results are:
TC007-001, TC007-002, etc.

For this case you have to store your number that needs to be incremented somewhere. In my example it is a global variable called ${ID}. You should increment this one using the Evaluate keyword and before that you can simply concatenate it with the base number of the TC.
Based on where you want to do this ${ID} can be a global, suite or test variable, or even local. For more see the User Guide, Using Set Test/Suite/Global Variable keywords.
*** Variables ***
${TCNUMBER_BASE} TC007-00
${ID} 1
*** Test Cases ***
Append Test
${TCNUMBER}= Set Variable ${TCNUMBER_BASE}${ID}
${ID}= Evaluate ${ID} + 1
Log ${TCNUMBER}
${TCNUMBER}= Set Variable ${TCNUMBER_BASE}${ID}
${ID}= Evaluate ${ID} + 1
Log ${TCNUMBER}
${TCNUMBER}= Set Variable ${TCNUMBER_BASE}${ID}
${ID}= Evaluate ${ID} + 1
Log ${TCNUMBER}

Related

Robotframework, How to define a dynamic variable name

I want to Define variable having dynamic name.
For example
${${FILE}{VAR}} Create List // ${FILE} = TEST, ${VAR} = VAR
Then I want to get variable named '${TESTVAR}'.
Here is my Summarized code...
*** Settings ***
Library SeleniumLibrary
Library ExcelLibrary
Library Collections
*** Variables ***
${VAR}= VAR
*** Keywords ***
Open Document And Assign Variable
[Arguments] ${FILE}
Open Excel Document filename=${FILE} doc_id=doc_var
${${FILE}${VAR}} Create List # It doesn't work ..
The previous answer is not really accurate.
This can be achieved exactly with robot's "Variables inside variables" feature like so:
FOR ${idx} IN RANGE 3
${var_name} = Catenate SEPARATOR=_ var ${idx}
Set Suite Variable ${${var_name}} ${idx}
END
you get:
   var_1=1
   var_2=2
   var_3=3
Please note that variables resolution is happening only for keywords arguments (at least for robot version 3.1.2 that I am using), therefore either one of 'Set test variable', 'Set Suite Variable' or 'Set Global Variable' keywords must be used. The following won't work:
FOR ${idx} IN RANGE 3
${var_name} = Catenate SEPARATOR=_ var ${idx}
${${var_name}} = ${idx}
END
results in:
    No keyword with name '${${var_name}} =' found.
For your list case you just need to create a list variable and reassign it to dynamically named variable using the above mentioned keywords
This is currently not possible with Robot Framework. You can use "variables inside variables" to resolve the values of variables (see the documentation on this topic) but not to resolve/set the name of the variable itself.
Though I am afraid that would be confusing anyway. Maybe you can explain your motivations and people can come up with another solution for your problem.

Is it possible to give arguments inside variables in robot framework?

Library REST ${base_url}
*** Keywords ***
Get Requests
GET ${rest_of_the_url}
Output response body
*** Test Cases ***
Do some searching
Get Requests
*** Variables ***
${base_url} https://business.com
${rest_of_the_url} /api/${department}/${person_name}
How can I assign values to ${department} and ${person_name}? I don't want to set those inside Variables because then I cannot write multiple scenarios inside one .robot file. Is it possible to do the assignment as arguments?
i do not think there is a way to pass arguments within the variables,
The below section is straight from the documentation of Robotframework,
where you can create Variables inside variables
Variables inside variables
Variables are allowed also inside variables, and when this syntax is used, variables are resolved from the inside out. For example, if you have a variable ${var${x}}, then ${x} is resolved first. If it has the value name, the final value is then the value of the variable ${varname}. There can be several nested variables, but resolving the outermost fails, if any of them does not exist.
In the example below, Do X gets the value ${JOHN HOME} or ${JANE HOME}, depending on if Get Name returns john or jane. If it returns something else, resolving ${${name} HOME} fails.
*** Variables ***
${JOHN HOME} /home/john
${JANE HOME} /home/jane
*** Test Cases ***
Example
${name} = Get Name
Do X ${${name} HOME}
E.g.,
${person_name}= Set Variable Matt
${department}= Set Variable R&D
# then your code continues
${rest_of_the_url} /api/${department}/${person_name}
Set Variable documentation.
Try to see this using the Set Test / Suite / Global Variable keywords here:
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html
Use "Set Suite Variable" keyword then enter the variables $ {person_name} e
$ {department} inside * Variables * then you should read the value inside the test.

How to re initiate Variable value in RobotFramework in mid of test case

I have some variables defined in Resource file.
*** Variables ***
${x} SomeValue
# Derived String
${y} SomeString_${x}
After using this in existing test case I modified ${x}. After same am able to use ${x} as modified variable but ${y} remain unchanged. Do we have some way to re initiate ${y} as per new ${x}.
Short answer - not automatically; the value of ${y} will remain as is, regardless that ${x} changed.
The reason is the values in the Variables section are set once, on instantiating the suite. At that time the value of ${y} is set to "SomeString_the-current-value-of-x", and that's it; e.g. it's not some kind of pointer to the present value of ${x}, changing as ${x} changes.
If you want to re-set the value of y, you can do it after you've changed x:
${y}= Set Variable SomeString_${x}

Robot: assign variables in setup phase using Run Keywords

I'm trying to create a setup phase for a test case in which I assign variables. I know in order to do multiple keywords I need to use Run Keywords, but is it possible to set variables when doing this? For example:
*** Test Cases ***
Case1
[Setup] Run Keywords
... ${var1}= Keyword1
... AND ${var2}= Keyword2
obviously the above doesn't work because ${var1} and ${var2} are just treated as arguments to Run Keywords. Since they haven't been defined yet, setup fails.
No, you cannot. Even though you added "using Run Keywords", this question has the same answer as Is possible to create new variable in suite/test set up - Robot Framework?
You can use the Set Suite Variable keywork to do that.
set suite variable ${var1} Hello World
You might need to escape the variable...
set suite variable \${var1} Hello World
From the builtin library documentation:
If a variable already exists within the new scope, its value will be overwritten. Otherwise a new variable is created. If a variable already exists within the current scope, the value can be left empty and the variable within the new scope gets the value within the current scope.
The question here why are you trying to do this?
The way I do it, if I want to call keywords and set their outputs in variables
to reuse them in my test suite, I do the following:
*** Settings ***
Library BuiltIn
Suite Setup Initialize Variables
*** Keywords ***
Initialize Variables
${Argument1} = Set Variable some_value
${output1} = Keyword1 ${Argument1}
${output2} = Keyword2
${output3} = Keyword3 ${Argument1} other_value
*** Test Cases ***
Test Case 1
# Here you can use the variables that you initialized in the Suite Setup.
Log ${output1}
Log ${output2}
Log ${output3}
Test Case 2
# Also here you can use the same variables.
No Operation
Note: If you want to setup the variables for each test case, you can do it either in the settings section like this:
*** Settings ***
Test Setup Initialize Variables
Or you can use the setting in the test case itself (same as what you did in your question)
Test Case 1
[Setup] Initialize Variables
Note that "Initialize Variables" can take arguments as well if you need.

Using Variables in Variable definition

I am trying to get variables into variables but it wont work. I searched google and tried a lot of stuff but it did not wor out.
I hope this question ist not "dumb":
What am I doing wrong ?
*** Settings ***
Library SeleniumLibrary
Library OperatingSystem
*** Variable ***
${year} Get Time return year
${month} Get Time return month
${day} Get Time return day
${output} ${CURDIR}\Testing\Tests\SRV\csdb_#{year}-#{month}-#{day}.log
*** Testcases ***
Textfile should have a line saying the service is started
${errors} = Grep File ${output} Test
From the robot framework user's guide:
The most common source for variables are Variable tables in test case
files and resource files. Variable tables are convenient, because they
allow creating variables in the same place as the rest of the test
data, and the needed syntax is very simple. Their main disadvantages
are that values are always strings and they cannot be created
dynamically.
In order to do what you want, you'll need to define the variables in a keyword. For example:
*** Keywords ***
Get Output
${year}= Get Time year
${month}= Get Time month
${day}= Get Time day
${output}= Set variable ${CURDIR}/Testing/Tests/SRV/csdb_${year}-${month}-${day}.log
[Return] ${output}
*** Testcases ***
Textfile should have a line saying the service is started
${output}= Get Output
${errors} = Grep File ${output} Test
Note: you can fetch all three parts of the data in a single call to the keyword, like so:
${year} ${month} ${day}= Get Time year month day
It's a bit hard to read with the space-separated format, but the variable names must each be separated by two or more spaces, but "year month day" should have only one.

Resources