RobotFramework: empty variable check suddenly not working anymore - robotframework

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.

Related

Cannot set a RobotFramework dictionary

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

Variables not being Set dynamically in Robotframework

I am using the robotframework for automating testing in one of my project.
I am trying to Set the variable based on the IF condition under *** variables*** section but i see that the whole if condition is getting assigned to it
For example, in one of robot file test.robot
*** Variables ***
${Host} = Set Variable If ${account} == test testingHost prodHost
I am passing the ${account} value from the command line as
robot --variable {account}:test
When i try to log the value of ${Host} what i see is the full if condition into it as output. Something like this
Started Noisy Test Case ... Set Variable If test==test testingHost
Followed the link of builtin(https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Variable%20If) library

how to pass variable with value from one robot file to another robot file in robot frame work

Test 1.robot
*** Variables ***
${node} babitha
*** Test Cases ***
A Test Case
Log To Console ${node}
Test 2.robot
*** Settings ***
Resource C:\Users\2013\Desktop\Test 1.robot
*** Test Cases ***
A Test Case
Log To Console ${node}
Trying to print babitha on console when i am running Test 2.robot file
A resource file cannot contain *** Test Cases *** section - if you try to run your sample the framework will probably produce an error with such message.
If you want to use a variable from one suite in another, then in the initial one you declare it it as such with the Set Global Variable keyword:
Set Global Variable ${node} # you can reassign its value here, or leave to the previously set
This has one caveat though - you must be sure the setter is going to be called before the case that will try to use it (naturally) - otherwise, it will not be defined for it.
An alternative is to store the variable in a 3rd file (a resource one) and import it in the suites that need it.
And another alternative is to pass it with --variable node:babitha in the command line when running the tests - thus it will be globally available from the start, and all cases will be able to access it (and modify, through the Set Global Variable).
If more .robot files have to share some variable(s), it's better to keep these variables in a separate file (and folder), I'd suggest similar structure:
.
|__Resources
|__Tests
In Resources/, you will have a file I'll call config.py:
node = "babitha"
The last step you need is to load the config file in both of your test suites:
Tests/Test 1.robot
*** Settings ***
Variables ../Resources/config.py
*** Test Cases ***
A Test Case
Log To Console ${node}
Tests/Test 2.robot
*** Settings ***
Variables ../Resources/config.py
*** Test Cases ***
A Test Case
Log To Console ${node}
One remark at the end:
You used an absolute path C:\Users\2013\Desktop\Test 1.robot, try to avoid that, if someone else clones your project, it will likely break on their environment

RobotFramework/RIDE can't pass sample test - "No keyword with name 'Run ${sakura}' found."

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

Run a test case Multiple times and display the pass and fail count under test statistics

How to run a particular test case multiple times and display the pass and fail count under Test Statistics?
Below is the current code I have to run a test case multiple times. (The test case is implemented in a keyword and called)
*** Test Cases ***
Testcase
repeat keyword 5 Run Keyword And Continue On Failure Execute
*** Keywords ***
Execute
log Hello world!
The code is run from cmd using "pybot testcase.robot"
This code runs the test multiple times but I'm not getting the final pass/fail count in the logs.
I need to manually count the pass and fail test case repetitions.
So what modifications should I do to get the data automatically and should be seen in Test Statistics of the log also.
Instead of using "Repeat Keyword", use For loop.
Use "Run Keyword And Return Status" instead of "Run Keyword And Continue On Failure ".
*** Test Cases ***
Test Me
${fail}= Set Variable 0
:FOR ${index} IN RANGE 5
\ ${passed}= Run Keyword and Return Status Execute
\ Continue For Loop If ${passed}
\ ${fail}= ${fail} + 1
${success}= Set Variable 5 - ${fail}
Log Many Success: ${success}
Log Many fail: ${fail}

Resources