array Element¶
The <array>
element defines arrays of data items that can be either statically defined or generated dynamically using scripts. Arrays are essential when you need to generate multiple items of the same data type, such as lists of values, and can be combined with other elements like <list>
, <key>
, and <nestedKey>
to create complex data structures.
Attributes¶
name
: Specifies the name of the array. This is mandatory and will be the key for the array in the generated output.type
: Specifies the data type of items in the array (e.g.,string
,int
,float
,bool
). This is mandatory unless ascript
is used to generate the array.count
: Specifies the number of items to generate in the array. This is required unless ascript
is used to dynamically generate the array content.script
: An optional attribute that allows for generating the array's content dynamically using Python-like expressions. This can be used to overridetype
andcount
.
Example 1: Static Array of Strings¶
This example shows how to generate an array of strings, where the items are explicitly listed.
1 |
|
In this case, the fruits
array will contain three static string values: ['Apple', 'Banana', 'Cherry']
.
Example 2: Static Array of Integers¶
Here’s an array of integers generated using the type
and count
attributes.
1 |
|
The numbers
array will contain five integers, starting from a default value and increasing sequentially based on the data generation logic within the framework.
Example 3: Dynamic Array of Random Integers¶
You can use a script to generate an array of random integers.
1 |
|
This random_numbers
array will contain five random integers between 1 and 100.
Example 4: Dynamic Array of Random Floats¶
Similarly, you can generate an array of floating-point numbers using a script.
1 |
|
This will produce three random floating-point numbers between 0.0 and 10.0.
Example 5: Array of Boolean Values¶
You can create an array of boolean values, either statically or dynamically.
Static Boolean Array:
1 |
|
Dynamic Boolean Array (using randomness):
1 |
|
This dynamically generates four boolean values chosen randomly between True
and False
.
Example 6: Array of UUIDs¶
Generating unique identifiers (UUIDs) dynamically can be useful in scenarios requiring unique keys.
1 |
|
This will generate an array of 5 unique UUIDs.
Example 7: Array of Fake Names¶
By leveraging the fake
library in scripts, you can generate an array of random names.
1 |
|
This will create an array of 5 random names.
Example 8: Nested Arrays with Dictionaries¶
You can nest arrays inside dictionaries using the <nestedKey>
element, which allows more complex structures.
1 2 3 4 |
|
In this example, the user_data
key will contain two arrays: user_ids
(a dynamic array of random integers) and user_roles
(a static array of role names).
Example 9: Arrays with Conditional Logic¶
You can use conditions inside the array script to generate dynamic values based on certain criteria.
1 |
|
This generates an array where even numbers are included as integers, and odd numbers are replaced with the string 'odd'
.
Example 10: Arrays Within a List¶
You can also generate arrays inside <list>
elements to represent more complex data.
1 2 3 4 5 6 7 8 9 10 |
|
In this example, the employees
list contains two items, each with an array of skills
.
Example 11: Arrays of Custom Objects¶
You can generate arrays that contain custom objects or dictionaries using dynamic scripts.
1 |
|
This will generate an array of 5 products, each represented as a dictionary with id
, name
, and price
.
Best Practices for Using Arrays¶
- Use Scripts for Dynamic Content: Whenever you need flexibility in generating data, use the
script
attribute to dynamically create array content. - Type Matching: Ensure that the
type
attribute matches the type of data being generated, especially when not using a script. For scripts, make sure the generated data matches your expected type. - Combine with Other Elements: Arrays are powerful when combined with
<list>
,<key>
, and<nestedKey>
elements to create nested and hierarchical data structures. - Leverage External Libraries: Utilize external libraries such as
random
,uuid
, andfake
to add variability and complexity to your generated arrays.