how to send dynamic variable Xpath using user code

Ranorex Studio, Spy, Recorder, and Driver.
Alpha13
Posts: 8
Joined: Thu Feb 23, 2023 3:56 pm

how to send dynamic variable Xpath using user code

Post by Alpha13 » Wed Mar 01, 2023 2:03 pm

Hello i would like some helps

I use an action user code to call script to get an if button is visible condition.
I have created a function iFVisibleDoClickButtonOK() and setup a continue step if fail on this action to continue other steps.
It execute Click action when a button is visible on screen.
I have a variable repo which contain the xPath library informations of components.

This function is working :
// Repo variable getting Xpath info in library
public static global::Aquarelle.MAR_V6.AquarelleRepository_V6
repo = global::Aquarelle.MAR_V6.AquarelleRepository_V6.Instance;

[UserCodeMethod]
public static void iFVisibleDoClickButtonOK()
{
Report.Log(ReportLevel.Info,"Log", "Appel de la fonction FiFExistDoClick");
if (repo.ButtonOK.Visible == true)
{
Report.Log(ReportLevel.Info,"Log", "Elément trouvé");
repo.ButtonOK.Click(Location.Center);
Report.Log(ReportLevel.Info, "Log", "Mouse Left Click item 'ButtonOK", repo.ButtonOKInfo);
repo.Aquarelle.RectMainContainer.ButtonAt.Click(Location.UpperRight);
Report.Log(ReportLevel.Info, "Log", "Mouse Left Click item 'ButtonAt", repo.Aquarelle.RectMainContainer.ButtonAtInfo);

}
}

This satisfy my need, but i would like to go further on this.
Is there a way to make it dynamic with a variable. Replacing ButtonOK by sPathName
I want to continue to use the propriety visible to validate the if condition
This will be something like this :

public static void iFVisibleDoClickObject(string sPathName)
{
Report.Log(ReportLevel.Info,"Log", "Appel de la fonction iFVisibleDoClickObject");
pathRepo = repo.sPathName);

if (pathRepo.visible == true)
{
Report.Log(ReportLevel.Info,"Log", "Elément trouvé");
repo.ButtonOK.Click(Location.Center);
Report.Log(ReportLevel.Info, "Log", "Mouse Left Click item 'ButtonOK", repo.ButtonOKInfo);
repo.Aquarelle.RectMainContainer.ButtonAt.Click(Location.UpperRight);
Report.Log(ReportLevel.Info, "Log", "Mouse Left Click item 'ButtonAt", repo.ButtonOKInfo);

}
}

The thing is i don't know how to setup this repo.sPathName.visible to get the dynamic variable and the visible propriety.
In my library repo i can select the xPath items and i have also some propriety Equals, GetHashCode, GetMetaInfos, GetType, ToString.
Any clues ?
You do not have the required permissions to view the files attached to this post.

IvanF
Posts: 151
Joined: Thu Aug 11, 2022 8:55 pm

Re: how to send dynamic variable Xpath using user code

Post by IvanF » Thu Mar 09, 2023 10:39 pm

Hi, i'm not yet sure about the exact syntax for your situation, but the code below does something similar (you can ignore Gherkin):
- a dynamic variable comes from the method (e.g. IntRate, similar to your sPathName)
- the part of xpath that remains static is stored in a variable (e.g. IntRatePathString)
- the "complete" element path is built from 2 variables
(e.g. ((OptionTag)$"{IntRatePathString}/option[@innertext='{IntRate}']")
- once the complete element path is built, you can do any applicable actions, such as .visible or .click

This assumes that the element type is the same regardless of the value of the dynamic variable.

Code: Select all

[When(@"Select (.*) Expected Interest Rate with (.*) Periodic Investment")]
        public void WhenSelectExpectedInterestRateWithPeriodicInvestment(string IntRate, string PeriodicPmt)
        {
        	try {
				String IntRatePathString = repo.ApplicationUnderTest.IntRate.GetPath().ToString();
				((OptionTag)$"{IntRatePathString}/option[@innertext='{IntRate}']").Select();
				
                } catch(Exception ex) { Report.Log(ReportLevel.Warn, "Module", "(Optional Action) " + ex.Message, new RecordItemIndex(2)); }
                
            try {
				String PeriodicPmtPathString = repo.ApplicationUnderTest.PeriodicPmt.GetPath().ToString();
				((OptionTag)$"{PeriodicPmtPathString}/option[@innertext='{PeriodicPmt}']").Select();
				
                } catch(Exception ex) { Report.Log(ReportLevel.Warn, "Module", "(Optional Action) " + ex.Message, new RecordItemIndex(3)); }
        	
        }  

your example seems to hint that you may need a separate repository object with xpath from "/form[..." to "...dialogBackground']", to which you then add "//button[@name='{sPathName}']"

Depending on the element type, you may not need OptonTag or anything similar

Alpha13
Posts: 8
Joined: Thu Feb 23, 2023 3:56 pm

Re: how to send dynamic variable Xpath using user code

Post by Alpha13 » Thu Mar 23, 2023 11:12 am

I have find a solution for my need.
[UserCodeMethod]
		public static void iFVisibleDoClickObject(Ranorex.Core.Repository.RepoItemInfo idElement, Ranorex.Core.Repository.RepoItemInfo buttonToClick)
		{	
			
			Report.Log(ReportLevel.Info,"Log", "Appel de la fonction iFVisibleDoClickObject");
		    if (idElement.Exists())  
	        {  
	            string GetIdElementValue= idElement.CreateAdapter<Ranorex.Unknown>(false).Element.GetAttributeValueText("Visible");
	            if(GetIdElementValue == "True")
	            {  
	            	Report.Log(ReportLevel.Info,"Log", "textBoxVal = true ");
	                buttonToClick.CreateAdapter<Ranorex.Unknown>(true).Click();  
	            } 
				
	        }
			else
			{
				Report.Log(ReportLevel.Info,"Log", "textBoxVal = false ");
			}	
			
    	}	
Then i call it on a record module with user code iFVisibleDoClickObject(Ranorex.Core.Repository.RepoItemInfo idElement, Ranorex.Core.Repository.RepoItemInfo buttonToClick)
idElement : Btn_SaisieVisite_OK
buttonToClick : Btn_SaisieVisite_OK

This function check if the button OK if it's visible it click on it. If not i does nothing. I have also enable the continue on fail propriety.