A user code action is used in situations where standard out-of-the-box features provided by the recorder are not enough. Here are some examples where user code actions could be convenient.
Looking at a recording file in the project's view you will see that each recording has two associated code files.
Note: User code actions are not available within the standalone Recorder.
Within Ranorex Studio, each recording manages two types of source code files:
You can jump to the generated code of a recorded action by right-clicking the action and choosing 'View Code' from the context menu.
You can create user specific code actions by converting existing items or by adding a new 'Usercode' action item via the toolbar button 'Add New Action'.
After the creation of a new user code action within the actions table, a new method is added to the partial class of the recording. If you're converting an existing action item, the code generated for the main recording file is transferred to the new user code method.
namespace MyFirstTestProject
{
public partial class AddNewEntry
{
/// <summary>
/// This method gets called right after the recording has been started.
/// It can be used to execute recording specific initialization code.
/// </summary>
private void Init()
{
// Your recording specific initialization code goes here.
}
public void ClickOnPasswordField()
{
Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'FormAdd_Entry.TabPageList.InputFields.TextPassword' at 175;9.", repo.FormAdd_Entry.TabPageList.InputFields.TextPasswordInfo);
repo.FormAdd_Entry.TabPageList.InputFields.TextPassword.Click("175;9");
}
}
}
Namespace MyFirstTestProject
Public Partial Class AddNewEntry
''' <summary>
''' This method gets called right after the recording has been started.
''' It can be used to execute recording specific initialization code.
''' </summary>
Private Sub Init()
' Your recording specific initialization code goes here.
End Sub
Public Sub ClickOnPasswordField()
Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'FormAdd_Entry.TabPageList.InputFields.TextPassword' at 175;9.", repo.FormAdd_Entry.TabPageList.InputFields.TextPasswordInfo)
repo.FormAdd_Entry.TabPageList.InputFields.TextPassword.Click("175;9")
End Sub
End Class
End Namespace
Since Version 3.3 it is possible to use parameters for user code actions. You can pass (string) values to your user code methods to gain more flexibility in your testing environment.
To use parameters for a new user code action, simply click the cell next to the method's cell and type in a value to be used as a parameter (e.g. 'WordPressDemo').
After declaring the parameters with values or variables, you can switch to the user code method and use the passed parameters..
public void AddEntryWithParams(string aTitle, string aUsername, string aPassword, string aURL)
{
MyFirstTestProjectRepository MyRepo = MyFirstTestProjectRepository.Instance;
// Set text fields
MyRepo.AddEntry.TabSheetAddEntry.Title.TextValue = aTitle;
MyRepo.AddEntry.TabSheetAddEntry.UserName.TextValue = aUsername;
MyRepo.AddEntry.TabSheetAddEntry.Password.TextValue = aPassword;
MyRepo.AddEntry.TabSheetAddEntry.Repeat.TextValue = aPassword;
MyRepo.AddEntry.TabSheetAddEntry.URL.TextValue = aURL;
}
Public Sub AddEntryWithParams(aTitle As String, aUsername As String, aPassword As String, aURL As String) MyFirstTestProjectRepository MyRepo = MyFirstTestProjectRepository.Instance ' Set text fields MyRepo.AddEntry.TabSheetAddEntry.Title.TextValue = aTitle MyRepo.AddEntry.TabSheetAddEntry.UserName.TextValue = aUsername MyRepo.AddEntry.TabSheetAddEntry.Password.TextValue = aPassword MyRepo.AddEntry.TabSheetAddEntry.Repeat.TextValue = aPassword MyRepo.AddEntry.TabSheetAddEntry.URL.TextValue = aURL End Sub
Additionally to creating method calls in Ranorex Recorder you are able to define your methods in code and just select the intended method in the Recorder Table. Here you are also able to choose overloaded methods.
Another reason for writing user code is to read text values from UI elements like text boxes and to reuse them for conditional automation steps.
Note: Only create small and easy to maintain user code actions for a recording. If an implemented method should also be available for other test cases, create a code module (see Lesson7: Code Modules) instead.
Within the 'DeleteEntry' recording that was created in Lesson 3: Data-Driven Testing, there are three simple actions for deleting the selected entry (selecting the item, opening the context menu and choosing the context menu item). As you can see it is meaningful to only delete an entry if it is selected. To accomplish this, code with a condition can be used.
As a first step, open the 'DeleteEntry' recording and select the last two items as only they should be executed if the entry is selected. Click on the 'Merge Items to User Code Item' menu item in the context menu.
After doing this, these two actions are merged into one user code action. Give this method a meaningful name (e.g. 'DeleteItemIfFocused'). Switch into the code view by clicking on the context menu item 'View User Code'. Now change the converted code so it is only executed if the cell is focused on.
public void DeleteItemIfFocused()
{
if (repo.MainForm.Entry.HasFocus) {
Report.Log(ReportLevel.Info, "Mouse", "Mouse Right Click item 'MainForm.Entry' at 127;10.", repo.MainForm.EntryInfo);
repo.MainForm.Entry.Click(System.Windows.Forms.MouseButtons.Right, "127;10");
Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'KeePass.DeleteEntry' at 168;14.", repo.KeePass.DeleteEntryInfo);
repo.KeePass.DeleteEntry.Click("168;14");
}
}
Public Sub DeleteItemIfFocused()
If repo.MainForm.Entry.HasFocus Then
Report.Log(ReportLevel.Info, "Mouse", "Mouse Right Click item 'MainForm.Entry' at 127;10.", repo.MainForm.EntryInfo)
repo.MainForm.Entry.Click(System.Windows.Forms.MouseButtons.Right, "127;10")
Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'KeePass.DeleteEntry' at 168;14.", repo.KeePass.DeleteEntryInfo)
repo.KeePass.DeleteEntry.Click("168;14")
End If
End Sub
Download Test Automation Guide
(PDF file, 20MB)