Information:

Environment

  • Dynatrace Recorder

 

 

 

Solution

In tests provisioned from Dynatrace Recorder scripts, you may sometimes need to enter a random number in a text field or select a random option from a drop-down list, as opposed to using the static values recorded in the script. 


The JavaScript library has several functions that make this possible. You can use this JavaScript in a Custom action. 

There are many ways to create a random number using JavaScript. Below are two methods which are commonly used in scripts.

Method One 

The first method creates a random integer within a given time range. This method is used when you want to randomize a value that is submitted or selected. Use this JavaScript.

var randNum = Math.round(Math.random() * MAXVAL);

Replace MAXVAL with the maximum value your random integer can be. This line will set the randNum variable to any integer between 0 and your MAXVAL. 

If you need a minimum value (denoted as MINVAL) as well, the JavaScript should look like:

var randNum = Math.round(Math.random() * (MAXVAL - MINVAL)) + MINVAL;


Method Two 

The second method of creating a random number uses the current timestamp. This method is frequently used when a unique number is needed every time.

Use this JavaScript:

 

 

 

var hour = d.getHours();
var minute = d.getMinutes();
var month = d.getMonth();//This will return the month number between 0 (January) and 11 (December)
var year_4digit = d.getFullYear();
var year_2digit = d.getYear();
var seconds = d.getSeconds();
var day = d.getDay(); // This will return the weekday number between 0 (Sunday) and 6 (Saturday)

 

 

 

 

 

  • No labels