In API test automation, a common requirement is to extract data from one API response to use in a subsequent API request (a practice known as API chaining) or for complex data validation. ACCELQ provides a powerful and layered set of commands to handle data extraction from any REST API response.
The general workflow is a two-step process:
- Fetch the raw data from the API response (e.g., the entire body, a specific header).
- Parse the fetched data using format-specific commands (e.g., JSON, XML, or plain text) to pinpoint and retrieve the exact value you need.
Step 1: Fetching Raw Response Data
First, you need to get the general component of the response where your target data resides. ACCELQ provides clear commands for this purpose. The output of these commands is typically stored in a variable for the next step.
Get ReST Response: Use this command to retrieve the entire body of the API response as a string. This is the most common starting point.Get ReST Response Header: Use this to fetch the value of a specific header field from the response (e.g.,Authorization,Content-Type).Get Status Code From ReST Response: Use this to get the numerical status code (e.g.,200,404) of the response.
Step 2: Parsing and Extracting Specific Values
Once you have the raw response body stored in a variable, you can use ACCELQ's specialized automation libraries to parse it based on its format.
Handling JSON Responses
For API responses in JSON format, use the JSON Automation commands. You'll use a JSONPath expression to navigate the JSON structure and locate the data.
Key Extraction Commands:
Get JSON Node value: This is the primary command for extraction. You provide the JSON string (from Step 1) and aJSONPathexpression (e.g.,$.data.user.id) to get the value of that specific node.Get JSON Array size: Use this command to find out how many elements are in a JSON array.
Example Workflow:
- Use
Get ReST Responseto get the JSON body into a variable, say jsonResponse. - Use
Get JSON Node valuewith jsonResponse as input and theJSONPath$.tokento extract an authentication token into a new variable, authToken. - Use the authToken variable in the header of the next API call.
Handling XML Responses
For legacy systems or SOAP web services that return XML, use the XML Automation commands. These commands use XPath expressions to navigate the XML document tree.
Key Extraction Commands:
Get XML Node Value: The main command to get the content of an XML element or attribute. You provide the XML string and anXPathexpression (e.g.,//order/id).Get XML Node Count: Counts how many nodes match a givenXPath, useful for verifying the number of items.
Example Workflow:
- Use
Get ReST Responseto get the XML body into a variable, xmlResponse. - Use
Get XML Node Valuewith xmlResponse as input and theXPath//customer/@idto extract the customer ID into a variable, customerId.
Handling Plain Text or Other Formats
If the response is not a structured JSON or XML file but plain text, CSV, or another custom format, you can still process it.
- Use
Get ReST Responseto fetch the body into a variable. - Use ACCELQ's built-in String Utilities library to parse the text. You can use commands to:
- Find text using regular expressions.
- Split the string by a delimiter.
- Get a substring between two points.
Comments
0 comments
Please sign in to leave a comment.