Visual Studio integration

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.

Note icon


The sample in this chapter works with with Microsoft Visual Studio 2005 and later.

In this chapter

    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.

    Creating a new Console Application with Microsoft Visual Studio

    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:


    using Ranorex;
    using System;
    using System.Runtime.CompilerServices;
    using System.Threading;
    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.

    static void Main(string[] args)
    private static void InitResolver()
    private static void RanorexInit()
    private static int run()
        int error = 0;
        //Start calculator and wait for UI to be loaded
            System.Diagnostics.Process pr = System.Diagnostics.Process.Start("calc.exe");
            //Get process name
            string processName = GetActualCalculatorProcessName();
            //Find Calculator | Windows 10
            if (IsWindows10())
                WindowsApp calculator = Host.Local.FindSingle("winapp[@processname='" + processName + "']");
                Button button = calculator.FindSingle(".//button[@automationid='num2Button']");
                button = calculator.FindSingle(".//button[@automationid='plusButton']");
                button = calculator.FindSingle(".//button[@automationid='num3Button']");
                button = calculator.FindSingle(".//button[@automationid='equalButton']");
                //Close calculator
    ().Close(); } //Find Calculator | Windows 8.X or older else { Form calculator = Host.Local.FindSingle("form[@processname='" + processName + "']"); calculator.EnsureVisible(); Button button = calculator.FindSingle(".//button[@controlid='132']"); button.Click(); button = calculator.FindSingle(".//button[@controlid='92']"); button.Click(); button = calculator.FindSingle(".//button[@controlid='133']"); button.Click(); button = calculator.FindSingle(".//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.

    static int Main(string[] args) 
    int error = 0;  
        System.Diagnostics.Process pr = System.Diagnostics.Process.Start("calc.exe");  
        Form form = Host.Local.FindSingle ("form[@processname='"+pr.ProcessName+"']");  
        Button button = form.FindSingle(".//button[@controlid='132']");  
        button = form.FindSingle(".//button[@controlid='92']");  
        button = form.FindSingle(".//button[@controlid='133']");  
        button = form.FindSingle(".//button[@controlid='121']");  
    catch (RanorexException e)  
        error = -1;  
    return error;  
    Public Shared Function Main(args As String()) As Integer  
    Dim returnError As Integer = 0  
      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 & "']")  
      Dim button As Button = form.FindSingle(Of Ranorex.Button)(".//button[@controlid='132']")  
      button = form.FindSingle(Of Ranorex.Button)(".//button[@controlid='92']")  
      button = form.FindSingle(Of Ranorex.Button)(".//button[@controlid='133']")  
      button = form.FindSingle(Of Ranorex.Button)(".//button[@controlid='121']")  
    Catch e As RanorexException  
      returnError = -1  
    End Try  
    Return returnError