Test Data Generator - JavaScript Methods

The following functions are available to generate synthetic test data. When you parameterize your tests, you can assign these functions as variable values instead of using static hard-coded values. For related functions, see also Test Data Generator Functions and Test Data Generator - Seed Lists.

Built-in JavaScript Methods

For advanced users, the parameter definition supports a subset of common JavaScript prototype methods. Prior knowledge of JavaScript is required.

Think of the following scenario: This parameter definition generates an email address like Joe.Smith@example.com:

randFromSeedlist("firstnames") + "." + randFromSeedlist("lastnames") + "@example.com"

Some first names and last names, however, contain special characters such as whitespace which are not allowed in email addresses. The following example shows how to replace all whitespaces by a period:

( randFromSeedlist("firstnames") + "." + randFromSeedlist("lastnames") + "@example.com" ).replace(/ /g,".")

You see how the use of the .replace() method ensures that multi-name email addresses such as Mark.Graca.Maria.Fuego@example.com are generated correctly.

The following table shows the list of supported JavaScript methods.

JavaScript Method Description Examples

All static properties and static methods from Math.

Offers math constants, exponents and logarithms, angle functions, min/max, floor/ceiling, and more. See also Numerical and Mathematical Functions.
${radians} / (Math.PI / 180)
Math.SQRT2
Math.trunc(42.84)
Math.floor(5.95)
Math.sin(1)

All static properties from Number.

Lets you inspect JavaScript constants and limits.
Number.MAX_VALUE
Number.MIN_VALUE
Number.POSITIVE_INFINITY

isNaN()

Tells you whether a value is not a number.
isNaN(${var}) ? 0 : ${var}+3

parseInt()
parseFloat()

Converts strings to numbers.
parseInt("123")

parseFloat("83.468")

encodeURI()
decodeURI()
decodeURIComponent()
encodeURIComponent()

Converts URLs and paths to escape special characters.
encodeURI("/my path/some file")

normalize()

Converts a Unicode string into a (human readable) string in Unicode Normalization Form.
normalize('\u0041\u006d\u00e9')
toLocaleLowerCase()
toLocaleUpperCase()
Changes the capitalization of a string, respecting the given locale.
'İstanbul'.toLocaleLowerCase('tr')

replace()

Searches and replaces strings or regular expressions in strings, and returns a new string.

"Joe Moe Doe".replace(/ /g,".")
"Joe Doe".replace("Joe","John")

repeat()

Repeats a string a number of times.
"Cheers! ".repeat(3)

padEnd()
padStart()

Adds characters before or after a string up to the specified length.
"123".padStart(5, '0')
"abc".padEnd(6, 'x')

concat()

Conjoins two strings with the given character.
"hello".concat(' ', "world")

substring()
slice()

Returns the substring between two indices. Slice() also supports negative indices for counting backwards from the end.
"Chrome 80.0.123".substring(7)
"Chrome 80.0.123".substring(7,9)
"Chrome 80.0.123".slice(-8)
"Chrome 80.0.123".slice(7,9)

charAt()
charCodeAt()
codePointAt()

Returns one character, or UTF-16 code, or Unicode code point found at the index position.
"abc".charAt(1)
"abc".charCodeAt(1)
'☃★♲'.codePointAt(1)

search()
indexOf()
lastIndexOf()

Tells you the index where a regular expression or substring first matches a string. Returns -1 if no match was found.
"hello world".search(/[\s]+/g)
"hello world".indexOf("o")
"hello world".lastIndexOf("o")
test() Tells you whether a regular expression matches a string.
RegExp('foo*').test('fooo')
startsWith()
endsWith()
includes()
Tells you whether a string contains a substring.
"abcdefg".startsWith("ab")
"abcdefg".includes("cde")
"abcdefg".endsWith("fg")
localeCompare() Returns 0 if the strings are the same, returns 1 if the second string is earlier in the sort order, and -1 if it's lower.
"m".localeCompare("m")
"z".localeCompare("a")
"a".localeCompare("z")

Indices start counting at 0.

For more information on method arguments, see https://developer.mozilla.org/en-US/docs/Web/javascript.

Regular Expressions

Typically, you use literal search terms: For example, searching for "high-school" misses all instances of "high school". Regular expressions (RegEx), however, are a smart way to describe search text patterns. With a RegEx pattern you can look for "either 'high-school' or 'high school', but not 'high. School'".

The JavaScript methods test(), search(), and replace() support regular expressions as arguments. Surround the RegEx with two slashes. Symbols such as *,+,?, \ have special meanings.

  • "hello world".search(/[\s]+/g)
    Tell me the index of the first one or more spaces in the text "hello world".
  • RegExp('foo*').test('fooo')
    Tell me whether "fooo" matches the pattern "the word 'fo' followed by zero or more o's".
  • "Joe Moe Doe".replace(/ /g,".")
    Give me a new string with the same text, but all spaces are replaced by periods.

For more information on regular expression syntax, including links to online RegEx testers, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

The following table shows some examples of regular expressions.

RegEx Example Meaning
/abc/ "abc"
"labcoat"
Literally these characters in that order
/ab*c/

"abc"
"abbbbbbbc"
"ac"

One a, then maybe some bs, then one c.
  • * repeats the preceding character zero or more times
/ab+c/

"abc"
"abbbbbbbc"

One a, then some bs, then one c.
  • + repeats the preceding character one or more times
/sha[la]+lee/ "shalalee"
"shalalalalalalee"
One sha, then some la's, then one lee.
  • Use brackets to apply a repetition symbol to the preceding group.
/^Introduction$/ "Introduction"

Just this pattern on a line by itself.

  • ^ stands for the beginning of a line.
  • $ stands for the end of a line.
/abc/g

"Learning my abc's wearing a labcoat"

Search globally, don't stop searching after the first.
/abc/i

"ABC"
"AbC"
"abc"

Case-insensitive search
/\w+\s/g

"a "
"sample "

Search any words followed by one space.
  • \w stands for any alphanumeric Latin character including underscore
  • + repeats the preceding character one or more times
  • \s stands for any space or tab character.
/\d{3}-\d{2,4}/

273-0379
925-221
444-10

Exactly 3 digits, a hyphen, then 2 to 4 digits.
  • \d stands for any digit
  • {n} repeats the preceding character exactly n times.
  • {n,m} repeats the preceding character at leastn and at most m times.

/ab\*c/

/ab\\c/

"ab*c"

"ab\c"

First a, b, then literally this symbol, then one c.

  • Use a backslash to take the following special character literally.

/.ie/

"pie"
"lie"
"tie"...

Any letter followed by the given text "ie".

  • . stands for any one character.