Self-Solve Knowledge Search

Minimize Maximize
How to select random checkbox value in TruClient protocol
Title :
How to select random checkbox value in TruClient protocol
Document ID :
KM01788615
Product - Version:
loadrunner ;
OS :
Updated :
Wed Jan 18 11:30:19 GMT 2017
Summary :
Instructions are provided how a random element can be selected in TruClient protocol in VuGen.

How to select random checkbox value in TruClient protocol?

Solution
1. Record a few steps clicking on different radio button.
2. Open the steps and change the ID Method to JavaScript in the step object section.
The code will be for example:  
evalXPath("(//input[@type=\"radio\" and @name=\"outFlight\"])[1]");
evalXPath("(//input[@type=\"radio\" and @name=\"outFlight\"])[2]");
evalXPath("(//input[@type=\"radio\" and @name=\"outFlight\"])[3]");
The number in the brackets at the end indicates the number of radio button which was selected for example [3] is the third one.
 3. The next step is to select a random number from the available checkboxes.
The formula which can be uses is: Math.floor(Math.random() * (max - min)) + min;
It returns a random integer between min (included) and max (excluded)
4. Write the code in the JavaScript field
var randomID = Math.floor(Math.random() * (4 - 1)) + 1;
//evalXPath("(//input[@type=\"radio\" and @name=\"outFlight\"])[3]");
var xpath = "(//input[@type=\"radio\" and @name=\"outFlight\"])["+randomID+"]";
evalXPath(xpath);
5. If the number of checkboxes is changing use JavaScript to count their number
 
  1. select all elements meeting your criteria
    var radioArray = document.querySelectorAll('input[type="radio"]');
  2. Save the number of the selected radio buttons
    var max = radioArray.length;
  3. Use the max parameter in the code calculating the randomID.

image text