English|Deutsch
Subscribe Ranorex Announcements Feed Ranorex LinkedIn Ranorex twitter Ranorex Facebook

Visual Studio 2

This sample works with both Visual Studio 2003 and Visual Studio 2005.

The RanorexVS2005Sample2 illustrates how to use Ranorex from a simple Visual Studio C# console application.
The sample shows how to create a new Visual Studio C# console application and how to start and automate a simple windows GUI application with RanorexNet.

Create, build and run the sample

Start Microsoft Visual Studio, from the 'File' menu click 'New Project' and select a 'Console Application'.

Right-click the 'References' folder within the projects 'Solution Explorer' and open the 'Add Reference' dialog. Browse for the 'RanorexNet.dll' within the Ranorex installation path. Also add the 'System.Drawing' library from the '.Net' tab.

The libraries 'RanorexNet' and 'System.Drawing' are added to the 'References' folder.
Open the file 'Program.cs'.
Add the following lines at the using region.
using System.Drawing;
using Ranorex;
Mark the main thread with the attribute [STAThread]
[STAThread]
static int Main(string[] args)
We use COM in RanorexCore and COM Interop in RanorexNet, so we need a SingleThreadedAppartement.
If you create a WindowsApplication, Visual Studio creates this attribute for you automatically. Insert following code lines into the 'main' routine of the class 'Program':
C#
Application.SleepTime = 100;
Mouse.MoveTime = 400;
 
String startApplication = "VS2005Application";
if (  args.Length>1 && args[0] == "-a" )
	startApplication = args[1];
 
//-------------------------------------------------------------------------------------
// F O R M   search and test
//-------------------------------------------------------------------------------------
Form form = Application.FindFormTitle("RanorexTestedApp");
if ( form == null )
{	
	Console.WriteLine("Start {0}",startApplication);
	Application.Start(startApplication);
	form = Application.FindFormTitle("RanorexTestedApp",SearchMatchMode.MatchExact,true,5000);
	if ( form == null )
	{
		Console .WriteLine("ERROR: starting application \"{0}\"", startApplication); 
		return -1;
	}
}
 
Console.WriteLine("Application found");
//-------------------------------------------------------------------------------------
// Print location and size of the form
//-------------------------------------------------------------------------------------
Point location = form.Location;
Console.WriteLine("Form location X={0} Y={1}", location.X, location.Y);
Size size = form.Size;
Console.WriteLine("Form size Width={0} Height={1}\n", size.Width, size.Height);
 
//-------------------------------------------------------------------------------------
// B U T T O N   Test
//-------------------------------------------------------------------------------------
Button button = form.FindButton("button1");
if ( button == null )
{
	Console.WriteLine("ERROR: button1 not found ");
	return -1;
}
 
Console.WriteLine("Button \"button1\" found");
// Move the mouse to the control
//Mouse.MoveToControl(button);
Mouse.MoveToElement(button.Element);
button.Focus();
location	= button.Location;
size		= button.Size;
string text = button.Text;
Console.WriteLine("  Location X={0} Y={1}", location.X, location.Y);
Console.WriteLine("  Width={0} Height={1}", size.Width, size.Height);
Console.WriteLine("  Text={0}", text);
Console.WriteLine("  Button hide");
button.Hide();
bool visible = button.Visible;
Console.WriteLine("  Visible flag = {0}", visible);
Console.WriteLine("  Button show");
button.Show();
visible = button.Visible;
Console.WriteLine("  Visible flag = {0}", visible);
Console.WriteLine("  Click the button");
button.Click();
 
//Console.WriteLine("\nPress any key to exit the application");
//string inText = Console.ReadLine();
Console .WriteLine("Closing application"); 
Application .Sleep(2000); 
form. Close (); 			
Console.WriteLine("\nTEST PASSED\n");
 
return 0;

Important: Make sure that 'RanorexCore.dll' and 'RanorexSpy.dll' exist at your project output path:

Setup you project settings by selecting the menu item 'Project' | 'Properties'.

Open the 'Build' tab and set the 'Output Path' to the installation path of Ranorex ('bin' directory):
Build and run the sample.

Building and Running the Sample from the Ranorex Samples directory

  1. Open the project file 'RanorexVS2005Sample2.csproj' from the Ranorex installation path ('Sample' directory).
  2. Rebuild the project using the menu item 'Build'
  3. Run the Ranorex sample with F5.