Error while incrementing the value in Robot Framework - robotframework

*** Settings ***
Library DateTime
*** Test Cases ***
Test title
${TIME}= get current date result_format=%H
RUN KEYWORD IF
... int(${TIME})%2==0
... ${TIME}= catenate SEPARATOR= ${TIME} :00
... ELSE
... ${TIME}= Evaluate int(${TIME})+1
... ${TIME}= catenate SEPARATOR= ${TIME} :00
log to console ${TIME}
I'm getting the following error:
No keyword with name '11=' found.

You cannot have a variable assignment inside Run Keyword If - as its name goes it's only for running keyword, and doesn't support assignments (it treats the variable as another keyword it has to run). Thus the error - the framework substituted ${TIME} with its value (11), and tried executing that.
In version 4.0 it supports proper IF/ELSE blocks, where this restriction doesn't apply:
Test title
${TIME}= get current date result_format=%H
IF int(${TIME})%2==0
${TIME}= catenate SEPARATOR= ${TIME} :00
ELSE
${TIME}= Evaluate int(${TIME})+1
${TIME}= catenate SEPARATOR= ${TIME} :00
END
log to console ${TIME}
In the mean time, you can solve it with another approach - using Set Variable If. It's sumilar to Run Keyword If that it's conditional, but takes only values.
There's a functionality in the framework that you can do calculations in place (strictly speaking - calling methods), that we'll employ - increment the value with 1:
Test title
${TIME}= get current date result_format=%H
${TIME}= Convert To Integer ${TIME} # to be sure it's type is this, I haven't checked what the keyword returns
${TIME}= Set Variable If ${TIME}%2==0 ${TIME}:00
... ${TIME + 1}:00 # this is the ELSE block
log to console ${TIME}

Related

How to skip remaining steps in a test case 1 and continue with other Test cases in Robot Frame work

*** Test Case ***
Test Case 1
step 1
step 2
error handling $value
step 3
step 4
Test Case 2
....
....
....
*** Keyword ***
error handling
Run Keyword if $value=='Text'
"execute this steps" #after matching how to skip remaining steps in test Case 1 and continue with Test Case 2
ELSE
Continue with next steps
i want to skip "step3 & step 4" if "$value == Text" in "error handling" keyword
Use Pass Execution inside the if clause, if you want to skip the rest.
It does just that, marks the case as passed without running what remains in it (apart from the teardown). And the same can be achieved with failing status by the Fail keyword call.
Try below code, it will not mark status as skip
*** Variables ***
${status}= ${False}
*** Tasks ***
Conditional task: Skip if a response matches
Skip If ${status}
Log to console Task is skipped
*** Tasks ***
Main task
Log to console Task is completed
Try this. It will skip all rest of the steps when condition match
*** Test Case ***
Test Case 1
${value}= Set Variable Text
${Text}= Set Variable Text
Log To Console message1
Log To Console message2
${status}= error handling ${value} ${Text}
BuiltIn.Skip if '${status}' == '${True}'
Log To Console message3
Log To Console message4
*** Keyword ***
error handling
[Arguments] ${value} ${Text}
IF '${value}' == '${Text}'
Return From Keyword ${True}
ELSE
Continue with next steps
END

RobotFramework: empty variable check suddenly not working anymore

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.

How to fix "No keyword with name '0=' found" in Robot Framework

I am a newbie in Robot framework. I want to run multiline IF Statement but I am getting below error:
Error :
"0= Evaluate, ${G_NO_OF_RECIPIENTS}+${NUMBER_OF_CALLEE} FAIL No
keyword with name '0=' found. "
This error is occurring for variable ${REM_COUNT}
Code :
Log ${G_NO_OF_RECIPIENTS}
Log ${NUMBER_OF_CALLEE}
${REM_COUNT} Set Variable ${0}
Run Keyword If "${NUMBER_OF_CALLEE}" != "${G_NO_OF_RECIPIENTS}" Run Keywords
... ${REM_COUNT}= Evaluate ${G_NO_OF_RECIPIENTS}+${NUMBER_OF_CALLEE}
... AND Log "ITS WORKING"
Similar piece of code works somewhere else, only thing was I did not use multiline if statement in it. I appreciate if I get help on this.
Thanks
The Run Keywords does not allow variable assignment inside its block, e.g. this line:
Run Keywords
... ${REM_COUNT}= Evaluate ${G_NO_OF_RECIPIENTS}+${NUMBER_OF_CALLEE}
... AND Log "ITS WORKING"
is illegal syntax. It tried to substitute ${REM_COUNT} with its value (0), and to run it - thus the failure.
Run Keyword If does pass any return values, so you can do it this way:
${REM_COUNT}= Run Keyword If "${NUMBER_OF_CALLEE}" != "${G_NO_OF_RECIPIENTS}"
... Evaluate ${G_NO_OF_RECIPIENTS}+${NUMBER_OF_CALLEE}
... ELSE Set Variable ${REM_COUNT} # if the condition is False, leave the variable to its previous value
Run Keyword If "${NUMBER_OF_CALLEE}" != "${G_NO_OF_RECIPIENTS}" Log "ITS WORKING"

