Random object choosing

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

Random object choosing

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


I have an issue I need to find a solution to: I need to click on a random directory :
Random directory
ScreenShot005.jpg (550.36 KiB) Viewed 1134 times
and afterwards click on a random file:
Random file
ScreenShot006.jpg (305.34 KiB) Viewed 1134 times
How can I choose a random object with ranorex?


Ranorex Guru
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...

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;