Random object choosing

Class library usage, coding and language questions.
Nono
Posts: 20
Joined: Wed Oct 02, 2013 11:19 am

Random object choosing

Post by Nono » Tue Jul 08, 2014 1:59 pm

Hi,

I have an issue I need to find a solution to: I need to click on a random directory :
ScreenShot005.jpg
and afterwards click on a random file:
ScreenShot006.jpg
How can I choose a random object with ranorex?

Thx!
You do not have the required permissions to view the files attached to this post.

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

Re: Random object choosing

Post by krstcs » Tue Jul 08, 2014 2:21 pm

Ranorex is built on .NET. You can use the .NET Random class to choose a number between 0 and 1 and then multiply by the number of items in the list. This will give you the zero-based index of the folder to pick. Then add 1 (XPath uses 1 based indexing) and store the result in a module variable. You can use that variable in your item's path like ".//element[$FolderIndex]".
Shortcuts usually aren't...

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: Random object choosing

Post by mzperix » Wed Jul 09, 2014 2:59 pm

I would like to add some hint on directories:
First make sure, that the proper dll-s are used:
using System;
using System.IO;
To count the numbers of directories in C:\ directory
int countDir = System.IO.Directory.GetDirectories.("C:\").Length;
To count the files in a directory:
string targetDirectory = "C:\";
string [] fileEntries = Directory.GetFiles(targetDirectory );
int countFiles = filEntries.Length;