The following code examples show how to automate GUI controls using Ranorex Automation Framework for testing purposes. Currently all examples are available for C# and VB.NET testing.
Button
Following code first finds a Button using the control name "button1". After that the button is clicked and its state read.
C#
Button button = form.FindButton("button1");
button.Click();
button.Click(MouseButtonType.RightButton);
if(button.Pressed)
Console.WriteLine("button1 is pressed");
VB.NET
Dim button As Ranorex.Button
button = form.FindButton("button1")
Ranorex.Mouse.MoveToControl(button)
button.Click()
Python
button1 = Ranorex.FormFindChildControlName(form,'button1')
Ranorex.ButtonClick(button1)
if Ranorex.ButtonIsPressed(button1):
print 'button1 is pressed'
CheckBox
This code finds a CheckBox by its control name, reads its state and sets the checked state to true.
C#
CheckBox checkBox = form.FindCheckBox("checkBox1");
Console.WriteLine("checkBox1 state: " + checkBox.Checked);
checkBox.Checked = true;
VB.NET
Dim checkBox As Ranorex.CheckBox
checkBox = form.FindCheckBox("checkBox1")
checkBox.Checked = True
Ranorex.Application.Sleep(1000)
checkBox.Checked = False
Python
check1 = Ranorex.FormFindChildControlName(form,'checkBox1')
print 'checkBox1 state: ' + Ranorex.ButtonIsChecked(check1)
Ranorex.ButtonCheck(check1)
Ranorex.ButtonUncheck(check1)
RadioButton
This code finds a RadioButton by its control name "radioButton1", reads its state and sets the checked state to true.
C#
String itemText;
RadioButton radio1 = form.FindRadioButton("radioButton1");
Console.WriteLine("radio1 state: " + radio1.Checked);
radio1.Checked = true;
VB.NET
Dim radioButton As Ranorex.RadioButton
radioButton = form.FindRadioButton("radioButton1")
radioButton.Checked = True
Python
radio1 = Ranorex.FormFindChildControlName(form,'radioButton1')
print 'radioButton1 state: ' + Ranorex.ButtonIsChecked(radio1)
Ranorex.ButtonCheck(radio1)
Ranorex.ButtonUncheck(radio1)
ComboBox
This example finds a ComboBox with control name "comboBox1". The methods "ShowDropDown" and "HideDropDown" causes the control to show and hide all items of the ComboBox. After that the amount of entries and it's text content will be read.
C#
ComboBox comboBox = form.FindComboBox("comboBox1");
comboBox.ShowDropDown();
comboBox.HideDropDown();
int items = comboBox.ItemCount;
Console.WriteLine("ComboBox ItemCount=" + items.ToString());
for(int i=0; i<items; i++)
{
string itemText = comboBox.GetItemText(i);
comboBox.SelectedIndex = i;
Console.WriteLine("Item[{0}]={1}", comboBox.SelectedIndex, comboBox.SelectedText);
}
</pre>
VB.NET
Dim comboBox As Ranorex.ComboBox
comboBox = form.FindComboBox("comboBox1")
comboBox.ShowDropDown()
comboBox.HideDropDown()
Dim items As Integer
items = comboBox.ItemCount
Dim text As String
For i As Integer = 0 To items
text = comboBox.GetItemText(i)
comboBox.SelectedIndex = i
Console.WriteLine(text)
Ranorex.Application.Sleep(200)
Next
TextBox
The following example shows different ways of setting the text of a TextBox.
C#
TextBox textBox = form.FindTextBox("textBox1");
// Moves the mouse to the control
Mouse.MoveToControl(textBox);
textBox.Focus();
// Sets the content of the TextBox to "hello"
textBox.Text = "hello";
// Inserts the value "Ranorex" at the beginning of the current
// text content using a time delay of 100 milliseconds between
// each keystroke
textBox.SendKeys("Ranorex", 100);
VB.NET
Dim textBox As Ranorex.TextBox
textBox = form.FindTextBox("textBox1")
Ranorex.Mouse.MoveToControl(textBox)
textBox.Focus()
' Sets the content of the TextBox to "hello"
textBox.Text = "hello"
' Inserts the value "Ranorex" at the beginning of the current
' text content using a time delay of 100 milliseconds between
' each keystroke
textBox.SendKeys("Ranorex", 100)
ListView
This code selects and reads elements of a ListView control.
C#
ListView listView = form.FindListView("listView1");
// Moves the mouse to the control
Mouse.MoveToControl(listView);
listView.Focus();
// Reading item count
Console.WriteLine("ItemCount={0}", listView.ItemCount);
// Selects each item of the ListView and reads
// it's main and first sub entry
for (int i=0; i < itemCount;i++)
{
// Reads main entry
text = listView.GetItemText(i,0);
Console.WriteLine(" Item[{0},{1}]={2}",i,0,text);
listView.SelectItem(text);
// Reads sub entry
text = listView.GetItemText(i, 1);
Console.WriteLine(" Item[{0},{1}]={2}", i, 1, text);
}
VB.NET
Dim listView As Ranorex.ListView
listView = form.FindListView("listView1")
'Moves the mouse to the control
Ranorex.Mouse.MoveToControl(listView)
listView.Focus()
'Reading and writing item count
Console.WriteLine("ItemCount=" + listView.ItemCount.ToString())
'Selects each item of the ListView and reads
'it's main and first sub entry
Dim items As Integer
Dim text As String
items = listView.ItemCount
For i As Integer = 0 To items
'Reads main entry
text = listView.GetItemText(i, 0)
Console.WriteLine("Item[{0},{1}]={2}", i, 0, text)
'Select item
listView.SelectItem(text)
'Reads sub entry
text = listView.GetItemText(i, 1)
Console.WriteLine("Item[{0},{1}]={2}", i, 1, text)
Ranorex.Application.Sleep(200)
Next
ListBox
The following example reads the content of a ListBox and selects each item using the item's text.
C#
// Finds the ListBox by it's control name "listBox1"
ListBox listBox = form.FindListBox("listBox1");
Mouse.MoveToControl(listBox);
listBox.Focus();
// Reads the selected element
string text = listBox.SelectedText;
Console.WriteLine(" Selected text={0}", text);
// Gets the item count of the ListBox
int itemCount = listBox.ItemCount;
Console.WriteLine("ItemCount={0}", itemCount);
// Reads the currently selected index of the ListBox
int index = listBox.SelectedIndex;
Console.WriteLine(" Selected index={0}",index);
// Selects each element of the ListBox
for (int i=0; i < itemCount;i++)
{
// Reads item text
text = listBox.GetItemText(i);
Console.WriteLine("Item[{0}]={1}",i,text);
// Using item text to select element
listBox.SelectedText = text;
}
VB.NET
Dim listBox As Ranorex.ListBox
listBox = form.FindListBox("listBox1")
'Moves the mouse to the control
Ranorex.Mouse.MoveToControl(listBox)
listBox.Focus()
'Reads the selected element
'Dim text As String
text = listBox.SelectedText
Console.WriteLine("Selected text: " + text)
'Gets the item count of the ListBox
Dim itemCount As Integer
itemCount = listBox.ItemCount
Console.WriteLine("Item count: " + itemCount.ToString())
'Reads the currently selected index of the ListBox
Dim index As Integer
index = listBox.SelectedIndex
Console.WriteLine("Selected index: " + index.ToString())
'Dim items As Integer
items = listBox.ItemCount
Selects each element of the ListBox
For i As Integer = 0 To items
'Reads item text by index
text = listBox.GetItemText(i)
'Using item text to select element
listBox.SelectedText = text
Ranorex.Application.Sleep(200)
Next
CheckedListBox
The following code sample shows how to select and check items of a CheckedListBox control.
C#
Control checkedListBox = form.FindControlName("checkedListBox1");
checkedListBox.Focus();
Element element = checkedListBox.Element.FindChild(Role.CheckButton, "Item2", null);
if (element != null)
{
Console.WriteLine("Element Item2 found");
// Reads size and positon of "Item2"
Point point = element.Location;
Size size = element.Size;
Console.WriteLine(" Item Value={0} Location=({1},{2}) DefaultAction={3}", element.Value, point.X, point.Y, element.DefaultAction);
// If elemet state is checked "DoDefaultAction" means to uncheck the check box
element.DoDefaultAction();
Console.WriteLine(" Item Value={0} Location=({1},{2}) DefaultAction={3}", element.Value, point.X, point.Y, element.DefaultAction);
element.DoDefaultAction();
}
VB.NET
Dim checkedListBox As Ranorex.Control
checkedListBox = form.FindControlName("checkedListBox1")
Dim element As Ranorex.Element
element = checkedListBox.Element.FindChild(Role.CheckButton, "Item2")
If Not element Is Nothing Then
Console.WriteLine("Element Item2 found")
'Reads size and position of "Item2"
Dim point As System.Drawing.Point
Dim size As System.Drawing.Size
point = element.Location
size = element.Size
Console.WriteLine(" Item Value={0} Location=({1},{2}) DefaultAction={3}", element.Value, point.X, point.Y, element.DefaultAction)
' If elemet state is checked "DoDefaultAction" means to uncheck the check box
element.DoDefaultAction()
Console.WriteLine(" Item Value={0} Location=({1},{2}) DefaultAction={3}", element.Value, point.X, point.Y, element.DefaultAction)
element.DoDefaultAction()
End If
TreeView
The following code sample shows how to read content from a TreeView and how to select items using a recursive routine.
C#
TreeView treeView = form.FindTreeView("treeView1");
Mouse.MoveToControl(treeView);
treeView.Focus();
// Reads the text of the currently selected item
string text = treeView.SelectedText;
// Gets item number of root item
Int32 actItem = treeView.RootItem;
TreeViewCheckItem(treeView, actItem);
// Selects each item of the TreeNode recursively
static void TreeViewCheckItem(TreeView treeView, Int32 item)
{
text = treeView.GetItemText(item);
Console.WriteLine(" text={0}",text);
Int32 actItem = item;
while ( actItem >= 0 )
{
Int32 subitem = treeView.GetChildItem(actItem);
// Calls routine for each sub item
if ( subitem > 0 )
TreeViewCheckItem(treeView, subitem);
actItem = treeView.GetNextItem(actItem);
if ( actItem == 0)
break;
// Reads the text of the current element
text = treeView.GetItemText(actItem);
Console.WriteLine(" text={0}",text);
// Selects current item using the item's text
// The actItem value accelerates the selection
treeView.SelectItem(text,actItem);
// Reads the text of the selected element
Console.WriteLine(" selected text={0}", treeView.SelectedText);
Application.Sleep(100);
}
}
VB.NET
Dim treeView As Ranorex.TreeView
treeView = form.FindTreeView("treeView1")
Mouse.MoveToControl(treeView)
treeView.Focus()
'Reads the text of the currently selected item
Dim text As String
text = treeView.SelectedItem
'Gets item number of root item
Dim actItem As Integer
actItem = treeView.RootItem
TreeViewCheckItem(treeView, actItem)
Sub TreeViewCheckItem(ByVal treeView As Ranorex.TreeView, ByVal item As Integer)
Dim text As String
text = treeView.GetItemText(item)
Console.WriteLine(" text={0}", text)
Dim actItem As Integer
While actItem > 0
Dim subItem As Integer
subItem = treeView.GetChildItem(actItem)
'Calls routine for each sub item
If subItem > 0 Then
TreeViewCheckItem(treeView, subItem)
End If
actItem = treeView.GetNextItem(actItem)
If actItem = 0 Then
Exit While
End If
'Reads the text of the current element
text = treeView.GetItemText(actItem)
Console.WriteLine(" text={0}", text)
'Selects current item using the item'S text
'The actItem value accelerates the selection
treeView.SelectItem(text, actItem)
'Reads the text of the selected element
Console.WriteLine(" selected text={0}", treeView.SelectedText)
Application.Sleep(100)
End While
End Sub
ProgressBar
This example shows how to read and write values from and to a ProgressBar control.
C#
ProgressBar progressBar = form.FindProgressBar("progressBar1");
// Move the mouse to the control
Mouse.MoveToControl(progressBar);
progressBar.Focus();
// Reads current progress bar value
int position = progressBar.Value;
// Set progress bar value to 20
progressBar.Value = 20;
VB.NET
Dim progressBar As Ranorex.ProgressBar
progressBar = form.FindProgressBar("progressBar1")
'Move the mouse to the control
Ranorex.Mouse.MoveToControl(progressBar)
progressBar.Focus()
'Reads current progress bar value
Dim position As Integer
position = progressBar.Value
'Set progress bar to 20
progressBar.Value = 20
TrackBar
The following example shows how to read the current TrackBar value and how to set different values to the TrackBar control.
C#
TrackBar trackBar = form.FindTrackBar("trackBar1");
Console.WriteLine("TrackBar \"trackBar1\" found");
// Move the mouse to the control
Mouse.MoveToControl(trackBar);
trackBar.Focus();
// Reads the current value from the TrackBar control
position = trackBar.Value;
// Sets some values to the TrackBar control
trackBar.Value = 2;
trackBar.Value = 4;
trackBar.Value = 6;
VB.NET
Dim trackBar As Ranorex.TrackBar
trackBar = form.FindTrackBar("trackBar1")
Console.WriteLine("'trackBar' found")
'Move the mouse to the control
Ranorex.Mouse.MoveToControl(trackBar)
trackBar.Focus()
'Reads the current value from the TrackBar control
Dim position As Integer
position = trackBar.Value
'Sets some values to the TrackBar control
trackBar.Value = 2
trackBar.Value = 4
trackBar.Value = 6
TabControl
The TabControl sample code describes how to access tabs within a TabControl.
C#
TabControl tabControl = form.FindTabControl("tabControl1");
// Move the mouse to the control
Mouse.MoveToControl(tabControl);
tabControl.Focus();
// Reads the text of the current active tab
string text = tabControl.SelectedText;
Console.WriteLine(" Selected text={0}", text);
// Reads the index of the current active tab
index = tabControl.SelectedIndex;
Console.WriteLine(" Selected index={0}", index);
// Gets the count of tabs within the tab control
int itemCount = tabControl.ItemCount;
Console.WriteLine(" ItemCount={0}", itemCount);
// Selects each tab and reads its name
for (int i=0; i < itemCount;i++)
{
text = tabControl.GetItem(i);
Console.WriteLine(" Item[{0}]={1}",i,text);
tabControl.SelectedIndex = i;
}
VB.NET
Dim tabControl As Ranorex.TabControl
tabControl = form.FindTabControl("tabControl1")
' Move the mouse tho the control
Mouse.MoveToControl(tabControl)
tabControl.Focus()
'Reads the text of the current active tab
Console.WriteLine("Selected text={0}", tabControl.SelectedText)
'Reads the index of the current active tab
Console.WriteLine("Selected index={0}", tabControl.SelectedIndex.ToString())
'Gets the count of tabs within the tab control
Console.WriteLine("ItemCount={0}", tabControl.ItemCount.ToString())
'Selects each tab and reads its name
For i As Integer = 0 To tabControl.ItemCount
Console.WriteLine(" Item[{0}]={1}",i,tabControl.SelectedText)
tabControl.SelectedIndex = i
Next
NumericUpDown
The following code example shows how to set values to a NumericUpDown control.
C#
NumericUpDown numericUpDown = form.FindNumericUpDown("numericUpDown1");
// Sets value to "45"
numericUpDown.Value = "45";
// Increments the current value
numericUpDown.UpButton();
// Decrements the current value
numericUpDown.DownButton();
VB.NET
Dim numericUpDown As Ranorex.NumericUpDown
numericUpDown = form.FindNumericUpDown("numericUpDown1")
'Sets value to 45
numericUpDown.Value = "45"
'Increment the current value
numericUpDown.UpButton()
'Decrement the current value
numericUpDown.DownButton()
MenuControl
This example describes the way of navigating through a menu structure using the Menu control class.
C#
String itemText;
Menu menu = new Menu(form);
int ret = 0;
if (menu != null)
{
// Gets the item count of the submenus
ret = menu.GetItemCount();
Console.WriteLine(" GetItemCount={0}", ret);
// Gets the item count of the first submenu
ret = menu.GetItemCount(1);
Console.WriteLine(" GetItemCount(1)={0}", ret);
// Gets the text of the second menu item of the first submenu
itemText = menu.GetItemText(1, 2);
Console.WriteLine(" GetItemText(1,2)={0}", itemText);
// Selects a menu item
ret = menu.SelectItemText("MenuItem1", "SubMenu2", "SubItem1");
}
VB.NET
Dim itemText As String
Dim menu As Ranorex.Menu
menu = New Ranorex.Menu(form)
Dim ret As Integer
ret = 0
If Not menu Is Nothing Then
'Gets the item count of the submenus
Console.WriteLine(" GetItemCount={0}", menu.GetItemCount.ToString())
'Gets the item count of the first submenu
Console.WriteLine(" GetItemCount(1)={0}", menu.GetItemCount(1).ToString())
'Gets the text of the second menu item of the first submenu
Console.WriteLine(" GetItemText(1,2)={0}", menu.SelectItemText(1, 2).ToString())
'Selects a menu item
menu.SelectItemText("MenuItem1", "SubMenu2", "SubItem1")
End If
ContextMenu
The sample describes how to automate a context menu control.
C#
// Simulates a right mouse click within an existing form
Mouse.Click(MouseButtonType.RightButton, form.Location.X + 50, form.Location.Y + 100, 1);
// Waiting for context menu
Application.Sleep(50);
// Selects a subitem of "ContextMenuItem"
ContextMenu.SelectItemText("ContextMenuItem", "SubItem");
VB.NET
'Simulates a right mouse click within the existing form
Ranorex.Mouse.Click(MouseButtonType.RightButton, form.Location.X + 50, form.Location.Y + 100, 1)
'Waiting for context menu
Ranorex.Application.Sleep(50)
'Selects a subitem of "ContextMenuItem"
ContextMenu.SelectItemText("ContextMenuItem", "SubItem")
StatusBar
This sample code shows how to read text from a StatusBar control.
C#
StatusBar statusBar = form.FindStatusBar("statusBar1");
if ( statusBar != null )
{
Console.WriteLine("StatusBar \"statusBar1\" found");
// Moves the mouse to the control
Mouse.MoveToControl(statusBar);
statusBar.Focus();
// Reads current text from StatusBar control
string text = statusBar.Text;
Console.WriteLine(" StatusBar text={0}", text);
}
VB.NET
Dim statusBar As Ranorex.StatusBar
statusBar = form.FindStatusBar("statusBar1")
If Not statusBar Is Nothing Then
Console.WriteLine("StatusBar 'statusBar1' found")
'Moves the mouse to the control
Ranorex.Mouse.MoveToControl(statusBar)
statusBar.Focus()
'Reads current text from StatusBar control
Console.WriteLine(" StatusBar text={0}", statusBar.Text)
End If
ScrollBar
The following code describes how to automate movements of a ScrollBar control.
1)
C#
// Finds horizontal ScrollBar
ScrollBar hScrollbar = form.FindScrollBar("hScrollBar1");
Mouse.MoveToControl(hScrollbar);
// Moves horizontal ScrollBar
for (int value = 0; value <= 100; value += 10)
{
hScrollbar.Value = value;
Application.Sleep(100);
}
// Reads maximum and minimum values of the ScrollBar
Console.WriteLine("Minimum = {0}", hScrollbar.Minimum);
Console.WriteLine("Maximum = {0}", hScrollbar.Maximum);
VB.NET
'Finds horizontal ScrollBar
Dim hScrollBar As Ranorex.ScrollBar
hScrollBar = form.FindScrollBar("hScrollBar1")
Ranorex.Mouse.MoveToControl(scrollBar)
'Moves horizontal ScrollBar
For i As Integer = 0 To 100 Step 10
hScrollBar.Value = value;
Ranorex.Application.Sleep(100)
Next
' Reads maximum and minimum values of the ScrollBar
Console.WriteLine("Minimum = {0}", hScrollBar.Minimum)
Console.WriteLine("Maximum = {0}", hScrollBar.Maximum)
DataGrid
This sample code explains how to access a DataGrid control, how to read and write data from and to it.
1)
C#
DataGrid dataGrid = form.FindDataGrid("dataGridView1");
// Reading colum text of column 1
String columnText = dataGrid.GetColumnText(1);
Console.WriteLine("DataGrid ColCount = {0} ", dataGrid.ColumnCount);
// Reading item text from row 1 and column 2
String itemText = dataGrid.GetItemText(1, 2);
Console.WriteLine("DataGrid ItemText={1}", itemText);
// Sets item text at row 1 and column 1 to "Ranorex"
dataGrid.SetItemText(1, 1, "Ranorex");
// Clicks the column header with text "Col1"
dataGrid.ClickColumn("Col1");
VB.NET
Dim dataGrid As Ranorex.DataGrid
dataGrid = form.FindDataGrid("dataGridView1")
'Reading colum text of column 1
Dim columnText As String
columnText = dataGrid.GetColumnText(1)
Console.WriteLine("DataGrid ColCount = {0} ", dataGrid.ColumnCount)
'Reading item text from row 1 and column 2
Console.WriteLine("DataGrid ItemText={1}", dataGrid.GetItemText(1, 2))
'Sets item text at row 1 and column 1 to "Ranorex"
dataGrid.SetItemText(1, 1, "Ranorex")
'Clicks the column header with text "Col1"
dataGrid.ClickColumn("Col1")
ToolBar
Older applications, like Microsoft WordPad, often use ToolBar controls with CommandIds. In some cases the ToolBar items are only accessible by using the CommandId shown by RanorexSpy. Use following code example to access all elements of a ToolBar control.
1)
C#
Form form = Application.FindForm("Document - WordPad", SearchMatchMode.MatchFromStart, "WordPadClass");
form.Activate();
// Find ToolBar control by ControlId - see RanorexSpy
ToolBar toolbar = form.FindToolBar(59392);
// Reads item information
Int32 count = toolbar.ItemCount;
for (int i = 0; i < count; i++)
{
int commandid = toolbar.GetItemCommandId(i);
Point point = toolbar.GetItemLocation(commandid);
Size size = toolbar.GetItemSize(commandid);
Console.WriteLine("commandid ={0} loc={1} size={2}", commandid, point, size);
}
// Select item by CommandId - see RanorexSpy
toolbar.ClickItem(57603);
VB.NET
form = Application.FindForm("Document - WordPad", SearchMatchMode.MatchFromStart, "WordPadClass")
form.Activate()
'Find ToolBar control by ControlId - see RanorexSpy
Dim toolBar As Ranorex.ToolBar
toolBar = form.FindToolBar(59392)
'Reads item information
For i As Integer = 0 To toolBar.ItemCount
Dim commandId As Integer
commandId = toolBar.GetItemCommandId(i)
Dim point As System.Drawing.Point
point = toolBar.GetItemLocation(commandId)
Dim size As System.Drawing.Size
size = toolBar.GetItemSize(commandId)
Console.WriteLine("commandid ={0} loc={1} size={2}", commandId, point, size)
Next
'Select item by CommandId - see RanorexSpy
toolBar.ClickItem(57603)
1) This feature is only available in versions of Ranorex greater or equal to 1.3.