Data Manipulation Tricks

We present you some tricks that you can use to solve common problems while writing tests.

Unique emails

If you want to generate a unique random email you can incorporate the current epoch in it. Epochs progress every millisecond so it’s pretty improbable to have a duplicate! The D language extension (see here for more details about this extension) provides date manipulation utilities, so:

<string>${D.nowMillis()}@gmail.com

if ‘string’ is ‘apifortress’ the result will be something like: apifortress1489051395378@gmail.com

String manipulation

If a data item is a composite string of values and you need to evaluate portions of it, there are numerous ways you can do it.

Let’s say we have this string “55,5,84,65,26,7,82” and you want to work with each number. The simpler way is transforming the string into an array. You can do using the ‘split’ function:

<variable_name>.split('<separator>')

In the example above, if the string is included in a variable named ‘randomNumbers’ writingĀ randomNumbers.split(‘,’) you produce an array like [55,5,84,65,26,7,82]. At this point you can work with it as you generally do when you have an array.

Another trick you can use to improve your tests is extracting something from a string using the ‘substring’ functionality. If you need to extract the first n characters from a string there is a function that helps you: take(n).

Let’s consider the following string: “whatever” saved in a variable named “substring_variable”, if you:

substring_variable.take(4)

“what” will be returned.

Another function for a string is drop(n) that removes the first n characters and return the resulting String. Considering the same string and variableĀ used as example for take(n), if you:

 substring_variable.drop(4)

“ever” will return.

The last example is extracting a substring using index ranges. If we have the string “wherever” by doing:

wherever[1..4]

we will obtain “here”

Dates manipulation

Maybe your endpoint requires a date in a specific format as parameter.

${D.format(D.nowMillis(),'MM/dd/YY')}

This expression will generate today date (e.g. 03/09/17)