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

Visual Studio Integration

This example illustrates how to use Ranorex 2 within a simple Visual Studio C# console application. It shows how to create a new Visual Studio C# console application and how to start and automate the Windows Calculator.

Note: The sample works with both Microsoft Visual Studio 2005 and 2008.

Create a new Visual Studio project

Start Microsoft Visual Studio. From the 'File' menu click 'New Project' to open the New Project Dialog. In this example we use C# as programming language. Choose another language if you'd like to implement Ranorex code in VB.NET or Visual C++.

Create a new Console Application with Microsoft Visual Studio

Add Ranorex.Core as reference

Right-click the 'References' folder within the projects 'Solution Explorer' and open the 'Add Reference' dialog. Select the components “System.Drawing” and “Ranorex Core”.

Add new references ...
... select System.Drawing and Ranorex.Core

Write some Ranorex automation code

Open the file 'Program.cs' and add following 'using' statement to your existing using section:

C#

using System.Drawing;
using Ranorex;

VB.NET

Imports System.Drawing
Imports Ranorex

python

import System.Drawing
import Ranorex
Mark the main thread with the attribute [STAThread] and change the return value of the Main function to int.

C#

[STAThread]
static int Main(string[] args)

VB.NET

<STAThread> _
Public Shared Function Main(args As String()) As Integer

Python

def Main(self, args):
Add the following code lines to the 'Main' routine of the class 'Program':

C#

int error = 0;

try
{
    System.Diagnostics.Process.Start("calc.exe");
    Form form = Host.Local.FindChild<Ranorex.Form>("Calculator");
    form.Activate();

    Button button = form.FindChild<Ranorex.Button>("2");
    button.Click();

    button = form.FindChild<Ranorex.Button>("*");
    button.Click();

    button = form.FindChild<Ranorex.Button>("3");
    button.Click();

    button = form.FindChild<Ranorex.Button>("=");
    button.Click();

}
catch (RanorexException e)
{
    Console.WriteLine(e.ToString());
    error = -1;
}

return error;

VB.NET

Dim returnError As Integer = 0

Try
  System.Diagnostics.Process.Start("calc.exe")
  Dim form As Form = Host.Local.FindChild(Of Ranorex.Form)("Calculator")
  form.Activate()

  Dim button As Button = form.FindChild(Of Ranorex.Button)("2")
  button.Click()

  button = form.FindChild(Of Ranorex.Button)("*")
  button.Click()

  button = form.FindChild(Of Ranorex.Button)("3")
  button.Click()

  button = form.FindChild(Of Ranorex.Button)("=")

  button.Click()
Catch e As RanorexException
  Console.WriteLine(e.ToString())
  returnError = -1
End Try
Return returnError

Python

error = 0
try:
	System.Diagnostics.Process.Start("calc.exe")
	form = Form(Host.Local.FindChild("Calculator"))
	form.Activate()

	button = Button(form.FindChild("2"))
	button.Click()

	button = Button(form.FindChild("*"))
	button.Click()

	button = Button(form.FindChild("3"))
	button.Click()

	button = Button(form.FindChild("="))
	button.Click()

except RanorexException, e:
	Console.WriteLine(e.ToString())
	error = -1
finally:

return error
Build and start the application by pressing F5.