Dynamic Data Generation
Have you ever wanted to dynamically create data as you write a script? Wished you had more data to work with? This post is all about using dynamic elements in scripts, data generation, and dynamic variable creation.
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 machine to draw data from as you test. This post will cover both of these scenarios.
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.
“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:
The logged number will be any number between 1 and 20, randomly selected by the script at run time. For a more in-depth example using this approach, please see Dynamically Naming and Creating a File.
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 can either be “.com”, ”.net”, or “.org”.
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.
Now, 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 system. To see the full script, click here.
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.
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.
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.