I wrote this simple minimal script to show how I suffer:
main.robot
Library Collections
Library BuiltIn
Library String
*** Variables ***
&{info} Create Dictionary
*** Test Cases ***
Case_00_Initialization
Log Hello 1 WARN
Set To Dictionary ${info} field1=A sample string
Log Hello 2 WARN
Running this code by
python -m robot -L TRACE --NoStatusRC main.robot
Gives me errors:
[ ERROR ] Error in file 'C:\test\main.robot' on line 7: Setting variable '&{info}' failed: Invalid dictionary variable item 'Create Dictionary'. Items must use 'name=value' syntax or be dictionary variables themselves.
==============================================================================
Main
==============================================================================
[ WARN ] Hello 1
Case_00_Initialization | FAIL |
No keyword with name 'Set To Dictionary' found.
------------------------------------------------------------------------------
Main | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
Output: C:\test\output.xml
Log: C:\test\log.html
Report: C:\test\report.html
The application is supposed to set a variable info in the initialization and it will be used in the next test cases. I do not want to use Set Global Variable however.
Please note that this is a minimal working example, do not suggest to set field1 at Variables section. It is not possible. Even that one will not solve the problem of No keyword with name 'Set To Dictionary' found.
In the Variables section you cannot use keywords - and this is exactly what you did with the Create Dictionary there.
You can add some key:values to it (like "field" , but you don't allow us that ;), or - you can initialize it to be an empty dictionary (e.g. like {} in python). The later is done by passing the special value &{Empty}:
*** Variables ***
&{info} &{Empty}
I eventually found what is wrong. I was missing the line for *** Settings *** I couldn't imagine that it does matter. Shame that RF does not have many full examples online.
This is the working code:
*** Settings ***
Library Collections
*** Variables ***
&{info}
*** Test Cases ***
Case_00_Initialization
Log Hello 1 WARN
Set To Dictionary ${info} field1=A sample string
Log Hello 2 WARN
Related
Is it possible to call keyword along with template in robotframework,
*** Keywords ***
PASSWORD UPDATE
[Arguments] ${apidata} ${expctdRspnce} ${jmxPath}
---------
-------
*** Test Cases ***
CHANGE_PASSWORD
${apidata} = Get Excel value 2 3
${expctdRspnce} = Get Excel value 2 5
[Template] PASSWORD UPDATE
${apidata} ${expctdRspnce} ${jmxPath}
In above code i am calling get excel value before calling the template.So here i am getting below error,
Keyword 'PASSWORD UPDATE' expected 3 arguments, got 4.
If i remove both the variables and call only the template then it is working fine.Is there any way to call kewyrods before calling template?
I'm playing around with command-line arguments and had something working both with / without giving a command-line argument, but suddenly it starts failing if I don't give a command-line argument.
On https://groups.google.com/g/robotframework-users/c/_5Usp-K4Dlw?pli=1 I read that adding a space before the variable and comparing it to a value like this '${ VAR}'==<value> should work to check if a variable is existing or not.
The code-snippet that was working before is:
*** Test Cases ***
My test
[Documentation] test to check if the SUT reacts as expected
${is_test}= Evaluate '${ VAR}'=='test'
Log To Console ${is_test}
Run Keyword If ${is_test} Log To Console VAR == test
After changing the code to (Removed '[Documentation]' since it was copied from another test):
*** Test Cases ***
My test
${is_test}= Evaluate '${ VAR}'=='test'
Log To Console ${is_test}
Run Keyword If ${is_test}
... Log To Console VAR == test
it suddenly started failing with: Variable '${ VAR}' not found. errors.
And after changing it back to the original it still fails.
I can't figure what I did wrong in the change.
Note: The company I'm working for uses RobotFramework version 3.0.4 (yes I know ancient)
Managed to solve it by adding ${VAR} with some default value to a *** Variables *** list. But still I don't understand why it was originally working without that list.
Here is how your test should look like if you want to check if variable is empty and do something based on that.
*** Variables ***
${VAR}= test
*** Test Cases ***
My test
Run Keyword If '${VAR}'=='${EMPTY}' Log To Console Variable is empty
... ELSE Log To Console Variable is not empty
If you want variable default value to be empty and execute some keyword only if it comes from command line set variable as empty, for example:
*** Variables ***
${VAR}= ${EMPTY}
*** Test Cases ***
My test
Run Keyword If '${VAR}'=='${EMPTY}' Log To Console Variable is not set
... ELSE Log To Console Variable is ${VAR}
Run your test from command-line without passing variable
$ robot sample.robot
==============================================================================
Sample
==============================================================================
My test Variable is not set
My test | PASS |
------------------------------------------------------------------------------
Sample | PASS |
1 test, 1 passed, 0 failed
==============================================================================
Now, run with passing variable
$ robot -v VAR:test sample.robot
==============================================================================
Sample
==============================================================================
My test Variable is test
My test | PASS |
------------------------------------------------------------------------------
Sample | PASS |
1 test, 1 passed, 0 failed
==============================================================================
P.S. About space inside variable, I think it's just typo in Google Groups. If it was working then variable might be globally accessible or defined somewhere else.
This test case if working fine in Robot Framework 3.1.2 but in 3.2.1 I'm getting error message:
Setting 'Template' accepts only one value, got 2.
I can't see that there is any updates to the documentation that explains this. Any ideas?
*** Test Cases ***
Test Case
[Template] The result of ${calculation}
... should be ${expected}
1 + 1 2
2 + 2 4
*** Keywords ***
The result of ${calculation} should be ${expected}
${result} = Evaluate ${calculation}
Should Be Equal As Integers ${result} ${expected}
The ... marks an argument boundary. What you did is exactly the same as this:
[Template] The result of ${calculation} should be ${expected}
Like the error says, the [Template] setting only accepts a single argument but you're passing two. You cannot define the keyword on multiple lines.
It appears that the old (pre 3.2) parser may have been a bit lax and allowed you to split the template keyword on multiple lines. The new parser doesn't allow that.
I'm trying sample test of RobotFramework/RIDE according to this article;
Desktop Application Automation With Robot Framework
https://medium.com/#joonasvenlinen/desktop-application-automation-with-robot-framework-6dc39193a0c7
Now, I'd set up ride and run it, and made first test code below;
*** Settings ***
Documentation sample
Library OperatingSystem
Library C:/Python27/Lib/site-packages/AutoItLibrary/
*** Variables ***
${sakura} C:\Sakura\sakura.exe
*** Test Cases ***
first_test
first_test_run
*** Keywords ***
first_test_run
log to console Hello, world!
Run ${sakura}
But when I run this test in ride, I got result report below;
command: pybot.bat --argumentfile c:\users\tie292~1\appdata\local\temp\RIDEujrsg3.d\argfile.txt --listener C:\Python27\Lib\site-packages\robotide\contrib\testrunner\TestRunnerAgent.py:57677:False C:\Users\tie292025\Desktop\first_test.robot
TestRunnerAgent: Running under CPython 2.7.13
First Test :: sample
first_test Hello, world!| FAIL |
No keyword with name 'Run ${sakura}' found.
First Test :: sample | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
My application environment is below;
numpy==1.16.5
Pillow==6.2.1
Pygments==2.4.2
PyPubSub==3.3.0
pywin32==227
robotframework==3.1.2
robotframework-autoitlibrary==1.2.2
robotframework-ride==1.7.3.1
robotframeworklexer==1.1
six==1.13.0
wxPython==4.0.7.post2
Anyone can help?
Put two or more spaces between Run and ${sakura}.
Now Robot tries to find a keyword called Run ${sakura} rather than a keyword Run with ${sakura} value as an argument.
You can test
$ {sakura} C:\\Sakura\\sakura.exe
or
Run C:\\Sakura\\sakura.exe
From what I read about variable scopes and importing resource files in robotframework doc i would expect this to work (python 2.7, RF 2.8.7):
Test file:
*** Settings ***
Resource VarRes.txt
Suite Setup Preconditions
*** Variables ***
*** Test Cases ***
VarDemo
Log To Console imported [${TODAY}]
*** Keywords ***
Resource file:
*** Settings ***
Library DateTime
*** Variables ***
${TODAY} ${EMPTY} # Initialised during setup, see keyword Preconditions
*** Keywords ***
Format Local Date
[Arguments] ${inc} ${format}
${date} = Get Current Date time_zone=local increment=${inc} day result_format=${format}
[Return] ${date} # formatted date
Preconditions
${TODAY} = Format Local Date 0 %Y-%m-%d
Log To Console inited [${TODAY}]
However the output is:
inited [2015-03-20]
imported []
RF documentation states:
Variables with the test suite scope are available anywhere in the test
suite where they are defined or imported. They can be created in
Variable tables, imported from resource and ....
which I think is done here.
If I add a line to keyword Preconditions like this, it works:
Preconditions
${TODAY} = Format Local Date 0 %Y-%m-%d
Set Suite Variable ${TODAY}
Log To Console inited [${TODAY}]
The reason is that in the first line a local variable is defined, instead of initialising the test suite variable declared in the variable table. A paragraph in RF doc hints to that:
Variables set during the test execution either using return values
from keywords or using Set Test/Suite/Global Variable keywords always
override possible existing variables in the scope where they are set
I think a major drawback of RF is that you cannot define variables dynamically in the variable table. Setting the scope of a variable from within a keyword is something I try to avoid.
For dynamic variables, you might to use a variable file in Python. See the section "Implementing variable file as Python or Java class" in the User Guide.
For example, I use a variables.py with:
if platform.system() in ['Darwin', 'Linux']:
OS_FAMILY = 'unix'
elif platform.system() == 'Windows':
OS_FAMILY = 'windows'
else:
OS_FAMILY = 'unknown'
OS_FAMILY_IS_UNIX = OS_FAMILY == 'unix'
OS_FAMILY_IS_WINDOWS = OS_FAMILY == 'windows'
Then in the Robot Tests I can use the dynamic variables ${OS_FAMILY}, ${OS_FAMILY_IS_UNIX}, and ${OS_FAMILY_IS_WINDOWS} anywhere.
You should be able to do create your ${TODAY} variable.