How can I start a variable index containing a number? robotframework - robotframework

I need to start my variable ${i} containing int 1, to utilize the variable in xpath.
FOR ${i} IN RANGE 1 ${row}
${SKU} Get Text xpath://*[#id="tblList"]/tbody/tr[${i}]/td[1]
Input Text ${Devolucoes.addEtiqueta} 000000${SKU}0000
Click Element ${botoes.btnAdicionar}
END

You can assign int values to variables by putting the int inside the brackets like:
${i} Set Variable ${1}
or
${i} Set Variable ${10}

Related

How to identify if a value is integer or number?

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')

How to replace the single quote ( ' ) into double quotes ( " ) in Robot Framework?

I have a List, items are created by append by a Loop. I want to use this list as a json. The problem is that the items of that list use the single quote so it can't be the json.
Get Order Items
[Tags] Get Order Items
Set Headers ${HEADER CUSTOMER}
GET ${ORDER GET BY CODE ENDPOINT}/${ORDER CODE}
Integer response status 200
${NUMBER OF ITEMS} Output $.number_of_items
${NUMBER OF ITEMS} Evaluate ${NUMBER OF ITEMS} + 1
${ORDER ITEMS} Create List
:FOR ${i} IN RANGE 1 ${NUMBER OF ITEMS}
\ Append To List ${ORDER ITEMS} ${ORDER CODE}${i}
Set Global Variable ${ORDER ITEMS}
Actual result: ['N19072596HB1', 'N19072596HB2', 'N19072596HB3', 'N19072596HB4', 'N19072596HB5']
Expected result: ["N19072596HB1", "N19072596HB2", "N19072596HB3", "N19072596HB4", "N19072596HB5"]
This: ['N19072596HB1', 'N19072596HB2', 'N19072596HB3', 'N19072596HB4', 'N19072596HB5'] , is python's string representation of a list, and they have picked to use single quotes for it.
As your end goal is to use the double-quoted version in a json, the best bet is to use the python's json library to convert it for you, instead of replacing single with double quotes:
${list as string}= Evaluate json.dumps($ORDER_ITEMS) json
(note how the variable is not surrounded by curly brackets - thus you're passing the actual variable to the method, not its value)
Why not to use a simple string replacement?
Because you don't want to deal with cases where a list member may have a quote, single or double - like ['N19072596HB1', "N1907'2596HB2", 'N19072596HB1', 'N19072"596HB2'].
A trivial string replace will fail miserably, while the json library is designed to handle these cases as expected.

Place a condition on a for loop in robot framework

I have the for loop below and I am trying to place a condition for it:
: FOR ${i} IN RANGE ${size}
\ Validate Item List ${items[${i}]}
so that the for runs only if the condition is met.
I tried "Run keyword if", but that does not seem to work:
Run keyword if ${flag}>0 : FOR ${i} IN RANGE ${size}
... \ Validate Item List ${items[${i}]}
I get "No keyword with name ': FOR' found.".
Note: flag can be zero or negative.
'Run Keyword If' keyword cannot be used directly for "For-Loop".
For-loop statements should be mentioned inside an user defined keyword and then 'Run Keyword If' should be mentioned as below:
User Defined function for For Loop
: FOR ${i} IN RANGE ${size}
\ Validate Item List ${items[${i}]}
Run Keyword If ${flag}>0 User Defined function for For Loop
From code perspective I can imagine two ways of doing the same:
${size} = Run Keyword If "${flag}">"0" Get Size #Whatever you want here
... ELSE Set Variable 0 #0 in order to make the loop not looping at all
: FOR ${i} IN RANGE ${size}
\ Validate Item List ${items[${i}]}
Or alternatively you can do following:
: FOR ${i} IN RANGE ${size}
\ Exit For Loop If "${flag}">"0"
\ Validate Item List ${items[${i}]}

Run keyword if to append to a string ... ${query_string}= catenate ${query_string} AND

I want to append to a variable if dictionary size is greater than 1
${queryString}= startOfString
Run keyword if ${dictionary_size} > 1
... ${query_string}= catenate ${query_string} restofString
However the only if statement i can see in Robot is the above. Obviously variable assignment isnt a keyword. Is there another way of doing this so i would end up with
startOfString restofString
Set Variable If is your friend here.
${queryString}= Set Variable startOfString
&{dict}= Create Dictionary foo=bar
${dictLen}= Get Length ${dict}
${queryString}= Set Variable If ${dictLen} > 1 ${queryString} restofString ${queryString}
If the start of the query is static:
&{dict}= Create Dictionary foo=bar zaz=lop
${dictLen}= Get Length ${dict}
${queryString}= Set Variable If ${dictLen} > 1 startOfString restofString startOfString

How can we use "Set Variable if" with a keyword where Keyword is returning a variable RobotFramework

How can we use Set Variable if with a keyword, where Keyword is returning a variable that needs to be Set in RobotFramework.
Eg: ${Var} = set variable if ${i}==10 Keyword.
Actually, the easiest way to do it is by using Run Keyword If instead of Set Variable If like below:
Foo
${ret}= Run Keyword If ${i} == 10 Keyword Which Return Something
Should Be Equal ${ret} something
Keyword Which Return Something
${var}= Set Variable something
[Return] ${var}
One way to do this would be the usage of "Run keyword if" with "set test variable" eg.
*** Test cases ***
foo
Run keyword if ${i} == 10 kw that sets test variables
should be equal ${var} HELLO
*** keywords ***
kw that sets test variables
set test variable ${var} HELLO
The way I would do it is:
*** Test Cases ***
Test Case Title
${passed} = run keyword and return status
... Should be equal ${i} 10
${var} = set variable if ${passed} It is today
Another Test Example
${var} = set variable if ${i}==10 It is today
*** Keywords ***
It is today
${today} = Get Current Date UTC result_format=%-d-%-m-%Y exclude_millis=true
[Return] ${today}
Read further in the documentation
here and here.
Just store the value from Keyword in a temp variable:
${temp} = | Keyword | Param1 | Param2 | .....
${Var} = | ${i} == 10 | ${temp}
If i is 10, ${Var} will be set to the return of Keyword.

Resources