Often times when writing test logic that involves a set of data elements, you encounter a need to use basic data structures such as Lists and Maps (Collections). This article discusses examples of Collections in the test automation context and some useful commands in the Action logic editor.
Example 1:
Test logic needs to read the Transaction IDs from the screen and at a later point, compare them with records in DB. You can read the text from the table column and store it in a List.
Example 2:
Assume that you need to read the product name and the price for each product from the search results and store them as a set of key/value pairs. You may later want to use this information for downstream validation. You will use a Map to store this information as a collection of records with each record being a key/value pair of Item name and Price.
List Commands
A List is used for storing an array of data items as a collection. For example, a list of Origin airport codes or a list of Passenger names.
Following commands are helpful to store, retrieve and verify contents in a list. You can type "collection" in the logic editor to get a listing of all relevant commands.
Creating List
You can create a list as
- an empty list or
- a list with an initial set of items in it or
- a new list as a clone of an existing list
Create new List (collection)
When you create a list, you assign a handle of this list to a parameter. This parameter (price list in the above example) will be used to refer to this list in all the subsequent commands.
Another common way to create a list is from an existing string literal which is delimited by a character. For example, you may have a comma-separated text of city names, and you want to convert this to a list. You will use the following command:
Split text and assign to list (collection)
Updating List
Following commands are used to update the content of a list. Note that the list handler generated as part of list creation must be used to refer to the list.
- Add item(s) to list (collection): To add one or more items to a given list
- Update item in list (collection): Updates an item at a given index in the list. To update the first item in the list, use '1' as the index.
- Remove item from list (collection): Removes an item at a given index from the list (index starts from 1)
- Sort items in list (collection): This allows you to sort items in the list in either ascending or descending order, based on the specified data type for the items. You can sort the list while considering the items to be either plain text (case sensitive or not), numbers, or dates.
Getting List information
- Get item from list (collection): Returns an item at a specified index from the list. Index starts from 1.
- Is item exists in list (collection): Returns true or false depending on if the given item exists in the list or not. You may use this directly in conditional statements.
- Is list empty (collection): Returns true/false based on the content of the list. You may use this directly in conditional statements.
- Get size of list (collection): Returns the number of items in the list.
- Get index of item from list (collection): Returns the index at which an item is found. Index starts from 1. If the item does not exist in list, -1 is returned.
- Get item occurrence count in list (collection): Returns the number of times a given item appears in the list.
Verifying information in List
- Verify items sort order in list (collection): Verifies that items in the list are in expected sort order. You can consider the items to be either simple text, numbers or dates while performing this verification.
- Verify item exists in list (collection): Verifies that given item exists in the list.
HashMap Commands
HaspMap stores information in key, value pairs. You may store, retrieve and verify the contents of a HashMap using the commands listed below:
Creating a Map
You can create a map as
- an empty map or
- as a clone of an existing map
Create new Map (collection)
When you create a Map, you assign a handle of this Map to a parameter. This parameter (items in the above example) will be used to refer to this Map in all the subsequent commands.
Updating Map
Following commands are used to update the content of a map. Note that the map handle generated as part of map creation must be used to refer to the map.
- Add item(s) to a Map (collection): To add one or more items to a given map. If an item with the key already exists, it is overridden.
- Remove item(s) from Map (collection): Removes an item with a given key or, all items from a map.
Getting Map information
- Get value for a key, from Map (collection): Returns an item value for a given key.
- Is item exists in Map (collection): Returns true or false depending on if an item exists with the given key in the map or not. You may use this directly in conditional statements.
- Is Map empty (collection): Returns true/false based on if the map is empty or not. You may use this directly in conditional statements.
- Get size of Map (collection): Returns the number of items in the map.
- Get all keys from Map (collection): Returns all the keys in the Map as a List. If the Map is empty, returns an empty list.
- Get all values from Map (collection): Returns all the values in the Map as a List. If the Map is empty, returns an empty list.
Verifying information in Map
- Verify item exists in Map (collection): Verifies that given item exists in the map.
Passing List as Action Input
If an Action Input Parameter is expecting a List as input, follow this process in the Test Case modal.
Click on the "..." icon and select "Format List Values"
Enter the list items in separate lines and click "Format & Copy"
Now paste the content from the clipboard into the parameter field you need.
Passing Map as Action Input
If an Action Input Parameter is expecting a Map as input, follow this process in the Test Case modal.
Click on the "..." icon and select "Format Map Values"
Enter the list items in separate lines and click "Format & Copy"
Now paste the content from the clipboard into the parameter field you need.
Comments
0 comments
Please sign in to leave a comment.