Load JSON File into Robot Framework - robotframework

I am trying to load a JSON file and use the values to perform some actions based on my tests. I tried to load the json value which I think I got right, but when trying to log the output, I got error message:
Resolving variable '${qa["REQUEST_ID"]}' failed: TypeError: list indices must be integers or slices, not str
Not exactly sure what this means since I am new to Robot Framework. This is what I did to load and log the values:
${file} Get File ${CURDIR}/RequestIDs.json
${qa} Evaluate json.loads('''${file}''') json
Log To Console ${qa["REQUEST_ID"]}
Json file looks something like:
[
{
"REQUEST_ID" : 10513
},
{
"REQUEST_ID" : 48156
},
{
"REQUEST_ID" : 455131
}
]
So basically I want to get the "REQUEST_ID" value and type that in a text field.

Look at the structure of your json - it's a list of dictionaries; so you have to first specify which list member you want, and then its REQUEST_ID field:
Log To Console ${qa[0]["REQUEST_ID"]
# print the value from all present dictionaries in the list:
FOR ${member} IN #{qa}
Log To Console ${member["REQUEST_ID"]
END

Related

How to get first item from array in Robot Framework

I have the following response from a POST request:
{"facilities":[{"id":"f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4","description":"luovbfvwofgdrcwvqtyqohjioocszgplcjh","hasAnyPartnership":false,"hasAnyProcedure":false}
So I used the "Convert String to JSON" function and got the following response:
{'facilities': [{'id': 'f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4',
'description': 'luovbfvwofgdrcwvqtyqohjioocszgplcjh',
'hasAnyPartnership': False, 'hasAnyProcedure': False}
How do I get the ID value that is inside FACILITIES?
'facilities': [{'id': 'f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4'
The JSON example you have provided is not the valid one. It is missing ] of facilities array and } of opening external brace. After correction it should look like this -
{"facilities":[{"id":"f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4","description":"luovbfvwofgdrcwvqtyqohjioocszgplcjh","hasAnyPartnership":false,"hasAnyProcedure":false}]}
You can use following keywords from JSONLibrary
${json}= Convert String to JSON ${JsonVar}
${idValue}= Get Value From Json ${json} $.facilities[0].id
Output -

How to deserialize the JSON array?

I am trying to add artifacts to VM in DevTest Lab and i want to pass the artifacts name dynamically.
Below is my parameter
"Artifacts": {
"type": "array"
},
And in Resource section am calling this as
"artifacts":["[parameters('Artifacts')]"]
Am calling this ARM as below from powershell file
$sampleJob += Start-Job -Name $fileName -FilePath $scriptlocation -ArgumentList $artifact
$artifact is defined as object type Object[]
When running the script am getting the following error.
"message": "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Microsoft.DevTestLab.VirtualMachine.Data.Models.Rest.ArtifactInstallProperties' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.
How to fix the above error. Any help can be appreciated. Thank you.
In the Resource section, remove the outer pair of square-brackets. It should look like:
"artifacts": "[parameters('Artifacts')]"

Postman: data retrieved from an input file cannot be logged in console

I am using Postman for Windows Version 6.5.2.
Whenever I use an input file with variables, I would like to see variables I use in current test case printed out to console. For example I have a data file with list of user id:s. Then, at some point in my tests, I would like to send a simple message to console: "INFO: logging with user id XXXX."
I have tried assigning data to both environment and global variables. It seems not working neither in "Pre-request Script" section, neither in "Tests" section. In my case, if I have an initial value defined, then this value is being printed out all way around (despite that Postman takes different values from file for each iteration). If no value is defined (tested with both environment and global ones), then I get an empty string printed out.
Using the console.log(pm.iterationData.toObject()) statement in the Tests tab would log an object, containing the data from the file used in the request.
My sample JSON data file:
[
{
"item":"1",
"item2": "Value 1"
},
{
"item":"2",
"item2": "Value 2"
},
{
"item":"3",
"item2": "Value 3"
},
{
"item":"4",
"item2": "Value 4"
}
]
This would log the following, when running the Collection from the Runner:

Conversion from an .avsc file to a Java file using Maven

I created a .avsc file and I declared the following field: { "name": "event_time", "type": { "type" : "long", "logicalType": "timestamp-millis"}, "doc": "The timestamp when the event was registered." }
When the package instruction from maven is executed, the following field is created: private DateTime event_time;
Then, the conversion from DateTime to Long is realized using the following code:
this.event_time = (DateTime)value$;
This line of code throws an exception when it is executed because the Long object cannot be converted into a DateTime object in this manner.
I would like to know if it is possible to rewrite the class which is generated from the .avsc file using the command package. This class is in the target folder.
Otherwise I would like to know what other possible options exist for sending a timestamp using AVRO.

Getting Error when query mongodb with AND and OR Condition in Robot Framework

In my project i need to query from mongodb with the query having AND and OR, but i am getting error.
below is my code:
*** Variables ***
${host} mongodb://username:password#192.10.23.126/RegressionDB
${port} 27017
${mongDBName} RegressionDB
${mongoCollection} Service
${mQuery} { "service.offerings.offering.offeringType.masterCode": "Plan", $or: [ { "service.offerings.offering.offeringSubType.masterCode": "WS" }, { $and: [ { "service.isDeliveryRequired": "N" } ] } ] }
${mReturnFields} service.customerCode
${isReturnID} False
*** Test Cases ***
Query from MongoDatabase
Connect To MongoDB ${host} ${port}
${result}= Retrieve Mongodb Records With Desired Fields ${mongDBName} ${mongoCollection} ${mQuery} ${mReturnFields} ${isReturnID}
I got the following error:
ValueError: Expecting property name enclosed in double quotes: line 1 column 58 (char 57)
The same query i executed using MongoChef it worked fine, but not working with robot framework, what can be the reason.
The issue here is not related to AND and OR, I guess it has to do with setting properties! when you are setting properties, the values have to be in double quotes. Since it works for normal query and only throws this error for and/or so can you try add and/or in double quotes!
That should fix it!

Resources