Is it possible to use another repository element in xPath?

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
lucian.teodorescu
Posts: 82
Joined: Fri Oct 24, 2014 10:58 am
Location: Bucharest

Is it possible to use another repository element in xPath?

Post by lucian.teodorescu » Wed May 06, 2015 4:40 pm

Hi all,

I am facing this issue: on a specific screen I have several labels (text) and some other elements: textboxes, comboboxes etc, like this:

Label 1 - Textbox 1
Label 2 - Textbox 2
Label 3 - Combobox 3 etc.

The labels are not linked to their mate element. Also, the elements have unique ID's in the database, but Ranorex doesn't see them. So far I can identify these elements (i.e. textboxes) by ChildIndex Value. But the hard part comes when this index changes (whether the order/location is changed, or strangely with every new build of the app Ranorex finds different indexes).

So I was wondering if there is any possibility to include another item in xpath, for example to reference the item on the same line with another.

e.g. textbox1=myapp/screen1/text[@location.Y=$variableRepositoryItem.Y] or even better:
textbox1=myapp/screen1/text[@location.Y=$variableRepositoryItem.Y and location.X=$variableRepositoryItem.X+100]

where in this case $variableRepositoryItem is bound to Label1.

I hope my question is clear enough for you to understand. If not, I will try harder :)

AUT: Android app

Thank you!
Lucian Teodorescu
NetSun Software

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3941
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Is it possible to use another repository element in xPath?

Post by odklizec » Thu May 07, 2015 8:51 am

Hi,

I believe the xpath does not accept @location.X or @location.Y attributes, but you can use cx() and cy() functions instead. And I'm afraid, xpath also does not accept calculations nor you can pass repo element to xpath via variable. However, it should be possible to achieve what you want via code? here is a quick&dirty example (constructed for MS Calculator buttons ;))...
RelButton.png
RelButton.png (24.98 KiB) Viewed 904 times

Code: Select all

        public void Mouse_Click_ButtonToClick(Ranorex.Adapter srcButton)
        {
        	int xVal = srcButton.Element.Location.X+39;
            int yVal = srcButton.Element.Location.Y;
        	string xPathStr = "//button[@class='Button' and cy()=" + yVal.ToString() + " and cx()=" + xVal.ToString() + "]";
			Ranorex.Button destButton = Host.Local.FindSingle<Ranorex.Button>(srcButton.Element.Parent.GetPath(PathBuildMode.Reduce) + xPathStr,2000);
			destButton.Click();
        }
Basically, the above code gets the location coordinates from the button "1" (passed to method via parameter), adds value 39 to x coordinate (constant distance from one button to next button on the same Y coordinate), creates new xpath, finds element belonging to that xpath and finally clicks it. Hope this helps?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

lucian.teodorescu
Posts: 82
Joined: Fri Oct 24, 2014 10:58 am
Location: Bucharest

Re: Is it possible to use another repository element in xPath?

Post by lucian.teodorescu » Thu May 07, 2015 11:24 am

Hi Pavel!

Thank you very much for the post, especially for the code example. I was thinking about user code approach and your help is highly appreciated.

This is what solved my problem:

Code: Select all

public void selectAssociateElement(Ranorex.Adapter srcLabel)
		{
			int xVal=srcLabel.Element.Location.X;
			int yVal=srcLabel.Element.Location.Y;
			int widthVal = srcLabel.Element.Size.Width;
			
			string xPathStr = "/mobileapp[@title='Wera.PL.Android']//androidelement[@Platformclass='Wera.Controls.Android.MobileAndrCombobox' and @Visible='True' and cy()=" + yVal.ToString() + " and cx()>" + xVal.ToString() + " and cx()<" + (xVal+widthVal+75).ToString() + "]/text";
			Report.Info("Info","Touch element to the right of " + srcLabel.Element.As<Ranorex.Text>().TextValue + " identified by location: "+ xPathStr);
			Ranorex.Text destElement = Host.Local.FindSingle<Ranorex.Text>(xPathStr,30000);
			
			destElement.Touch();
Lucian Teodorescu
NetSun Software

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3941
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Is it possible to use another repository element in xPath?

Post by odklizec » Thu May 07, 2015 11:48 am

You are welcome. I'm glad I could help!
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration