LR TruClient Selecting a Random Radio Button (1263 Views)
Reply
Occasional Collector
klfalb
Posts: 3
Registered: ‎12-26-2012
Message 1 of 6 (1,263 Views)

LR TruClient Selecting a Random Radio Button

I am using Ajax TruClient in LoadRunner 11.5.  I am attempting to select randomly from a list of radio buttons.

I have found documentation on selecting random links but that code does not work (random(document.getElementsbyTagName("a"));).  

I have tried the following JavaScript in the Object XPath JavaScript Editor:

 

ID Method is JavaScript

// original x path for one of the radio buttons /html/body/blockquote/form/center/table/tbocy/tr{2}/td[1]/input

//All radio buttons had the same x path where tr was the ordinal used for selecting

//I removed the ordinal for tr in order to get an array of the radio buttons

var options=evalXpath("/html/body/blockquote/form/center/table/tbocy/tr/td[1]/input");

 

var randomelementNumber = Math.floor(Math.random()* options.lenght);

 

I don't know how to get the randomized element into the xpath since it is a string.

 

Please use plain text.
Occasional Visitor
JumboGeng
Posts: 1
Registered: ‎12-26-2012
Message 2 of 6 (1,248 Views)

Re: LR TruClient Selecting a Random Radio Button

[ Edited ]

TC probably use eval() to evaluate user's javascript, so you should make sure that last statement evaluates to the desired value.

Like this:

 // suppose 'options' is the array of radio box controls

var options=evalXPath("/html/body/blockquote/form/center/table/tbocy/tr/td[1]/input");

// create a random number no larger than the array's length

var randomelementNumber = Math.floor(Math.random()* options.length);

// return the randome element

options[randomelementNumber];

 

You can also try this:

// randomly select an element from an array

random(document.getElementsByTagName("a"))

 

hope this solves your problem.

Please use plain text.
Valued Contributor
sdef
Posts: 94
Registered: ‎08-28-2012
Message 3 of 6 (1,227 Views)

Re: LR TruClient Selecting a Random Radio Button

i think the radio buttons have their specific variables  or id which i think you can do parameterization.

try pointing your cursor or try selecting radio buttons and see below the browser if there are ID's or variables assigned to it.

 

Came accross with a situation like this, but in list type not radio button.

Please use plain text.
Occasional Collector
klfalb
Posts: 3
Registered: ‎12-26-2012
Message 4 of 6 (1,213 Views)

Re: LR TruClient Selecting a Random Radio Button

Update to earlier post:

 

The original xpaths grabbed during recording:

//input[@type="radio" and @name="outboundFlight" and value ="562;134;12/20/2012"]

 

html/body/blockquote/form/center/table/tbody/tr[4]/td[1]/input 

 

I have tried the following:

//Here I try randomly selecting with dynamic number of elements 

var options;
var randomelementNumber;

options=document.getElementsByName("outboundFlight");

window.alert("length of options is: " + options);

randomelementNumber = (Math.floor(Math.random() * options.length)+2);

document.getElementByID(randomelementNumber.valueOf()).checked=true;

 

//options kept returning zero so 

//Now I try hardcoding the number of elements

var randomelementNumber;

randomelementNumber = (Math.floor(Math.random() * 3)+2);

document.getElementByID(randomelementNumber.valueOf()).checked=true;

 

//stilll no dice.  The only way I can get a selection is to use the original xpath so I try

random(evalXPath("/html/body/blockquote/form/center/table/tbody/tr/td[1]/input"));

 

//I do not get any errors but the selection never changes.  

//I have opened a ticket with HP.  I will update when I get a solution.

Please use plain text.
Occasional Collector
klfalb
Posts: 3
Registered: ‎12-26-2012
Message 5 of 6 (1,207 Views)

Re: LR TruClient Selecting a Random Radio Button

/*I was able to randomize the radio button selection by working backward.  I tried the exact same code in the past with a different variable name and it did not work.  Some how working backward worked.  I will take it.  I am still not able to have a dynamic random number Max.*/

 

//working code is below

//The Step is "Click"

//Object:ID Method is "JavaScript"

 

var options;
options = (Math.floor(Math.random() * 5)+2);
evalXPath("/html/body/blockquote/form/center/table/tbody/tr["+options.valueOf()+"]/td[1]/input");

 

/*

I started with what I know works

/html/body/blockquote/form/center/table/tbody/tr[5]/td[1]/input

 

Added evalXPath and changed Object:ID Method to "JavaScript"

evalXPath("/html/body/blockquote/form/center/table/tbody/tr[5]/td[1]/input");

 

I added a variable and added it to my XPath

var options;

options = 4;

evalXPath("/html/body/blockquote/form/center/table/tbody/tr["+options.valueOf()+"]/td[1]/input");

 

 

Then I randomized the variable.  

var options;
options = (Math.floor(Math.random() * 5)+2);
evalXPath("/html/body/blockquote/form/center/table/tbody/tr["+options.valueOf()+"]/td[1]/input");

*/

Please use plain text.
Advisor
manas123
Posts: 14
Registered: ‎03-27-2013
Message 6 of 6 (1,103 Views)

Re: LR TruClient Selecting a Sequential objects from a flex grid or Table .

[ Edited ]

 If I need to select the objects from the top to buttom in an array  , by not selecting randomly but sequentially in each iteration . So any suggesion for it .

 

and also need to count the number of rows present on the grid .

 

 

Regards,

Manas


klfalb wrote:

/*I was able to randomize the radio button selection by working backward.  I tried the exact same code in the past with a different variable name and it did not work.  Some how working backward worked.  I will take it.  I am still not able to have a dynamic random number Max.*/

 

//working code is below

//The Step is "Click"

//Object:ID Method is "JavaScript"

 

var options;
options = (Math.floor(Math.random() * 5)+2);
evalXPath("/html/body/blockquote/form/center/table/tbody/tr["+options.valueOf()+"]/td[1]/input");

 

/*

I started with what I know works

/html/body/blockquote/form/center/table/tbody/tr[5]/td[1]/input

 

Added evalXPath and changed Object:ID Method to "JavaScript"

evalXPath("/html/body/blockquote/form/center/table/tbody/tr[5]/td[1]/input");

 

I added a variable and added it to my XPath

var options;

options = 4;

evalXPath("/html/body/blockquote/form/center/table/tbody/tr["+options.valueOf()+"]/td[1]/input");

 

 

Then I randomized the variable.  

var options;
options = (Math.floor(Math.random() * 5)+2);
evalXPath("/html/body/blockquote/form/center/table/tbody/tr["+options.valueOf()+"]/td[1]/input");

*/


 

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation