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

Code Examples

This Documentation is only for Ranorex 2.x versions.

Click here for the Ranorex 3.x test automation documentation

General

The following code examples describe how to use Ranorex Adapters to automate not only simple controls like buttons or text fields, but also complex controls like tree or list view items. In combination with RanoreXPath the search for GUI elements can be done in many different ways.

The following code section describes two ways of searching applications and their GUI element:

C#

// Call explorer.exe
System.Diagnostics.Process.Start("explorer.exe");
// Search for a program window with 
// title "MyComputer" starting from local host
Form fileExplorer = Host.Local.FindChild<Ranorex.Form>("My Computer");
fileExplorer.Activate();
Button closeButton = fileExplorer.FindDescendant<Ranorex.Button>("Close");
closeButton.Click();

// Search for a program window
// and its close button using the RanoreXPath
Form fileExplorer = "/form[@title='My Computer']";
fileExplorer.Activate();
Button closeButton = fileExplorer.FindSingle<Ranorex.Button>
              ("titlebar/button[@accessiblename='Close']");
closeButton.Click();

VB.NET

' Call explorer.exe
System.Diagnostics.Process.Start("explorer.exe")		
' Search for a program window with
' title "MyComputer" starting from local host
Dim fileExplorer As Form  = Host.Local.FindChild(Of Ranorex.Form)("My Computer")
fileExplorer.Activate() 
Dim closeButton As Button = fileExplorer.FindDescendant(Of Ranorex.Button)("Close")
closeButton.Click()
			
' Search for a program window  
' and its close button using the RanoreXPath  
Dim fileExplorer As Form  = "/form[@title='My Computer']"
fileExplorer.Activate()
Dim closeButton As Button = fileExplorer.FindSingle(Of Ranorex.Button)
			("titlebar/button[@accessiblename='Close']")
closeButton.Click()

Python

# Call explorer.exe
System.Diagnostics.Process.Start("explorer.exe")
# Search for a program window with 
# title "MyComputer" starting from local host
fileExplorer = Form(Host.Local.FindChild("My Computer"))
fileExplorer.Activate()
closeButton = Button(fileExplorer.FindDescendant("Close"))
closeButton.Click()

# Search for a program window
# and its close button using the RanoreXPath
fileExplorer = Form("/form[@title='My Computer']")
fileExplorer.Activate()
closeButton = Button(fileExplorer.FindSingle("titlebar/button[@accessiblename='Close']"))
closeButton.Click()

Button

Following code first finds a Button within the windows calculator. After that the button is clicked if it is enabled.

C#

// Search button directly with RanoreXPath
Button myButton = "/form[@title='Calculator']/button[@text='5']";
// Check if button number 5 is enabled
if (myButton.Enabled)
{
  myButton.Click();
}

VB.NET

' Search button directly with RanoreXPath  
Dim myButton As Ranorex.Button = "/form[@title='Calculator']/button[@text='5']"
' Check if button number 5 is enabled  
If myButton.Enabled Then
  myButton.Click()
End If

Python

# Search button directly with RanoreXPath
myButton = Button("/form[@title='Calculator']/button[@text='5']")
# Check if button number 5 is enabled
if myButton.Enabled:
	myButton.Click()

TextBox

The following example shows different ways of setting the text of a TextBox.

C#

// Search textBox within .NET application
Text textBox = "/form[@controlname='TestedApp'
                    /text[@controlname='textBox1']";
// Set value directly
textBox.TextValue = "myText";
// Select first two characters of the
// text ("my")
textBox.Select(0, 2);

// Set values by simulating a user
// Simulate double click to select existing text
textBox.Click(Location.Center, 2);
Keyboard.Press("myText");

VB.NET

' Search textBox within .NET application  
Dim textBox As Text = "/form[@controlname='TestedApp'/text[@controlname='textBox1']"
' Set value directly  
textBox.TextValue = "myText"
' Select first two characters of the  
' text ("my")  
textBox.[Select](0, 2)

' Set values by simulating a user  
' Simulate double click to select existing text  
textBox.Click(Location.Center, 2)
Keyboard.Press("myText")

