Useful Ranorex Code snippets
This page contains few useful Ranorex code snippets
- To get current Test case name:
var curTestCase = (TestCaseNode)TestSuite.CurrentTestContainer; string tcName = curTestCase.Name;
- To add image in report
Report.LogData(ReportLevel.Info, "General Data", Ranorex.Imaging.Load(@"E:\Yosuva\Evidence\test.jpg"));
- To get Test status and Test suite status
var tcStatus = TestSuite.Current.GetTestContainer("tcName").Status; // get status of specified TC var tcStatus = Ranorex.Core.Reporting.ActivityStack.Current.Status; // returns test suite status
- To log Html in report
Report.LogHtml(ReportLevel.Success,"Generated "+filename+" Document", "<a href='"+filename+".pdf"+"' target='_blank'>Open Document</a>");
- Example for Table Iteration:
public void ValidateReceivedPayments(RepoItemInfo reference_obj, RepoItemInfo cell_amount, RepoItemInfo cell_reason) { IList<Ranorex.Cell> reference = reference_obj.CreateAdapters<Cell>(); IList<Ranorex.Cell> ada_amount = cell_amount.CreateAdapters<Cell>(); IList<Ranorex.Cell> ada_reason = cell_reason.CreateAdapters<Cell>(); //String mydate=tdate[0].Text; //String s1=tDescription[0].Text; int listCount = products.Count; for(int i=0;i<listCount;i++) { string transaction_ref=reference[i].Text; string amount=ada_amount[i].Text; string reason=ada_reason[i].Text; } }
- To get the report directory path
string dest = TestReport.ReportEnvironment.ReportFileDirectory;
- To call module inside module or user code.You can add the below user code into a module.
public void RunModule() { Recording_module_Name.Start(); }
- Code to get current data range
string rows = TestSuite.Current.CurrentTestContainer.DataRange.MaxRange.ToString();
-
To get custom logo in Ranorex PDF report
Download the style.xml and specify logo path in the below mentioned line. This style.xml can also be used to increase screenshot quality and to change look and feel of pdf.
<global toc="true" logo="C:\logo.png" bottommargin="2" topmargin="2" showtestdata="true" showdescription="true" showpictures="true"/>
Note:In order to see the changes in the pdf , Please specify style.xml path in ConvertToPDF method or in PDF parameters.
- To parameterize a value in the RxPath or to pass dynamic values in Rxpath.
//select[#'Category']/option[@value=$Category] $Category is the parameter/variable name
- To use xpath in user code
Ranorex.Button Button = Host.Local.FindSingle<Ranorex.Button>("/form[@controlname='RxMainFrame']//tabpage[@controlname='RxTabIntroduction']/button[@accessiblename='Submit']",20000); Button.MoveTo();
- To get video path and name
string folder_path = TestReport.ReportEnvironment.ReportVideoFolderPath; string folder_name = TestReport.ReportEnvironment.ReportVideoFolderName; string file_name = TestReport.ReportEnvironment.CurrentVideoFileName;
- To get selected run config name
TestSuite.Current.SelectedRunConfig.Name.ToString();