Though a Ranorex recording with all smart actions, variables and user code capabilities is good enough to create robust test automation modules, it might be useful or preferred to write pure Ranorex automation code. Within the following section you learn how to create a new code module which automates the process of adding and deleting a new person to the VIP Database application.
namespace MyFirstTestProject
{
/// <summary>
/// Description of AddAndDeleteVip.
/// </summary>
[TestModule("A730AB98-9CFE-49D5-BDA7-0CAE6C614866")]
public class AddAndDeleteVip : ITestModule
{
public AddAndDeleteVip()
{
}
void ITestModule.Run()
{
Mouse.DefaultMoveTime = 300;
Keyboard.DefaultKeyPressTime = 100;
Delay.SpeedFactor = 1.0;
}
}
}
Public Class AddAndDeleteVip Implements ITestModule Public Sub New() End Sub Private Sub ITestModule_Run() Implements ITestModule.Run Mouse.DefaultMoveTime = 300 Keyboard.DefaultKeyPressTime = 100 Delay.SpeedFactor = 1.0 End Sub End Class
The same way you use a repository in the recording to identify UI elements for automation, you can use it in code. Simply add a new private member which represents the repository to your code module class as follows:
namespace MyFirstTestProject
{
/// <summary>
/// Description of AddAndDeleteVip.
/// </summary>
[TestModule("A730AB98-9CFE-49D5-BDA7-0CAE6C614866")]
public class AddAndDeleteVip : ITestModule
{
// Repository object to access UI elements
MyFirstTestProjectRepository repo = MyFirstTestProjectRepository.Instance;
public AddAndDeleteVip()
{}
void ITestModule.Run()
{
Mouse.DefaultMoveTime = 300;
Keyboard.DefaultKeyPressTime = 100;
Delay.SpeedFactor = 1.0;
repo.MyApp.FirstName.TextValue = "John";
repo.MyApp.LastName.TextValue = "Smith";
repo.MyApp.ButtonAdd.Click();
}
}
}
''' <summary>
''' Description of UserCodeModule1.
''' </summary>
[TestModule("A730AB98-9CFE-49D5-BDA7-0CAE6C614866")]
Public Class AddAndDeleteVip
Implements ITestModule
' Repository object to access UI elements
Private repo As MyFirstTestProject.MyFirstTestProjectRepository = MyFirstTestProject.MyFirstTestProjectRepository.Instance
Public Sub New()
End Sub
Private Sub ITestModule_Run() Implements ITestModule.Run
Mouse.DefaultMoveTime = 300
Keyboard.DefaultKeyPressTime = 100
Delay.SpeedFactor = 1.0
' Set text values using the Text Value
repo.MyApp.InputFields.FirstName.TextValue = FirstName
repo.MyApp.InputFields.LastName.TextValue = LastName
repo.MyApp.Buttons.ButtonAdd.Click()
End Sub
End Class
Note: By default the class name of a repository is the same as it is used for the repository file name (*.rxrep) shown in the project's view.
Now the class uses a private member referring to repository in order to reuse some of the objects ('FirstName', 'LastName', 'ButtonAdd') within the 'Run' method.
namespace MyFirstTestProject
{
/// <summary>
/// Description of AddAndDeleteVip.
/// </summary>
[TestModule("A730AB98-9CFE-49D5-BDA7-0CAE6C614866")]
public class AddAndDeleteVip : ITestModule
{
private MyFirstTestProjectRepository repo =
MyFirstTestProjectRepository.Instance;
public AddAndDeleteVip()
{
}
void ITestModule.Run()
{
Mouse.DefaultMoveTime = 300;
Keyboard.DefaultKeyPressTime = 100;
Delay.SpeedFactor = 1.0;
// Click on 'ButtonSave' using repository directly
repo.MyApp.Buttons.ButtonSave.Click();
// Click on 'ButtonSave' and log text value
// after assignment to local variable 'buttonSave'
var buttonSave = repo.MyApp.Buttons.ButtonSave;
buttonSave.Click();
Report.Info(buttonSave.Text);
}
}
}
Public Class AddAndDeleteVip Implements ITestModule Private repo As MyFirstTestProject.MyFirstTestProjectRepository = MyFirstTestProject.MyFirstTestProjectRepository.Instance Public Sub New() End Sub Private Sub ITestModule_Run() Implements ITestModule.Run Mouse.DefaultMoveTime = 300 Keyboard.DefaultKeyPressTime = 100 Delay.SpeedFactor = 1.0 ' Click on 'ButtonSave' using repository directly repo.MyApp.Buttons.ButtonSave.Click() ' Click on 'ButtonSave' and log text value ' after assignment to local variable 'buttonSave' Dim buttonSave As Button = repo.MyApp.Buttons.ButtonSave buttonSave.Click() Report.Info(buttonSave.Text) End Sub End Class
To create local variables as shown in the code above simply drag and drop elements from the repository browser directly into the code.
Note: If the repository itself is not already part of the class (e.g. newly created code modules) a local variable for the repository is generated too.
By addding a new variable Ranorex Studio inserts a new code fragment at the current cursor position. A variable implementation consists of a public property '<VaribleName>' and a private member '_<VariableName>'.
public class AddAndDeleteVip : ITestModule
{
string _FirstName= "John";
[TestVariable("FDF6BC6C-AFFC-40F3-8899-5F1BC59B8DC8")]
public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName= value;
}
}
public AddAndDeleteVip()
// ...
}
}
Public Class AddAndDeleteVip
Implements ITestModule
Dim _FirstNameAs String = "John"
<TestVariable("8DC1549E-8DC2-434B-BA96-32E2CFDA84D4")> _
Public Property FirstNameAs String
Get
Return _FirstName
End Get
Set(ByVal value As String)
_FirstName= value
End Set
End Property
' ....
Sub Run() Implements ITestModule.Run
Mouse.DefaultMoveTime = 300
Keyboard.DefaultKeyPressTime = 100
Delay.SpeedFactor = 1.0
End Sub
End Class
Now create additional variables for the 'LastName', 'Gender' and 'Category'.
Note:Variables used by the repository are not automatically connected with variables defined by code modules. If it is necessary, use the 'set' method as explained below for the variable 'FirstName' to assign values to the repository variable.
[TestModule("41F66FF2-66D3-42F5-8EC1-F577F8D2D6BC")]
public class AddAndDeleteVip : ITestModule
{
// Repository object to access UI elements
MyFirstTestProjectRepository repo = MyFirstTestProjectRepository.Instance;
// Local variables with default values
string _FirstName = "John";
string _LastName = "Smith";
string _Gender = "Male";
string _Category = "Music";
[TestVariable("A6605D21-BD14-4F3E-9111-E28A9923C66A")]
public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName = value;
// Setting value for FirstName variable
// used by the repository
repo.FirstName = FirstName;
}
}
[TestVariable("28BA2FD2-4926-45C5-B07E-4B6DA1ACD708")]
public string LastName
{ .. }
[TestVariable("6D84257F-12BC-4E2A-B42B-3B119015F646")]
public string Gender
{ .. }
[TestVariable("279F312F-E2D5-4F9C-A077-1BE5BF830367")]
public string Category
{ .. }
public AddAndDeleteVip()
{
}
void ITestModule.Run()
{
Mouse.DefaultMoveTime = 300;
Keyboard.DefaultKeyPressTime = 100;
Delay.SpeedFactor = 1.0;
// Set text values using the Text Value
repo.MyApp.FirstName.TextValue = FirstName;
repo.MyApp.LastName.TextValue = LastName;
// Select 'Male' or 'Female' depending on
// the given test data
// Note: The value is set by the variable's
// property implementation
repo.MyApp.GenderOption.Select();
// Select the category list item depending
// on th the given test data
repo.MyApp.CategoryItem.Select();
// Press button 'Add'
repo.MyApp.ButtonAdd.Press();
// Report a screenshot of the application
Report.Screenshot(repo.MyApp.Self);
// Click the correct row within the
// datagrid.
repo.MyApp.RowToSelect.Click();
// Press button 'Delete'
repo.MyApp.ButtonDelete.Press();
}
}
<TestModule("56849E42-5A21-4EDB-BD57-94F2594EEF79", ModuleType.UserCode, 1)> _
Public Class AddAndDeleteVip
Implements ITestModule
' Repository object to access UI elements
Private repo As MyFirstTestProjectRepository = MyFirstTestProjectRepository.Instance
' Local variables with default values
Dim _FirstName As String = "John"
Dim _LastName As String = "Smith"
Dim _Gender As String = "Male"
Dim _Category As String = "Music"
<TestVariable("A6605D21-BD14-4F3E-9111-E28A9923C66A")> _
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set
_FirstName = value
' Setting value for FirstName variable
' used by the repository
repo.FirstName = FirstName
End Set
End Property
<TestVariable("28BA2FD2-4926-45C5-B07E-4B6DA1ACD708")> _
Public Property LastName() As String
Get
Return m_LastName
End Get
Set
m_LastName = Value
End Set
End Property
Private m_LastName As String
<TestVariable("6D84257F-12BC-4E2A-B42B-3B119015F646")> _
Public Property Gender() As String
Get
Return m_Gender
End Get
Set
m_Gender = Value
End Set
End Property
Private m_Gender As String
<TestVariable("279F312F-E2D5-4F9C-A077-1BE5BF830367")> _
Public Property Category() As String
Get
Return m_Category
End Get
Set
m_Category = Value
End Set
End Property
Private m_Category As String
Public Sub New()
End Sub
Private Sub ITestModule_Run() Implements ITestModule.Run
Mouse.DefaultMoveTime = 300
Keyboard.DefaultKeyPressTime = 100
Delay.SpeedFactor = 1.0
' Set text values using the Text Value
repo.MyApp.FirstName.TextValue = FirstName
repo.MyApp.LastName.TextValue = LastName
' Select 'Male' or 'Female' depending on
' the given test data
' Note: The value is set by the variable's
' property implementation
repo.MyApp.GenderOption.[Select]()
' Select the category list item depending
' on th the given test data
repo.MyApp.CategoryItem.[Select]()
' Press button 'Add'
repo.MyApp.ButtonAdd.Press()
' Report a screenshot of the application
Report.Screenshot(repo.MyApp.Self)
' Click the correct row within the
' datagrid.
repo.MyApp.RowToSelect.Click()
' Press button 'Delete'
repo.MyApp.ButtonDelete.Press()
End Sub
End Class
Online User Guide
download as: PDF (20.3MB)
Ranorex Tutorial
(PDF file, 13.5MB)