Microsoft Visual Studio is an integrated development environment (IDE) by Microsoft. It is used to develop computer programs for Microsoft Windows, as well as websites, web apps, web services, and mobile apps. This chapter introduces and explains how to use Ranorex Studio in 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.
The sample in this chapter works with Microsoft Visual Studio 2005 and later.
Create a new Visual Studio project
-
Start Microsoft Visual Studio.
-
Go to File > New Project.
-
Select .NET Framework 4.5.2 or higher (excluding .NET Core) and choose your programming language. We will be using Visual C# in this example.
-
Select Console Application from the list.
-
Enter a name for the project and click OK.
Add Ranorex core assemblies as references
-
In the project’s Solution Explorer, right-click the References folder and select Add Reference… .
-
Select Browse in the menu on the left.
-
Browse to the Bin folder of your Ranorex installation (default: C:\Program Files (x86)Ranorex Bin).
-
Add Ranorex.Bootstrapper, Ranorex.Common, Ranorex.Core, Ranorex.Core.Resolver, and all Ranorex.Plugin assemblies.
Write some Ranorex automation code
Before you start writing code, we recommend you set the Copy Local option to False for all Ranorex assemblies except for Ranorex.Core.Resolver. This assembly must always be set to True. You can leave Copy Local enabled for other assemblies if your solution requires it.
Now, to prepare the example in this section, open the file ‘Program.cs’ and add the following ‘using’ statement to your existing using section:
C#
using Ranorex;
using System;
using System.Runtime.CompilerServices;
using System.Threading
VB.NET
Imports Ranorex
Imports System;
Imports System.Runtime.CompilerServices;
Imports System.Threading;
It is crucial that the Ranorex.Core.Resolver assembly is initialized before any other Ranorex core functionalities are used. It is the assembly that finds all other Ranorex assemblies at runtime.
To do so, add the following code to the Main routine of the class Program. Your test automation code goes in the Run section. In our example, we perform a simple calculation in the Windows calculator. Once you’ve added the code, press F5 to build and start your project.
C#
static void Main(string[] args)
{
InitResolver();
RanorexInit();
run();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void InitResolver()
{
Ranorex.Core.Resolver.AssemblyLoader.Initialize();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void RanorexInit()
{
TestingBootstrapper.SetupCore();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static int run()
{
int error = 0;
//Start calculator and wait for UI to be loaded
try
{
System.Diagnostics.Process pr = System.Diagnostics.Process.Start("calc.exe");
Thread.Sleep(2000);
//Get process name
string processName = GetActualCalculatorProcessName();
//Find Calculator | Windows 10
if (IsWindows10())
{
WindowsApp calculator = Host.Local.FindSingle<WindowsApp>("winapp[@processname='" + processName + "']");
Button button = calculator.FindSingle<Ranorex.Button>(".//button[@automationid='num2Button']");
button.Click();
button = calculator.FindSingle<Ranorex.Button>(".//button[@automationid='plusButton']");
button.Click();
button = calculator.FindSingle<Ranorex.Button>(".//button[@automationid='num3Button']");
button.Click();
button = calculator.FindSingle<Ranorex.Button>(".//button[@automationid='equalButton']");
button.Click();
//Close calculator
calculator.As<Form>().Close();
}
//Find Calculator | Windows 8.X or older
else
{
Form calculator = Host.Local.FindSingle<Form>("form[@processname='" + processName + "']");
calculator.EnsureVisible();
Button button = calculator.FindSingle<Ranorex.Button>(".//button[@controlid='132']");
button.Click();
button = calculator.FindSingle<Ranorex.Button>(".//button[@controlid='92']");
button.Click();
button = calculator.FindSingle<Ranorex.Button>(".//button[@controlid='133']");
button.Click();
button = calculator.FindSingle<Ranorex.Button>(".//button[@controlid='121']");
button.Click();
//Close calculator
calculator.Close();
}
}
catch (RanorexException e)
{
Console.WriteLine(e.ToString());
error = -1;
}
return error;
}
private static string GetActualCalculatorProcessName()
{
string processName = String.Empty;
var processes = System.Diagnostics.Process.GetProcesses();
foreach (var item in processes)
{
if (item.ProcessName.ToLowerInvariant().Contains("calc"))
{
processName = item.ProcessName;
break;
}
}
return processName;
}
private static bool IsWindows10()
{
var reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftWindows NTCurrentVersion");
string productName = (string)reg.GetValue("ProductName");
return productName.StartsWith("Windows 10");
}
For reference purposes, this is what the code looked like for Ranorex versions before 8.0.
C#
[STAThread]
static int Main(string[] args)
int error = 0;
try
{
System.Diagnostics.Process pr = System.Diagnostics.Process.Start("calc.exe");
Form form = Host.Local.FindSingle <ranorex.form>("form[@processname='"+pr.ProcessName+"']");
form.Activate();
Button button = form.FindSingle<ranorex.button>(".//button[@controlid='132']");
button.Click();
button = form.FindSingle<ranorex.button>(".//button[@controlid='92']");
button.Click();
button = form.FindSingle<ranorex.button>(".//button[@controlid='133']");
button.Click();
button = form.FindSingle<ranorex.button>(".//button[@controlid='121']");
button.Click();
}
catch (RanorexException e)
{
Console.WriteLine(e.ToString());
error = -1;
}
return error;
}
VB.NET
<stathread> _
Public Shared Function Main(args As String()) As Integer
Dim returnError As Integer = 0
Try
Dim pr As System.Diagnostics.Process = System.Diagnostics.Process.Start("calc.exe")
Dim form As Form = Host.Local.FindSingle(Of Ranorex.Form)("form[@processname='" & pr.ProcessName & "']")
form.Activate()
Dim button As Button = form.FindSingle(Of Ranorex.Button)(".//button[@controlid='132']")
button.Click()
button = form.FindSingle(Of Ranorex.Button)(".//button[@controlid='92']")
button.Click()
button = form.FindSingle(Of Ranorex.Button)(".//button[@controlid='133']")
button.Click()
button = form.FindSingle(Of Ranorex.Button)(".//button[@controlid='121']")
button.Click()
Catch e As RanorexException
Console.WriteLine(e.ToString())
returnError = -1
End Try
Return returnError