Python

# Search textBox within .NET application
textBox = Text("/form[@controlname='TestedApp'/text[@controlname='textBox1']")
# Set value directly
textBox.TextValue = "myText"
# Select first two characters of the
# text ("my")
textBox.Select(0, 2)

# Set values by simulating a user
# Simulate double click to select existing text
textBox.Click(Location.Center, 2)
Keyboard.Press("myText")

CheckBox

This code finds a CheckBox by its control name, reads its state and sets the checked state to true.

C#

// Search for checkbox within .NET application
CheckBox checkBox = "/form[@controlname='TestedApp']
                /checkbox[@controlname='checkBox1']";
// Read the current state of a checkbox                
Console.WriteLine(checkBox.Text + " State: " +checkBox.CheckState.ToString());
// Check if the checkbox is not checked
if (!checkBox.Checked)
  checkBox.Checked = true;

VB.NET

' Search for checkbox within .NET application  
Dim checkBox As CheckBox = "/form[@controlname='TestedApp']/checkbox[@controlname='checkBox1']"
' Read the current state of a checkbox                  
Console.WriteLine(checkBox.Text & " State: " & checkBox.CheckState.ToString())
' Check if the checkbox is not checked  
If Not checkBox.Checked Then
  checkBox.Checked = True
End If

Python

# Search for checkbox within .NET application
checkBox = CheckBox("/form[@controlname='TestedApp']/checkbox[@controlname='checkBox1']")
# Read the current state of a checkbox
Console.WriteLine(checkBox.Text + " State: " + checkBox.CheckState.ToString())
# Check if the checkbox is not checked
if not checkBox.Checked:
	checkBox.Checked = True

ComboBox

This example describes how to automate a combo box.

C#

// Search for combo box within .NET application  
Ranorex.ComboBox comboBox = "/form[@controlname='TestedApp']
                         /combobox[@controlname='comboBox1']";
// Open combobox using property
comboBox.DropDownVisible = true;

// Open combobox by clicking the
// drop down button
Button open = comboBox.FindChild<Ranorex.Button>("Open");
open.Click();

// Select list item with text "Black"
// from DropDown list
ListItem listItem = comboBox.FindSingle<ListItem>
               ("list/listitem[@accessiblename='Black']");
listItem.Click();

VB.NET

' Search for combo box within .NET application    
Dim comboBox As Ranorex.ComboBox = "/form[@controlname='TestedApp']/combobox[@controlname='comboBox1']"
' Open combobox using property  
comboBox.DropDownVisible = True

' Open combobox by clicking the  
' drop down button  
Dim open As Button = comboBox.FindChild(Of Ranorex.Button)("Open")
open.Click()

' Select list item with text "Black"  
' from DropDown list  
Dim listItem As ListItem = comboBox.FindSingle(Of ListItem)("list/listitem[@accessiblename='Black']")
listItem.Click()

Python

# Search for combo box within .NET application
comboBox = ComboBox("/form[@controlname='TestedApp']/combobox[@controlname='comboBox1']")
# Open combobox using property
comboBox.DropDownVisible = True

# Open combobox by clicking the
# drop down button
open = Button(comboBox.FindChild("Open"))
open.Click()

# Select list item with text "Black"
# from DropDown list
listItem = ListItem(comboBox.FindSingle("list/listitem[@accessiblename='Black']"))
listItem.Click()

TreeView

The following code iterates through a Win32 tree control well known from windows file explorer using the TreeView class.
Folder options dialog
Warning dialog which has to be handled during test

C#

Form optionsDialog = "/form[@title='Folder Options']";
Tree settingsTree = optionsDialog.FindSingle<Ranorex.Tree>
                        ("container[@caption='View']/tree[@controlid='30120']");
foreach (TreeItem item in settingsTree.Items)
{
  IterateTree(item);
}

