Clicking on item after changing ChildIndex value

Ask general questions here.
zoework
Posts: 38
Joined: Tue Jan 27, 2015 10:44 pm
Location: Charlotte, NC, USA

Clicking on item after changing ChildIndex value

Post by zoework » Fri Jun 05, 2015 5:12 pm

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
Thanks,
Zoë (aka zoework)

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Clicking on item after changing ChildIndex value

Post by krstcs » Fri Jun 05, 2015 6:24 pm

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.
Shortcuts usually aren't...

zoework
Posts: 38
Joined: Tue Jan 27, 2015 10:44 pm
Location: Charlotte, NC, USA

Re: Clicking on item after changing ChildIndex value

Post by zoework » Fri Jun 05, 2015 6:33 pm

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.
Thanks,
Zoë (aka zoework)

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Clicking on item after changing ChildIndex value

Post by krstcs » Fri Jun 05, 2015 7:41 pm

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!
Shortcuts usually aren't...

zoework
Posts: 38
Joined: Tue Jan 27, 2015 10:44 pm
Location: Charlotte, NC, USA

Re: Clicking on item after changing ChildIndex value

Post by zoework » Fri Jun 05, 2015 7:51 pm

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
Thanks,
Zoë (aka zoework)

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Clicking on item after changing ChildIndex value

Post by Support Team » Mon Jun 08, 2015 5:59 pm

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
You do not have the required permissions to view the files attached to this post.

zoework
Posts: 38
Joined: Tue Jan 27, 2015 10:44 pm
Location: Charlotte, NC, USA

Re: Clicking on item after changing ChildIndex value

Post by zoework » Mon Jun 08, 2015 6:33 pm

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
Thanks,
Zoë (aka zoework)

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Clicking on item after changing ChildIndex value

Post by Support Team » Tue Jun 09, 2015 3:27 pm

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