Page 1 of 1

Clicking on item after changing ChildIndex value

Posted: Fri Jun 05, 2015 5:12 pm
by zoework
Hi,
Here's my problem. I have several multiple-choice questions with up to 5 answers each. I want to randomly select one of the answers on each run. The definition for the item is
//div[#'questionSection']/div[4]/div[1]/div[4]/?/?/ul/li[@childindex='1']
How can I change the childindex value randomly and then click on the item? I've tried using variables in the repository definition, but that only clicks on the item with the default value of the variable.
Thanks,
Zoe

Re: Clicking on item after changing ChildIndex value

Posted: Fri Jun 05, 2015 6:24 pm
by krstcs
You will need to use UserCode to do this.

Keep the variable in the XPath, but in UserCode, BEFORE you try to click the element, set the value of the variable to the random number.

Note: The Random object built into .NET only gives a random number if you use the .Next(<min value>, <max value>) method, otherwise it will pass the same seed number each time you call it.

Code: Select all

Random r = new Random();

myVariable = r.Next(1, 5);
I would also suggest adding a Report.Info() right after this so you can output the actual number used into the report for historical data keeping.

Re: Clicking on item after changing ChildIndex value

Posted: Fri Jun 05, 2015 6:33 pm
by zoework
I tried this and I call this method at the beginning of the script, but it is not updating the value in the repository. I'm getting random values with this code:
Random rnd = new Random();
int varAnswer1 = rnd.Next(1, 6);

My repository item is defined as //div[#'questionSection']/div[4]/div[1]/div[4]/?/?/ul/li[@childindex=$varAnswer1]
The mouse click statement on this item either does not do anything or it uses the default value for $varAnswer1 which is 1.

Re: Clicking on item after changing ChildIndex value

Posted: Fri Jun 05, 2015 7:41 pm
by krstcs
Can you post a Ranorex Snapshot of the element and post it here? If you can't post it, you can send it to [email protected].

Without seeing the element it's going to be hard to know exactly how to address the XPath.

I use run-time generated indexes like this a lot (like when the values inside a row that you would normally use to ID the row are going to change, like a quantity, for instance) and they work fine if the back-end code and XPath are setup correctly, so I'm pretty sure it isn't a bug or something like that.

Hmmm... :D


Make sure that you are setting the value immediately before using it. And you have to set the Ranorex Variable's value, not a user-code variable.

Also, if you can post a sample solution that demonstrates the issue it might help us figure out what's going on.

Sorry I'm not more help! If you have any questions, please ask!

Re: Clicking on item after changing ChildIndex value

Posted: Fri Jun 05, 2015 7:51 pm
by zoework
I am setting all variables at the beginning of the script. Let me change that to do it before each click. I tried to attach the snapshot, but got a 'bad request' response from the browser. I will email it to support.
Thanks,
Zoe

Re: Clicking on item after changing ChildIndex value

Posted: Mon Jun 08, 2015 5:59 pm
by Support Team
Update: After doing a remote session with Zoe, we were able to click on a random item by making sure that the repository variable was being set with the string representation of the random integer. It's important also to make sure that the local variable name, is not the same as the repository variable. From the code mentioned above:(varAnswer1: repo variable)

Code: Select all

Random rnd = new Random();
int rndNum= rnd.Next(1,6);
varAnswer1 =rndNum.ToString();
I have attached the snapshot file here for reference.

Regards,
Bassem

Re: Clicking on item after changing ChildIndex value

Posted: Mon Jun 08, 2015 6:33 pm
by zoework
Thank you for this Bassem. I changed the code to assign the variables correctly as strings so that they can be used in the repository. However, it looks like I still have to do it right before each of the individual questions. It doesn't work when I assign it at the beginning of the test. Just an FYI.
Regards,
Zoe

Re: Clicking on item after changing ChildIndex value

Posted: Tue Jun 09, 2015 3:27 pm
by Support Team
Update:
We were able to set the variables at the start of the test. Previously the code to set the variables wasn't actually executing.
Regards,
Bassem