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 (EPF) 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