Page 1 of 1

Random object choosing

Posted: Tue Jul 08, 2014 1:59 pm
by Nono
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!

Re: Random object choosing

Posted: Tue Jul 08, 2014 2:21 pm
by krstcs
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]".

Re: Random object choosing

Posted: Wed Jul 09, 2014 2:59 pm
by mzperix
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;