I want to use If-Condition with multiple actions in Robot Framework
${x} Set Variable 5
Run Keyword If ${x} == 5
... ${Test1} = Set Variable MyName
... ${Test2} = Set Variable MyLastName
... Else
... ${Test1} = Set Variable MyAddress
... ${Test2} = Set Variable MyTelephone
But it is not working
Error show FAIL : Variable '${Test1}' not found.
Could you please tell me about using IF-Condition with multiple actions
You can use "Run Keywords" keyword to perform multiple actions in IF condition
Kindly go through below link:
IF ELSE in robot framework with variables assignment
You have to either cover both actions with one custom keyword and then call Run Keyword If or call the keyword Set Variable If twice or write such logic into python (jython...) library.
Please notice "And" while using "Run Keywords"; also ensure tab used.
Run Keyword If <condition1> <action1>
... ELSE IF <condition1>
... Run Keywords
... <action1>
... AND <action2>
Set Test Variable ${temp} rxu
Run Keyword if '${temp}'=='rxu'
... Run Keywords
... Log To Console this is one
... AND Log To Console This is two
... ELSE Run Keyword Log To Console another block
Refer following keyword :
Run Keyword If ${x} == 5 Set Variable MyName
Run Keyword If ${x} == 1 Set Variable LastName
Or
Run Keyword If ${x} == 5 Set Variable MyName
... ELSE IF ${x} == 2 Set Variable MyName
... ELSE IF ${x} == 3 Set Variable Middle Name
Related
I'm trying to write a simple 'IF' statement on MariaDb but I stumbled on the basics.
This is my code:
SELECT #variable := `value` FROM configuration WHERE key = 'key'
this seems to work, but can a variable declared in this way be used in a if ?
IF #variable = "foo" THEN
UPDATE ...
UPDATE ...
DELETE ...
ELSE
UPDATE ...
END IF;
I'm forced to put this code only in a procedure ? Most of the code I read put all this inside a procedure but in the official documentation here there is nothing about it.
When I try to run this script inside phpMyAdmin I get:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IF #variable = "foo" THEN
UPDATE tags SET ....
The if statement can be used outside of stored procedures.
PHPMyAdmin apprently has a delimiter as part of its UI.
The alternative is:
UPDATE ... WHERE #variable = "foo"
UPDATE ... WHERE #variable = "foo"
DELETE ... WHERE #variable = "foo"
UPDATE ... WHERE #variable != "foo"
I have these variables:
*** Variables ***
${current} ""
${doc_type} ""
${document_type} ""
${HoiProo} Hoist\'s Proof Loading Certificate
${HoiMacIns} Hoisting Machinery Inspection Certificate
${inspection_certificate} Certificate.InspectionCertificate
${test_certificate} Certificate.TestCertificate
and this keyword:
*** Keywords ***
Set Doc_type
${doc_type} = Set Variable If
... '${current}' == '${HoiProo}' ${test_certificate}
... '${current}' == '${HoiMacIns}' ${inspection_certificate}
Set Suite Variable ${document_type} ${doc_type}
Whole thing
Setup current
${current} ${HoiMacIns}
Setup Doctype
Set Doc_type
But I don't understand why Robot keeps giving me this error:
Evaluating expression ''Hoisting Machinery Inspection Certificate' == 'Hoist's Proof Loading Certificate'' failed: SyntaxError: invalid syntax (<string>, line 1)
*I have also tried to remove '-signs *
Set Doc_type
${doc_type} = Set Variable If
... ${current} == ${HoiProo} ${test_certificate}
... ${current}' == ${HoiMacIns} ${inspection_certificate}
Set Suite Variable ${document_type} ${doc_type}
and to type it out
Set Doc_type
${doc_type} = Set Variable If
... ${current} == Hoist\'s Proof Loading Certificate ${test_certificate}
... ${current}' == ${HoiMacIns} ${inspection_certificate}
Set Suite Variable ${document_type} ${doc_type}
If ${current} is ${HoiProo} then ${doc_type} should be ${test_certificate}. This flow works as I have tested to compare only ${HoiMacIns}. In the future I want to add more certificates and more doc_types for the if-else, that's why I need to have this thing run like this.
When using expressions, you have to remember that the expression is valid syntax after robot substitutes variables. If your variable contains Hoist's and you try to use an expression like '${HoiProo}', robot is going to create an expression like 'Hoist's', which is invalid syntax.
The easiest way around this is to use a special syntax for variables in expressions. If you omit the curly braces, robot will directly use the variable in the expression, removing the need to do any extra quoting.
For example:
${doc_type} = Set Variable If
... $current == $HoiProo ${test_certificate}
... $current == $HoiMacIns ${inspection_certificate}
This is all documented in the BuiltIn library documentation in the section named Evaluating expressions.
I am trying to assign a variable the return value of the Keyword (Get Ipsec Pkt Stat) if a condition is true. The following is the syntax I am using, however my variable ${ipsec_stats} gets assigned None, even though the condition is being satisfied:
Run Keyword If '${chassis_cluster}' == 'True'
... ${ipsec_stat} = Get Ipsec Pkt Stats ${R0} node=local
... ELSE
... ${ipsec_stat} = Get Ipsec Pkt Stats ${R0}
[Return] ${ipsec_stat}
run keyword if requires keywords as arguments, not variable names followed by keywords.
The correct way to assign a variable with run keyword if is to set the variable to the result of that keyword:
${ipsec_stat}= Run keyword if '${chassis_cluster}' == 'True'
... Get ipsec Pkt Stats ${R0} node=local
... ELSE
... Get ipsec Pkt Stats ${R0}
Is there a way to unset environment variables dynamically?
I would like to access the environment vars and do a find & replace/delete action so I can test more dynamically.
For instance, say I want to test the creation of users, I create vars like {{tmp-username}}, {{tmp-email}}, etc... replace them with other values for the next test and remove them when I'm done.
I would do a stringsearch on tmp- if I knew how to access these using code...
Thanks in advance for any reply
To clarify, this question is different: Postman: How to delete/clear postman environment variable at run-time
This deals with knowing the exact name of the var you wish to unset. I want to search or iterate trough the vars to remove or edit them.
Could you use a function in the Tests tab to iterate through the variables and clear them out after the last test had run?
For example:
function cleanup() {
const clean = _.keys(pm.environment.toObject())
_.each(clean, (arrItem) => {
pm.environment.unset(arrItem)
})
}
cleanup()
This wouldn't 'replace' the values but this would give you confidence that the ones that are set during the run are not being used again.
EDIT
If you wanted to clear out a specific set of variables, ones that you have given a certain prefix, you could use this:
function cleanup() {
const clean = _.keys(pm.environment.toObject())
_.each(clean, (arrItem) => {
if (arrItem.startsWith("tmp")) {
pm.environment.unset(arrItem)
}
})
}
cleanup()
If you want to see all the keys and the values you could use this to log them to the console:
_.map(pm.environment.toObject(), (value, key) => console.log(`The key is '${key}' and the value is '${value}'`))
When I run this code it says ${month} not found and ${month status} is returning True:
${month status}= Run Keyword and Return Status Element Should Be Visible xpath=//div[#data-title='Cost for Month']
Run Keyword If '${month status}' == 'True'
... ${month}= Get Text xpath=//div[#data-title='Cost for Month']
... Log Dashboard is displaying ${month} Cost Usage for month Text
... ELSE
... Fail Cost Dashboard is not displaying Cost Usage for month Text
${month status}= Run Keyword and Return Status Element Should Be Visible xpath=//div[#data-title='Cost for Month']
${month}= Run Keyword If '${month status}' == 'True' Get Text xpath=//div[#data-title='Cost for Month']
Run Keyword If '${month status}' == 'True' Log Dashboard is displaying ${month} Cost Usage for month Text
Run Keyword If '${month status}' == 'False' Fail Cost Dashboard is not displaying Cost Usage for month Text
A bit of a crude solution but we came across this situation as well, and solved it using the fix above.
I think your code was failing because ${month} was being interpreted as a keyword rather than a variable.