find element in Repo by name

Ask general questions here.
Uwes
Posts: 5
Joined: Wed Sep 02, 2015 8:13 am

find element in Repo by name

Post by Uwes » Wed Sep 02, 2015 8:33 am

Say I have a string, let's call it "Textbox1". How can I find and use the corresponding item in my repository, say, "Page.Something.Textbox1"?

- the "Page.Something" - part is known.
- I want to use it as a <Ranorex.Text> object and use it, i.e. changing the text for this textbox, like

Code: Select all

t.Element.SetAttributeValue("Text", "123");
Hoping for the official solution, OR any workaround.

Thansks in advance!

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

Re: find element in Repo by name

Post by odklizec » Wed Sep 02, 2015 9:10 am

Hi,

If I understand your problem correctly, then what you want to do is to have a variable (e.g. varItemName) containing "Textbox1" string and to use this variable within the repo path? Something like Page.Something.varItemName ? I'm afraid, this is not possible.

Could you be more specific about your problem? Why you need to search the repo item? Are you trying to create a generic method, which is suppose to perform the same steps but with different repo elements? In this case, you should have to use something like this...

Code: Select all

public void YourMethod(Ranorex.Adapter repoItem)    	
{
repoItem.Element.SetAttributeValue("Text", "123");
}
Then you can call this method with different repo items as a parameter. For example, you can create multiple recordings/code modules and each of them can call different repo item.

But maybe I'm misunderstanding something? ;)
Last edited by odklizec on Thu Nov 12, 2015 8:01 am, edited 1 time in total.
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

Uwes
Posts: 5
Joined: Wed Sep 02, 2015 8:13 am

Re: find element in Repo by name

Post by Uwes » Wed Sep 02, 2015 9:55 am

I am using a different Tool (imbus TestBench) to specify my test cases. That's the place that needs to be tidy and useful for non-coders, the Ranorex code is the place where things may get messy :-)

So I have a generic Interaction in TestBench, let's say "Click". There is a parameter that says what to click on.

Solution 1:
All textboxes happen to have similar XPaths, distinguished by a single number; and "14" happens to correspond to "Textbox1".

So I go
Click("14")

in Ranorex I put that index directly into the xpath and get my TextBox

Code: Select all

index = 14;
...
string xpath = @"/form[...]//" + index + "/....";
Ranorex.Text txt = null;
bool found  = Host.Local.TryFindSingle<Ranorex.Text>(xpath, 2000, out txt);
Good: Just one XPath, code is compact. Don't even need Repoitems at all, just deal with XPath in code.
Bad: Person who wants to use my test case will think "wtf is 14?"

Solution 2:

make the interaction Click("Textbox1").

In Ranorex, write

Code: Select all

"if (param == "Textbox1") {click on Repo.Textbox1}"
"if (param == "Textbox2") {click on Repo.Textbox2}"
"if (param == "Textbox3") {click on Repo.Textbox3}"
"if (param == "Textbox4") {click on Repo.Textbox4}"
...
Good: Person who uses my test case can change controls around, use "Textbox1" or "Textbox23"
Bad: Lots of ifs in my code

Ideal Solution:

I want to call the controls in my application by name, and have a tidy repository with all relevant items, and have no spaghetticode to translate between the two.

Any ideas welcome

Uwes
Posts: 5
Joined: Wed Sep 02, 2015 8:13 am

Re: find element in Repo by name

Post by Uwes » Wed Sep 02, 2015 1:45 pm

allright, here is my own solution:

1. Put all repo elements in a (simple) folder in the repository and give them good names
2. Use the same names in the external application (in my case TestBench interactions)
3. The external application calls a method with the following code:

Code: Select all

var elementsInFolder = repo.Editor.SelfInfo.Children;
			
	foreach(var el in elementsInFolder)
	{				
		string elStr = el.ToString();
		elStr = elStr.Substring(elStr.LastIndexOf('.') +1 , elStr.Length - elStr.LastIndexOf('.') -1);
						
		if(elStr == controlName)
		{
			Ranorex.Text t = ".//" + el.Path.ToString();
			t.Element.SetAttributeValue("text", "Land der Berge, Land am Strome...");
		}
	}

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

Re: find element in Repo by name

Post by odklizec » Wed Sep 02, 2015 2:17 pm

Nice! Thanks for sharing your solution.
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