static void IterateTree(Ranorex.TreeItem treeItem)
{
   foreach (Ranorex.TreeItem item in treeItem.Items)
   {
     item.Click();                
     CheckForWarningDialog();

     Thread.Sleep(100);
     if (item.Items.Count > 0)
     {
       IterateTree(item);
     }

     item.Click();
     CheckForWarningDialog();                
  }
}

// Some mouse clicks causes a warning dialog to popup
// which has to be handled and closed
static void CheckForWarningDialog()
{
   Ranorex.Button button=null;
   if (Host.Local.TryFindSingle("/form[@title='Warning']/button[@text='&No']", 1000, out button))
   	  button.Click();
}

VB.NET

Dim optionsDialog As Form = "/form[@title='Folder Options']"
Dim settingsTree As Tree = optionsDialog.FindSingle(Of Ranorex.Tree)("container[@caption='View']/tree[@controlid='30120']")
For Each item As TreeItem In settingsTree.Items
  IterateTree(item)
Next


Private Shared Sub IterateTree(treeItem As Ranorex.TreeItem)
  For Each item As Ranorex.TreeItem In treeItem.Items
    item.Click()
    CheckForWarningDialog()

    Thread.Sleep(100)
    If item.Items.Count > 0 Then
      IterateTree(item)
    End If

    item.Click()
    CheckForWarningDialog()
  Next
End Sub

' Some mouse clicks causes a warning dialog to popup
' which has to be handled and closed
Private Shared Sub CheckForWarningDialog()
  Dim button As Ranorex.Button = Nothing
  If Host.Local.TryFindSingle("/form[@title='Warning']/button[@text='&No']", 1000, button) Then
	button.Click()
  End If
End Sub

Python

optionsDialog = Form("/form[@title='Folder Options']")
settingsTree = Tree(optionsDialog.FindSingle("container[@caption='View']/tree[@controlid='30120']"))
for item in settingsTree.Items:
        item = TreeItem(item)
	IterateTree(item)

def IterateTree(self, treeItem):
	for item in treeItem.Items:
		item.Click()
		self.CheckForWarningDialog()
		Thread.Sleep(100)
		if item.Items.Count > 0:
			self.IterateTree(item)
		item.Click()
		self.CheckForWarningDialog()

# Some mouse clicks causes a warning dialog to popup  
# which has to be handled and closed
def CheckForWarningDialog(self):
	buttons = Host.Local.Find("/form[@title='Warning']/button[@text='&No']")
	# if button with caption '&No' was found
	# click button and close dialog
	if buttons.Count > 0:
		button0 = Button(buttons[0])
		buttons0.Click()

Table

The following code automates rows and cells from a Win32 table.

Registered 'File Types' of Microsoft file explorer

C#

// Create Ranorex table using RanoreXPath from root
Table table = "/form[@title='Folder Options']/container[@caption='File Types']/table[@controlid='1000']";
table.EnsureVisible();

// Speed up mouse move
Mouse.DefaultMoveTime = 10;
bool endLoop = false;
                
foreach (Ranorex.Row row in table.Rows)
{                    
  // Scrolls automatically
  // when item is not visible
  row.EnsureVisible();
  Mouse.Click(row);
  foreach (Cell cell in row.Cells)
  {
    Mouse.Click(cell);
    // break loop if one of the
    // cells contains a string 'Microsoft'
    if (cell.Text.Contains("Microsoft"))
      endLoop = true; ;
  }
  if (endLoop)
    break;
}

VB.NET

' Create Ranorex table using RanoreXPath from root
Dim table As Table = "/form[@title='Folder Options']/container[@caption='File Types']/table[@controlid='1000']"
table.EnsureVisible()

' Speed up mouse move
Mouse.DefaultMoveTime = 10
Dim endLoop As Boolean = False

For Each row As Ranorex.Row In table.Rows
  ' Scrolls automatically
  ' when item is not visible
  row.EnsureVisible()
  Mouse.Click(row)
  For Each cell As Cell In row.Cells
    Mouse.Click(cell)
    ' break loop if one of the
    ' cells contains a string 'Microsoft'
    If cell.Text.Contains("Microsoft") Then
      endLoop = True
    End If
  Next
  If endLoop Then
    Exit For
  End If
