I'm having issue in negate the bool variable or comparison of FALSE against a bool variable to perform Run Keyword If
My Code is
Test Case
${isExist}= Run Keyword And Return Status Element Should Be Visible ${element_Id}
Run Keyword If ${isExist} == false click element ${cancelbtn}
Here I'm facing an Run time error
Evaluating expression 'True and' failed: SyntaxError: unexpected EOF
while parsing (, line 1)
I tried the following comparison too
${isExist} == 'false'
'${isExist}' == 'false'
${isExist} == ${false}
'${isExist}' == '${false}'
!${isExist}
Note: log to console ${isExist} - It logs the appropriate Boolean value in the console window.
if ${isExist} is a boolean, you can use not
Run Keyword If not ${isExist} ...
You can also compare to ${FALSE} or ${TRUE}:
Run Keyword If ${isExist} is ${FALSE} ...
Run Keyword If ${isExist} is not ${TRUE} ...
You say that ${isExist} == ${false} doesn't work, but it will if {isExist} is indeed the boolean value False.
Note: ${FALSE} and ${TRUE} are variables defined by robot. They are briefly mentioned in the documentation in the section titled Boolean and None/null variables in the robot framework user guide.
Related
I have a value that can be NULL or a string. The second test fails because a is of length 0.
How do I write an if-statement that handles both cases?
Example:
a <- NULL
if (is.null(a) | a=="something else then null") {
print("test")
}
is.null(a) || a=="something else then null"
(similarly, use && instead of & in a situation like this)
Explanation: when you use |, all conditions are checked one by one and then the "or" assertion is applied. This means that if one of the conditions is invalid, then there is an error, like in your case.
When you use ||, the "or" assertion is checked after each condition check. In your case, is.null(a) is TRUE. This means that is.null(a) "or" any other condition will also be TRUE, so there's no need to check the other condition.
This is based on my understanding of how it works, it's not a definition coming from the docs. I hope it's clearer.
More info: What's the differences between & and &&, | and || in R?
I want to identify if a value stored in a variable is an integer or not. If its an integer then it should return a Boolean value
I have tried using Built-In functions like Should be Equal As Numbers, Should be Equal As Integers but they did not work.
Since I am not so sound in Python, hence I was not able to make use of the Python built-in functions but I have a strong feeling that python functions like .isdigit() or .isnumeric() can come in handy here.
I am storing some value in a variable, say ${TestVariable}
Now, I have tried identifying the stored value as integer via following ways:
${Status} Run Keyword and Return Status Should be Equal As Numbers ${TestVariable} 1
Log to Console \n ${TestVariable}-${Status}
And I have passed values like
a,b,1,2
Since I have hard coded value 1 in Should Be Equal As Numbers, hence it returned True when the value stored in ${TestVariable} is 1 but returned False when the value was 2
Actual Result:
a-False
b-False
1-True
2-False
Expected Result:
I want Robot to return True when value is a number and False when its a character like below
a-False
b-False
1-True
2-True
Here is a possible solution, please note that "2e10" is converted to number, but the keyword does not consider that.
*** Test Cases***
Verify Types
FOR ${item} IN two ${None} 1235 2.567 2e10
${result}= Check Type ${item}
Log Item ${item} is ${result}
END
*** Keywords ***
Check Type
[Arguments] ${object}
[Documentation] Checks if the ${object } is INTEGER, NUMBER or STRING
Return From Keyword If not "${object}" NONE
${result} ${value}= Run Keyword And Ignore Error Convert To Number ${object}
${isnumber}= Run Keyword And Return Status Should Be Equal As Strings ${object} ${value}
${result} ${value}= Run Keyword And Ignore Error Convert To Integer ${object}
${isinteger}= Run Keyword And Return Status Should Be Equal As Strings ${object} ${value}
Return From Keyword If ${isnumber} NUMBER
Return From Keyword If ${isinteger} INTEGER
Return From Keyword STRING
if type(a) == int:
print('the value is integer')
a = "sabuj"
if type(a) == str:
print('the value is string')
a = [1,2,3]
if type(a) == list:
print('the value is List')
I've previously used a list as parameter to take in a variable/optional number of arguments to a keyword, and this has worked perfectly:
Keyword Name
[Arguments] ${otherVariable} #{args}
....
My question is how do I set up a default value for this, if the user omits any more values?
i.e. something like
Keyword Name
[Arguments] ${otherVariable} #{args}=['0']
....
Check is ${args} empty, and if so - set the default value to it:
Keyword Name
[Arguments] ${otherVariable} #{args}
${args}= Run Keyword If not $args Create List 0
... ELSE Set Variable ${args} # varags were passed, leave it as is
This is analogous to this python code (RF is based on it, so a lot of approaches / recipes are the same/pretty close):
def keword(otherVariable, *args):
if not args: args = [0]
file.robot
Keyword1
log this is keyword1
${some_value} = Set Variable Hello, world!
[Return] ${some_value}
file2.robot
Some_name
Run keyword If 'True' == 'True Run Keyword and return Status Keyword1
I want to use this way. How do i access the return value in file2.robot
Above, 'Some_name' in file2.robot calls the 'Keyword1', the return value 'some_value' to be printed in 'Some_name' of file2.robot.
How can it be achieved in one-liner as stated above ?
You cannot use a "Run keyword..." command and both get a return value and a pass/fail value. However, if all you need is the return value, Run keyword if will return the result of the keyword that it runs.
For example:
*** Test Cases ***
Example
${the_value}= run keyword if 'True' == 'True' keyword 1
With the above, keyword 1 will only run if the expression evaluates to true. ${the_value} will be set to the result of keyword 1.
If you need both the status and the returned value, you can use Run keyword and return status to run the keyword, but you'll have to modify the keyword to set a suite or global variable that your test can get after the keyword returns.
I'm using robot framework to test if a webpage opens correctly. The webpage has two possible outcomes if everything works as planned:
${element_1_visible} = Run Keyword And Return Status Element should be visible element_1
${element_2_visible} = Run Keyword And Return Status Element should be visible element_2
These variables are always True and False, so a simple or operation should be enough. How do I combine these two booleans to test if my page works? So far have tried:
Should be True ${element_1_visible} or ${element_2_visible}
Should be True ${element_1_visible} == True or ${element_2_visible} == True
also:
${result} = ${element_1_visible} or ${element_2_visible}
Should be True ${result}
The statement that needs to be evaluated should be a single argument. This means prevent multiple spaces, as 2+ consequtive spaces is the divider between arguments.
Updated your example, this now works.
*** Test Cases ***
TC
${element_1_visible} Set Variable ${True}
${element_2_visible} Set Variable ${False}
Should be True ${element_1_visible} or ${element_2_visible}
Should be True ${element_1_visible}==True or ${element_2_visible}==True
${element_1_visible} Set Variable ${False}
${element_2_visible} Set Variable ${False}
Should not be True ${element_1_visible} or ${element_2_visible}
Should not be True ${element_1_visible}==True or ${element_2_visible}==True