Skip to content

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 a script is used to generate the array.
  • count: Specifies the number of items to generate in the array. This is required unless a script 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 override type and count.

Example 1: Static Array of Strings¶

This example shows how to generate an array of strings, where the items are explicitly listed.

1
<array name="fruits" type="string" count="3" script="['Apple', 'Banana', 'Cherry']"/>

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
<array name="numbers" type="int" count="5"/>

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
<array name="random_numbers" script="[random.randint(1, 100) for _ in range(5)]"/>

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
<array name="random_floats" script="[random.uniform(0.0, 10.0) for _ in range(3)]"/>

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
<array name="boolean_values" type="bool" count="3" script="[True, False, True]"/>

Dynamic Boolean Array (using randomness):

1
<array name="random_boolean_values" script="[random.choice([True, False]) for _ in range(4)]"/>

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
<array name="uuid_list" script="[uuid.uuid4().hex for _ in range(5)]"/>

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
<array name="fake_names" script="[fake.name() for _ in range(5)]"/>

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
<nestedKey name="user_data">
    <array name="user_ids" script="[random.randint(1, 1000) for _ in range(3)]"/>
    <array name="user_roles" script="['admin', 'user', 'guest']"/>
</nestedKey>

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
<array name="conditional_values" script="[i if i % 2 == 0 else 'odd' for i in range(10)]"/>

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
<list name="employees">
    <item>
        <key name="name" constant="John Doe"/>
        <array name="skills" script="['Python', 'Java', 'SQL']"/>
    </item>
    <item>
        <key name="name" constant="Jane Doe"/>
        <array name="skills" script="['C++', 'JavaScript', 'HTML']"/>
    </item>
</list>

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
<array name="product_list" script="[{'id': i, 'name': f'Product {i}', 'price': random.uniform(10, 100)} for i in range(5)]"/>

This will generate an array of 5 products, each represented as a dictionary with id, name, and price.

Best Practices for Using Arrays¶

  1. Use Scripts for Dynamic Content: Whenever you need flexibility in generating data, use the script attribute to dynamically create array content.
  2. 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.
  3. Combine with Other Elements: Arrays are powerful when combined with <list>, <key>, and <nestedKey> elements to create nested and hierarchical data structures.
  4. Leverage External Libraries: Utilize external libraries such as random, uuid, and fake to add variability and complexity to your generated arrays.