メインコンテンツまでスキップ
バージョン:23.5

Dynamic Data Generation

Have you ever wanted to dynamically create data as you write a script? Wished you had more data to work with?

Often when writing a test, you have been given a set of prescribed data in advance. However, sometimes it can be helpful to create data as you go, so that you can conduct randomized tests or drive your testing from within the script instead of working with an external file.

The applications for dynamic content generation are widely varying. For instance, you can dynamically generate email addresses for testing against a form. Alternately, you can dynamically create a file on your Eggplant Functional machine to draw data from as you test. This post will cover both of these scenarios.

Using Dynamic Elements in Scripts; Data Generation and Dynamic Variable Creation

There are a few approaches to generating data content dynamically within your script. One of these approaches is to use Any. Another is the Random() function. These are going to be the most common approach to randomized testing in your scripts or conducting data-driven tests, and both will be covered in the following examples.

Using Any

Any is a chunk expression that allows you to select a random piece of data from a previously existing list or range. For example, if you store a range of numbers between 1 and 20 in a variable, and then use Any to select one of the numbers, your code might look like this.

Example:

put 1..20 into NumberRange
Log any item of NumberRange

The logged number will be any number between 1 and 20, randomly selected by the script at run time. The following is a more in-depth example using this approach.

Example:

(* This creates an empty file with a random five-character name. This method can be adapted to name files of various types. *)
Put "~/Temp/" into Path -- Specify a file path

put a..z as list into alphas -- Create a range of letters
put 1..5 as list into nums -- Create a range of numbers

put (any item of alphas for each item of nums) joined by empty into Name -- Dynamically create the name using any. Note that this initially creates a list, but then joins the items of the list with empty, creating a string from the list items, and storing that in the variable Name.

put path & Name into filePath -- Store the path and name into a fully assembled file path
put filePath & ".txt" into myFile -- Assemble the file with file extension
if there is not a file myFile then -- Check to make sure the file does not already exist before creating it
create file myFile -- Create the file
log "Created file " & myFile -- Log a message that the file was created, as well as its full path
end if

When you create your data using the Any Command, all of the content is randomly selected, so the data itself is random. As we just established, this randomization is created out of a base of pre-existing information. So, what if you have a variety of different elements, all of which are variable and need to be assembled into a larger data string?

One example of this is dynamic email generation. In this situation, there are four elements to the email address, three of which are varying. There is the first part of the email address, which we will call the ‘name’. Then, there is the “@” symbol, followed by a domain name, which is also dynamic. Finally, there is the ending, which in the example below can either be “.com”, ”.net”, or “.org”.

Example:

insert [".com",".net",".org"] into ListOfEnds
set Part1 to (any item of a..z for each item of 1..6) joined by empty -- Make a random first half
set Part2 to (any item of 1..9 for each item of 1..6) joined by empty -- Make a random second half
set EmailEnd to any item of ListOfEnds -- Choose a random ending
put Part1 & "@" & Part2 & EmailEnd into DynamicAddress -- Compile the email address
Log DynamicAddress -- Log the dynamically created email address

As you can see in the example above, three different parts are chosen randomly using Any, and then concatenated using an ampersand, and stored in a final variable for use later. This could be typed directly into the Username field of a login screen if desired, using TypeText.

Using Random()

This technique can be incorporated into a larger script which dynamically generates an external file of randomized customer data. The script generating random email addresses from above can be turned into a handler, and other handlers can then be created to dynamically generate customer names and ID numbers. In turn, the script can call out to these other handlers to generate the data and then the information returned by these called portions of code can be organized and written out to a file on the local Eggplant Functional system.

// The file is going to contain a column of customer ID numbers, a column of customer names, and a column of email addresses, all dynamically generated during the script run

// Create random file contents

Put 20 into numberofRecords -- Define the number of records you want to create

Repeat numberofRecords times -- Repeat the number of times you want rows in your file

// Create customer ID by calling handler
put createID() & comma into Row

// Create customer name by calling handler
put createName() & comma after Row

// Create customer email by calling handler
put createEmail() after Row

// Add row to file
put row & return after FileContents

end repeat

// Create file and store the created content in it

Put "~/Temp/CustomerEmail.txt" into myFile -- Specify a location on the Eggplant system for the file to be created

put "ID,Name,Email" & return into HeaderRow -- Create a header row for the file

put HeaderRow before FileContents -- Insert the header row before the previously created content

put fileContents into file myFile -- Insert the contents into the file, located on the Eggplant system

(* FUNCTIONS AND HANDLERS GO AT THE BOTTOM OF THE SCRIPT *)

// Create customer ID

to createID
put random(100000,999999) into customerID -- Use the random function to create the numbers for the ID
put "-" after character 2 of customerID -- Put the ID in the correct format by inserting a dash
returncustomerID -- Return the newly created customer ID to the calling handler
end createID

// Generate random names for file

to createName
put a..z into Letters -- Store the alphabet in a variable using a range
put random(5,10) into NewNameLength -- Use the random function to randomly select a name length
repeat NewNameLength times -- Repeat for each character in the new customer name
put any item of Letters after NewName -- Create a new name using random letters
end repeat
return Capitalized(NewName) -- Capitalize the new customer name and return it to the calling handler
end createName

// Dynamically create random email addresses

to createEmail
insert[".com",".net",".org"] into ListOfEnds
set Part1 to (any item of a..z for each item of 1..6) joined by empty -- Make a random first half
set Part2 to (any item of 1..9 for each item of 1..6) joined by empty -- Make a random second half
set EmailEnd to any item of ListOfEnds -- Choose a random ending
put Part1&"@"&Part2&EmailEnd into DynamicAddress -- Compile the email address
Return DynamicAddress -- Return the address to the calling handler
end createEmail

The main body of the script consists of a repeat loop which calls the handlers (these could also be made into separate scripts and called as sub-scripts). This loop will repeat any number of times, as defined by a variable that specifies the number of customer records desired.

Example:

// Specify a location on the Eggplant Functional system for the file to be created
Put "~/Temp/CustomerData.txt" into myFile

// Define the number of records to be created
Put 20 into numberofRecords

// Create random file contents

Repeat numberofRecords times -- Repeat the number of times you want rows in your file

// Create customer ID by calling handler
put createID() & comma into Row

// Create customer name by calling handler
put createName() & comma after Row

// Create customer email by calling handler
put createEmail() after Row

// Add row to file
put row & return after FileContents

end repeat

Both the handler for the Name creation and ID creation use the random() function. The random() function takes either one or two numbers as parameters, and it then generates a number either between 0 and the number specified, or between the two specified numbers.

Example:

// Create Customer ID
to createID
put random(100000,999999) into customerID -- Use the random function to create the numbers for the ID
put "-" after character 2 of customerID -- Put the ID in the correct format by inserting a dash
return customerID -- Return the newly created customer ID to the calling handler
end createID

// Generate random names for file
to createName
put a..z into Letters -- Store the alphabet in a variable using a range
put random(5,10) into NewNameLength -- Use the random function to randomly select a name length
repeat NewNameLength times -- Repeat for each character in the new customer name
put any item of Letters after NewName -- Create a new name using random letters
end repeat
return Capitalized(NewName) -- Capitalize the new customer name and return it to the calling handler
end createName

At the end, after everything has been generated, the created contents are written out to the file, creating the file if it did not previously exist, and overwriting any previously existing data if it did.

Example:

// Create file and store the created content in it
put "ID,Name,Email" & return into HeaderRow -- Create a header row for the file
put HeaderRow before FileContents -- Insert the header row before the previously created content
put fileContents into file myFile -- Insert the contents into the file, located on the Eggplant Functional system