Re: LR TruClient Selecting a Sequential objects from a flex grid or Table . (1717 Views)
Reply
Occasional Collector
klfalb
Posts: 4
Registered: ‎12-26-2012
Message 1 of 9 (1,877 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.

 

Occasional Visitor
JumboGeng
Posts: 1
Registered: ‎12-26-2012
Message 2 of 9 (1,862 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.

Valued Contributor
sdef
Posts: 94
Registered: ‎08-28-2012
Message 3 of 9 (1,841 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.

Occasional Collector
klfalb
Posts: 4
Registered: ‎12-26-2012
Message 4 of 9 (1,827 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.

Occasional Collector
klfalb
Posts: 4
Registered: ‎12-26-2012
Message 5 of 9 (1,821 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");

*/

Advisor
manas123
Posts: 14
Registered: ‎03-27-2013
Message 6 of 9 (1,717 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");

*/


 

Occasional Advisor
ChDinesh
Posts: 6
Registered: ‎08-28-2013
Message 7 of 9 (578 Views)

Re: LR TruClient Selecting a Random Radio Button

Dear Friend,

 

im looking for  solution and guidelines for selecting multiple links using Ajax Tru client.

 

As you said you solved this issue for multiple link selection, it would be helpful if you could provide me the steps to be follwoed for handling the multiple links,

 

Thanks in advance.

 

 

 

Occasional Collector
klfalb
Posts: 4
Registered: ‎12-26-2012
Message 8 of 9 (566 Views)

Re: LR TruClient Selecting a Random Radio Button

Selecting a random link is essentially the same as selecting a random radio button.  

 

1.  Determine the xpath of the objects in question

example:  

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

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

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

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

 

2.  Determine the part of the xpath that needs to be randomized

in this case:  tr[2]

 

3. Change the selection method to evaluate xpath

 

4. Randomize a variable

write the code to select a random object

 

var options;

options = (Math.floor(Math.random9- *5)2);     where 5 is the max and 2 is the min

 

 

5. Put the randomized variable into the xpath

 

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

Esteemed Contributor
James Leatherman
Posts: 543
Registered: ‎06-05-2008
Message 9 of 9 (53 Views)

Re: LR TruClient Selecting a Random Radio Button

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf

 

"JavaScript calls the valueOf method to convert an object to a primitive value. You rarely need to invoke the valueOf method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected."

 

Therefore:

 

5. Put the randomized variable into the xpath

 

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

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.