RF 3.0.4 nested dictionary syntax error if first key is number, dot notation doesn't work

I am using rf 3.0.4. I upgraded because of the dot notation upgrade (before I was using rf 2.9).
My problem is when I want to access a nested dictionary item and the first key (it is an id from db) is a number, I got a syntax error.
I have a nested dictionary: &{Attributes}
So what I want to do:
${Attributes.1000.name}
The syntax error I get:
Resolving variable '${Attributes.1000.name}' failed: SyntaxError: invalid syntax (<string>, line 1)
And what is working:
${Attributes["1000"]["name"]}
I'd like to use the dot notation, because it is more readable.
Do any of you know why it doesn't work?
It seems to me to be a limitation of Robot Framework. When a dictionary key item starts with a number (even when a string) then it will fail. In the below two test cases this is shown.
To me this sounds like a defect and you may want to log this as an issue with the project's GitHub issue log.
*** Settings ***
Library Collections
*** Variables ***
${name} MyName
&{person} name=${name}
&{person_valid} A1000=${person} A2000=${person}
&{person_invalid} 1000A=${person} 2000A=${person}
*** Test Cases ***
TC - Valid
${pers} Set Variable ${person_valid.A1000}
Dictionaries Should Be Equal ${pers} ${person}
${pers_name_1} Set Variable ${person_valid["A1000"]["name"]}
Should Be Equal As Strings ${pers_name_1} ${name}
${pers_name_2} Set Variable ${person_valid.A1000.name}
Should Be Equal As Strings ${pers_name_2} ${name}
TC - Fails
Run Keyword And Expect Error
... Resolving variable '\${person_invalid.1000A}' failed: SyntaxError: invalid syntax (<string>, line 1)
... Set Variable ${person_invalid.1000A}
Run Keyword And Expect Error
... Resolving variable '\${person_valid.1000A.name}' failed: SyntaxError: invalid syntax (<string>, line 1)
... Set Variable ${person_valid.1000A.name}

How to use TRUE and PASS values for the test cases in Robot Framework?

I am new to Robot Framework so I need help with this I am trying to do "RUN KEYWORD IF" ,if I look in docs they give
Run Keyword If | '${status}' == 'PASS' | Some Action arg.
My doubt is with this PASS value . How to get this as every time I Try ,I get a none value for every Keyword statement. I am trying to do something this :
*** Settings ***
*** Variables ***
${name} theon
*** Test Cases ***
Verify
${x}= Set Variable function
Run Keyword If '${x}==PASS' Log 'True'
... ELSE Log 'False'
*** Keywords ***
function
${return}= Should Equal ${name} theon
[Return] ${return}
I get error that x is defined NONE then how I can validate in the condition that my keyword is success or True.
I would also like some help with whether I can use a self defined keyword in place of 'condition' to run some keyword.
RUN KEYWORD IF|| Some self keyword here which return me pass or true|| Target Keyword
What does Should Equal do?
Since it does not exist in RF there is Should Be Equal but this just passes or fails the test.
Note all Should... behave like this.
Maybe you want the evaluate if ${name} and theon are equal (the same) then use evaluate ...
${return}= evaluate '${name}'=='theon'
Evaluate will return True or False
Then this will work.
Run Keyword if ${x} Log True
Instead of "status", you can provide the "conditions" directly and then take a specific action using the "self defined keyword"
An example below :
Run Keyword if ${medicine_expiry_year} > 2000 and ${medicine_expiry_year} < 2016 Send Mail to Company ${company_mail_id}
Run Keyword if ${medicine_expiry_year} == 2016 Report Remaining Months to Store operator ${month_of_expiry}
Run Keyword if ${medicine_expiry_year} > 2016 Log "Keep the medicine"
Conditions in Above Example :
${medicine_expiry_year} > 2000 and ${medicine_expiry_year} < 2016
${medicine_expiry_year} == 2016
${medicine_expiry_year} > 2016
Self defined keywords with parameters :
Sends a Mail to Company using keyword "send Main to Company" with the mail id provided in "${company_mail_id}"
Send Mail to Company ${company_mail_id}
Report the Remaining Months to Store operator using the keyword "Report the Remaining Months to Store operator" expiry month provided using variable "${month_of_expiry} "
Report Remaining Months to Store operator ${month_of_expiry}
Just Log the message using the keyword
Log "Keep the medicine"
Hope so this was helpful.

Resources