Next

Python

# Create Ranorex table using RanoreXPath from root
table = Table("/form[@title='Folder Options']/container[@caption='File Types']/table[@controlid='1000']")
table.EnsureVisible()

# Speed up mouse move 
Mouse.DefaultMoveTime = 10
endLoop = False

for row in table.Rows:
	# Scrolls automatically
	# when item is not visible
	row.EnsureVisible()
	Mouse.Click(row)
	for cell in row.Cells:
		Mouse.Click(cell)
		# break loop if one of the
		# cells contains a string 'Microsoft'
		if cell.Text.Contains("Microsoft"):
			endLoop = True
	if endLoop:
		break

ToolTip

The next code example reads the value of a tool tip.

C#

// Create Ranorex desktop object from RanoreXPath
Desktop desktop = "/desktop";
desktop.EnsureVisible();
List desktopShortCuts = "/desktop/*/list[@controlid='1']";
Mouse.MoveTo(desktopShortCuts["Recycle Bin"]);
// Wait for tooltip
Thread.Sleep(1500);
// Read and print text of current tool tip
Console.WriteLine("Current tool tip text: " + ToolTip.Current.Text);

VB.NET

' Create Ranorex desktop object from RanoreXPath
Dim desktop As Desktop = "/desktop"
desktop.EnsureVisible()
Dim desktopShortCuts As List = "/desktop/*/list[@controlid='1']"
Mouse.MoveTo(desktopShortCuts("Recycle Bin"))
' Wait for tooltip
Thread.Sleep(1500)
' Read and print text of current tool tip
Console.WriteLine("Current tool tip text: " & ToolTip.Current.Text)

Python

# Create Ranorex desktop object from RanoreXPath
desktop = Desktop("/Desktop")
desktop.EnsureVisible()
desktopShortCuts = List("/desktop/*/list[@controlid='1']")
Mouse.MoveTo(desktopShortCuts["Recycle Bin"])
# Wait for tooltip
Thread.Sleep(1500)
# Read and print text of current tool tip
Console.WriteLine("Current tool tip text: " + ToolTip.Current.Text)

MenuBar

This example describes how to automate menu items.

C#

// Create a Ranorex MenuBar using RanoreXPath
MenuBar menu = "/form[@controlname='TestedApp']
                    /menubar[@controlname='menuStrip1']";
                
// Get the file menu content...
// ... using index to access menu item
menu.Items[0].Click();
ContextMenu fileMenu = "/contextmenu[@processname='TestedForm']";
fileMenu.Items[0].Click(Location.CenterLeft);

// Get the file menu content...
// ... using a lable to access menu item
menu["File"].Click();
ContextMenu fileMenu = "/contextmenu[@processname='TestedForm']";
fileMenu["Item1"].Click(Location.CenterLeft);

VB.NET

' Create a Ranorex MenuBar using RanoreXPath
Dim menu As MenuBar = "/form[@controlname='TestedApp']/menubar[@controlname='menuStrip1']"

' Get the file menu content...
' ... using index to access menu item
menu.Items(0).Click()
Dim fileMenu As ContextMenu = "/contextmenu[@processname='TestedForm']"
fileMenu.Items(0).Click(Location.CenterLeft)

' Get the file menu content...
' ... using a lable to access menu item
menu("File").Click()
fileMenu = "/contextmenu[@processname='TestedForm']"
fileMenu("Item1").Click(Location.CenterLeft)

Python

# Create a Ranorex MenuBar using RanoreXPath
menu = MenuBar("/form[@controlname='TestedApp']/menubar[@controlname='menuStrip1']")

# Get the file menu content...
# ... using index to access menu item
menu.Items[0].Click()
fileMenu = ContextMenu("/contextmenu[@processname='TestedForm']")
fileMenu.Items[0].Click(Location.CenterLeft)

# Get the file menu content...
# ... using a lable to access menu item
menu["File"].Click()
fileMenu = ContextMenu("/contextmenu[@processname='TestedForm']")
fileMenu["Item1"].Click(Location.CenterLeft)