Robot Framework, often in conjunction with libraries like RequestsLibrary for API testing, offers good support for working with JSON and XML data. Here's how you can handle these data formats:
Working with JSON:
Parsing JSON:
Using Evaluate (for simple cases): For very basic JSON structures, you can use the Evaluate keyword with Python's built-in json library.
***Variables***
${json_string} Set Variable {"name": "John", "age": 30}
***Test Cases***
Parse JSON
${data} Evaluate json.loads('${json_string}') json
Log Name: ${data}[name]
Log Age: ${data}[age]
Using Convert To Dictionary (from Collections library): This is a better approach for more complex JSON and integrates well with Robot Framework's variable handling.
***Variables***
${json_string} Set Variable {"name": "John", "age": 30, "address": {"city": "New York"}}
***Test Cases***
Parse JSON
${data} Convert To Dictionary ${json_string}
Log Name: ${data}[name]
Log City: ${data}[address][city]
Creating JSON:
Using Evaluate: You can construct JSON strings using Python's json.dumps() method.
***Test Cases***
Create JSON
${data} Evaluate {"name": "Jane", "city": "London"} json
${json_string} Evaluate json.dumps(${data}) json
Log ${json_string}
Working with JSON in API Testing (using RequestsLibrary):
Sending JSON data in requests:
***Settings***
Library RequestsLibrary
***Test Cases***
Send JSON Request
Create Session my_session https://api.example.com
${data} Evaluate {"key1": "value1", "key2": "value2"} json
Post Request my_session /endpoint json=${data}
Handling JSON responses: The RequestsLibrary automatically parses JSON responses.
${response} Get Request my_session /endpoint
${name} Set Variable ${response.json()}[name]
Log Name: ${name}
Working with XML:
Parsing XML:
Using Evaluate and xml.etree.ElementTree (for simple XML): For basic XML, you can use Python's xml.etree.ElementTree library.
***Variables***
${xml_string} Set Variable John30
***Test Cases***
Parse XML
${root} Evaluate xml.etree.ElementTree.fromstring('${xml_string}') xml.etree.ElementTree
${name} Evaluate ${root}.find('name').text xml.etree.ElementTree
Log Name: ${name}
Using XML library (recommended for complex XML): The dedicated XML library provides more powerful and convenient ways to parse and navigate XML structures. You'll need to install it: pip install robotframework-xml
***Settings***
Library XML
***Variables***
${xml_string} Set Variable John30New York
***Test Cases***
Parse XML
${root} Parse XML ${xml_string}
${name} Get Element Text ${root}/person/name
${city} Get Element Text ${root}/city
Log Name: ${name}
Log City: ${city}
#Get all person elements
@{persons} Get Elements ${root}/person
Log Many @{persons}
#Get attribute of an element
${age} Get Element Attribute ${root}/person/age value
Log Age: ${age}
Creating XML:
Using Evaluate and xml.etree.ElementTree: You can construct XML structures using the xml.etree.ElementTree library.
***Test Cases***
Create XML
${root} Evaluate xml.etree.ElementTree.Element('data') xml.etree.ElementTree
${name} Evaluate xml.etree.ElementTree.SubElement(${root}, 'name') xml.etree.ElementTree
${name}.text Set Variable Jane
${xml_string} Evaluate xml.etree.ElementTree.tostring(${root}, encoding='unicode') xml.etree.ElementTree
Log ${xml_string}
Key Considerations:
Collections library and Python's built-in json library are often sufficient. For XML, the XML library or Python's xml.etree.ElementTree are commonly used. For API testing, RequestsLibrary handles JSON well.XML library's methods.TRY/EXCEPT blocks) when parsing or processing JSON and XML data, as these operations can sometimes raise exceptions.