Robotframework String Operator IN - robotframework

How can i check (short code) if some string contain another in Robotframework?
(like "IN" in Python)
Like this is working:
${aaax}= set variable aaa aa ba baavaa
${aaaxx}= set variable aaa aba baavaa
${aba}= set variable aba
${res1}= run keyword and return status should contain ${aaax} ${aba}
${res2}= run keyword and return status should contain ${aaaxx} ${aba}
log to console ${EMPTY}
log to console res1: ${res1}
log to console res2: ${res2}
Anyone have a better solution? like "${aba}" IN "${aaax}" or something like that working?

One way is to use the Evaluate keyword from the BuiltIn library to simply use the Python in operator.
*** Variables ***
${aaax} aaa aa ba baavaa
${aaaxx} aaa aba baavaa'
${aba} aba
*** Test Cases ***
String Contains
${res1}= Evaluate $aaax in $aba
${res2}= Evaluate $aba in $aaaxx
Log To Console ${res1}
Log To Console ${res2}

You can use also Set Variable If keyword combining it with in:
${res1}= Set Variable If $aba in $aaax True False
${res2}= Set Variable If $aba in $aaaxx True False

Related

Fetching a variable inside a list in ROBOT Framework

Suppose the below is my response body
body={
"message": {
"Code": 26181,
"rollnos": [
{
"ID": 1439173,
"date_input": "2022-01-31 14:09:30.206748",
}
],
"start_date": "2022-01-31 00:00:00",
}
}
I want to set the ID value in a variable.
I have tried the below but it did not work out.
${json_response}= set variable ${xyz.json()}
${temp}= Set Variable ${json_response['message']}
${value_1}= Set Variable ${temp['rollnos']}
${register_id} = Set Variable ${value_1["ID"]
Can someone please help where I went wrong in this.
If you have body already as a dictionary you can use only the last 4 lines from the test example. I have included also the transformation from string to dictionary in case you need it:
*** Settings ***
Library Collections
Resource robot/resources/environment_resources.robot
*** Variables ***
${body_temp} {"message": {"Code": "26181", "rollnos": [{"ID": "1439173", "date_input": "2022-01-31 14:09:30.206748"}],"start_date": "2022-01-31 00:00:00"}}
*** Keywords ***
Converting a JSON File
${body} evaluate json.loads($body_temp) json
[Return] ${body}
*** Test Cases ***
Example
# Get body as dict
${body}= converting a json file
# Get the ID
${message} = Get From Dictionary ${body} message
${rollnos} = Get From Dictionary ${message} rollnos
${id} = Get From Dictionary ${rollnos}[0] ID
# Log the ID
Log To Console ID:${id}
Can someone please help where I went wrong in this.
You are treating ${temp['rollnos']} as a dictionary, but it is a list of dictionaries.
Instead of this:
${value_1}= Set Variable ${temp['rollnos']}
${register_id} = Set Variable ${value_1["ID"]
Do this, to get the ID of the first item in the dictionary:
${value_1}= Set Variable ${temp['rollnos'][0]}
# ^^^
${register_id} = Set Variable ${value_1["ID"]}
Well, when you say:
but it did not work out.
You don't mention what were the error messages.
This is my interpretation of your code:
${json_response}= set variable ${xyz.json()}
# ${json_response} is now a dictionary with content: body=&{message}
# So the next step would be:
${temp}= Set Variable ${json_response['body']['message']}
# And now we have: &{temp} = { Code:26181, &{rollnos}, ...
# The next code will be correct
${value_1}= Set Variable ${temp['rollnos']}
${register_id} = Set Variable ${value_1['ID']
But you got the value of ID, and you wanted to Set it, so that is another question.

Netmiko: How to search in switch using a variable

I am not getting any output if I am doing this.
mac_address=abcd
output=net_connect.send_command('show mac-address-table | inc mac_address')
print("Output of the switch ",output)
I am getting the desired output if I am doing this.
output=net_connect.send_command('show mac-address-table | inc abcd')
print("Output of the switch ",output)
What should I make change in the code so that I can use variable?
The closing quote in the first example is after mac_address, making that literal text, not a variable. I'm not sure how you append two strings, but something like:
output=net_connect.send_command('show mac-address-table | inc '+mac_address)
where the + is appending the literal string and the variable string.

Check if string is a number

================
| Person |
|--------------|
|- id : String |
|--------------|
================
I have class Person with property id that is String type. I have to check that id is a number that contains 11 digits. I thinking about something like this:
context Person::id:String
inv: self.id->forAll(l|l.oclIsTypeOf(Integer))
and
self.id.size() = 11
but I have feeling that is not correct.
EDIT.
Now im sure it's not correct,
l.oclIsTypeOf(Integer) always return false, because is oclIsTypeOf should be only called on OclAny, when id is a String type.
EDIT 2. (Solution)
I solved it like this:
context Person::id:String
inv: not self.id.toInteger().oclIsInvalid()
and
self.id.size() = 11
Solution provided by Vincent Aranega below should works too
There is not so much methods on String, but the toInteger one can help you there. It returns the Integer value of a String or Invalid if the string cannot be converted to an Integer. So:
context Person::id:String
inv: not self.id.toInteger().oclIsUndefined()
and self.id.size() = 11
should do the trick! (tested with success in Acceleo)

multiple condition check in for loop using robot framework

I am trying to check multiple conditions in for loop using robot framework but it never returns true.
:FOR ${RowIndex} IN RANGE 0 ${rowscount}
${ColumnText1} Get Text //*[#id='RadSearchGrid_ctl00__${RowIndex}']/td[3]
${ColumnText2} Get Text //*[#id='RadSearchGrid_ctl00__${RowIndex}']/td[4]
${ColumnText3} Get Text //*[#id='RadSearchGrid_ctl00__${RowIndex}']/td[5]
${bStatus} | Run Keywords | Should Contain | ${ColumnText1} and ${ColumnText2} and ${ColumnText3} | ${VoucherNumber} and ${Voucherdate} and ${VoucherAmount}
Exit For Loop If ${bStatus}
${bStatus} Never returns true.
Try something like this
:FOR ${RowIndex} IN RANGE 0 ${rowscount}
${ColumnText1} Get Text //*[#id='RadSearchGrid_ctl00__${RowIndex}']/td[3]
${ColumnText2} Get Text //*[#id='RadSearchGrid_ctl00__${RowIndex}']/td[4]
${ColumnText3} Get Text //*[#id='RadSearchGrid_ctl00__${RowIndex}']/td[5]
${bStatus}= Run Keyword And Return Status Run Keywords Should Contain ${ColumnText1} ${VoucherNumber} AND Should Contain ${ColumnText2} ${Voucherdate} AND Should Contain ${ColumnText3} ${VoucherAmount}
Exit For Loop If ${bStatus}

Add string value to list

My web AUT has about 17 line of text in a table. I have already get each value to a temp variable. And now I want to add that string to the list.
I am getting the following error:
AttributeError: 'str' object has no attribute 'insert'
For example, I have the following text line:
Text Line 1
Text Line 2
Text Line 3
...
And I want to add them to the list like this:
#{mylist} = Text Line 1 | Text Line 2 | Text Line 3
Here is my code, in Robot Framework format:
#{list} Create List ${EMPTY}
${list position} Set Variable 0
${number of row} Get Matching Xpath Count //table[#class="GridView"]//tr
${i} Set Variable 2
: FOR ${i} IN RANGE 2 ${number of row}
${i} Convert To String ${i}
${item control} Replace String ${table profile name default value} rownumber ${i}
${item name} Get Text ${item control}
Append To List #{list} ${item name}
This is the problem line:
Append To List #{list} ${item name}
The problem is the use of #. You need to use $:
Append To List ${list} ${item name}
(you also seem to have the problem that you only have a single space between the last two arguments)
Using $ refers to the list as an object; using # expands the list as if you had typed them into individual cells in the test.

Resources