I have trouble with assert keys in my code.
No keyword with name 'in {'name': '$.data[0].name'}' found.
my code is
*** Variables ***
&{name} name=$.data[0].name
*** Variables ***
Run keyword if 'name' in ${name} log name is in the log as expected
The way you've defined your test cases (assuming the second *** Variables *** is actually *** Test Cases ***), robot thinks Run keyword if is the name of a test case, and the first keyword is 'name' in ${name}
The solution is to give your test case a name.
There's also a problem that you have two spaces after 'name' which should give you a different error than what you're reporting. Also, the second table should be named *** Test Cases ***.
*** Test Cases ***
Example test case
Run keyword if 'name' in ${name} log name is in the log as expected
You should probably also use $name rather than ${name} so that robot injects the actual variable into the expression.
*** Test Cases ***
Example test case
Run keyword if 'name' in $name log name is in the log as expected
Related
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.
I want to use given library name as an argument in my keyword as in the following:
*** Settings ***
Library some_library WITH NAME some_lib_name
*** Keywords ***
FOO
[Arguments] ${some_lib_name} ${params}
${some_lib_name}.SEND param1
But the robot framework gives an error "No keyword with name '${some_lib_name}.SEND' found."
So is there a way to make it work?
You can use run keyword since it will evaluate variables before calling the keyword:
*** Keywords ***
FOO
[Arguments] ${some_lib_name} ${param1}
Run keyword ${some_lib_name}.SEND ${param1}
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
How do I create a custom keyword in Robot Framework that takes an optional argument so that I can call that keyword either with or without argument? e.g. that argument should default to None.
Use ${arg_name}=${None}
Example:
*** Settings ***
Library REST
*** Keyword ***
POST /endpoint
# [Arguments] ${body}=NONE # BAD IDEA
[Arguments] ${body}=${None} # BETTER
&{response}= REST.POST /endpoint ${body}
Now in your test cases you can call POST /endpoint w/ or w/o argument
POST /endpoint
# or
POST /endpoint {"some": "valid json"}
Example:
*** Test Cases ***
My Cool Test Case
[Tags] cool
POST /endpoint
POST /endpoint {"Best Test Automation Framework": "Robot Framework"}
NOTE: don't use NONE since that will set the value to a string 'NONE'
Very basic example using built-in variable ${NONE}:
*** Test Cases ***
Call Keyword Without Argument
Log Name
Call Keyword With Argument
Log Name Steve
*** Keywords ***
Log Name
[Arguments] ${name}=${NONE}
Log Your name is '${name}'
Note: Consider that according to Robot Framework documentation ${NONE} and ${NULL} are synonyms
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#boolean-and-none-null-variables
I ran into an odd issue so I wrote this example where I call "print ${dir}" twice:
*** Variables ***
${dir} = "c:\\temp"
*** Test Cases ***
Test
print ${dir}
run keyword if 1 == 1 print ${dir}
*** Keywords ***
print ${input1}
log to console \r${input1}
Output:
"c:\temp"
"c: emp"
What do I need to do to make "print ${dir}" print the same thing each time?
The problem stems from the fact you're using the embedded argument syntax. In order for robot to know what keyword to call, it must first do expansion of the variable before calling the keyword. That removes one layer of backslashes. Then, when your keyword passes what's left to the log to console keyword it sees \t as a tab character, which is why you see a tab character rather than the backslash and the letter "t".
One solution is to use traditional arguments rather than embedded arguments. The following example gives the same output for both times the keyword is called:
*** Variables ***
${dir} = "c:\\temp"
*** Test Cases ***
Test
print ${dir}
run keyword if 1 == 1 print ${dir}
*** Keywords ***
print
[Arguments] ${input1}
log to console \r${input1}
It seems to me that the Run Keyword If keyword does some additional escaping of the backslashes. By adding 1 more backslash you'll see it happen in the first example as well.
To overcome this issue is to switch from backslashes () to forward slashes (/). This works on both *nix and Windows based systems.
*** Variables ***
${dir} = "c:\\temp"
to
*** Variables ***
${dir} = "c:/temp"
This still makes a valid path on Windows. So functionally the path reference will work as well.