Change C# type of repository item

Ranorex Spy, Recorder, and Studio.
haniball
Posts: 13
Joined: Fri Sep 18, 2015 2:27 pm

Change C# type of repository item

Post by haniball » Fri Sep 18, 2015 2:32 pm

I have a path like this /form/container/list[@classname='ListBox']/listitem/text[1]

This should return the first textbox of all listitems in a list. But the C# type in the *.cs file of the repository is Ranorex.Text. Therefore I can't use foreach.

How can I a list of items via repository without using index variables?

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Change C# type of repository item

Post by krstcs » Fri Sep 18, 2015 5:07 pm

If you want to get the whole list you will need to use the Find<T>(RanoreXPath) method, which returns a list of type <T> where all items match the given RanoreXPath.

Also, you can see the Ranorex API here: http://www.ranorex.com/Documentation/Ranorex/
Shortcuts usually aren't...

haniball
Posts: 13
Joined: Fri Sep 18, 2015 2:27 pm

Re: Change C# type of repository item

Post by haniball » Fri Sep 18, 2015 9:55 pm

Thanks but that's only a bit helpful. You could at least point to the Find Method in the documentation or give a short example. Does the Find method search for that very same element and casts it to the given type (in this case a list)?

Is it like:
Ranorex.Text myElement = repo.ElementXY;
List<Ranorex.Text> myList = myElement.Find<List<Ranorex.Text>>();

Or like:
Ranorex.Text myElement = repo.ElementXY;
List<Ranorex.Text> myList = Ranorex.Find<List<Ranorex.Text>>(myElementInfo.Path);

I don't get why the documentation tells me form//button gives me all the buttons in a form but in the background to my knowledge it's a property of type button.

I'll try it on monday...

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Change C# type of repository item

Post by krstcs » Mon Sep 21, 2015 3:42 pm

If you don't understand these basic principles of Ranorex and .NET programming, then my suggestion would be to read the Ranorex documentation and user guide (http://www.ranorex.com/support/user-guide-20.html) and then read about .NET programming, specifically with respect to C# and object oriented programming.

The API that I pointed to gives a minimal amount of information assuming that you understand .NET and Ranorex in general.

If you don't understand the nomenclature "Find<T>(RanoreXPath)" and such, then my suggestion is that you are either going to need to learn more or not do what you are trying to do.
Shortcuts usually aren't...

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Change C# type of repository item

Post by Support Team » Mon Sep 21, 2015 3:54 pm

Hello haniball,

Below you can find a sample, which shows one way to use the Find()-method.

Code: Select all

public IList<Element> Find(
	RxPath path
)
In my sample I'm using a simple form, which contains three text boxes.
form.png
form.png (2.51 KiB) Viewed 1649 times
The textbox-elements have the following paths:
/form[@controlname='Form1']/text[@controlname='textBox1']
/form[@controlname='Form1']/text[@controlname='textBox2']
/form[@controlname='Form1']/text[@controlname='textBox3']

Beginning from the form-element I start searching for all elements of type Ranorex.Text, which match the path "./text".

These elements are stored to a list.

Now we can loop through the list and write each text value to the Report.

Code: Select all

IList<Ranorex.Text> list = repo.Form.Self.Find<Ranorex.Text>("./text");
        	
foreach ( Ranorex.Text textbox in list )
{
      Report.Info(textbox.TextValue);
}
We could also use the path ".//text", which identifies all buttons that are descendants of the form.

More information about the RanoreXPath can be found in the user guide.

Please let me know if you need more information.

Regards,
Johannes
.
Image

haniball
Posts: 13
Joined: Fri Sep 18, 2015 2:27 pm

Re: Change C# type of repository item

Post by haniball » Mon Sep 21, 2015 10:57 pm

Damn, took me half the day to find out myself what you posted then with a perfect explaination :). It's not exactly what I was looking for because I don't like hardcoded strings in my sourcecode but it's not too bad.

After that problem got solved the very next question was how to find the nearest item with a certain id. Is there a way to do that? Like jQuery closest https://api.jquery.com/closest/ in Ranorex.

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

Re: Change C# type of repository item

Post by odklizec » Tue Sep 22, 2015 7:44 am

Hi,
haniball wrote:Damn, took me half the day to find out myself what you posted then with a perfect explaination :). It's not exactly what I was looking for because I don't like hardcoded strings in my sourcecode but it's not too bad.
You can always replace the hardcoded string with variable and fill that variable from a connected data source. This is definitely better way ;)
haniball wrote:After that problem got solved the very next question was how to find the nearest item with a certain id. Is there a way to do that? Like jQuery closest https://api.jquery.com/closest/ in Ranorex.
Could you be more specific about what exactly you want to do? Is your next question related to the previously explained Find method or something completely different? Could you please post a Ranorex snapshot showing the requested element and its surrounding elements? With snapshot, it would be easier for us to suggest a 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

haniball
Posts: 13
Joined: Fri Sep 18, 2015 2:27 pm

Re: Change C# type of repository item

Post by haniball » Tue Sep 22, 2015 11:17 am

You ask if that is related to the find Method. No I am looking for a command like jQuery closest but in Ranorex.

I created a new Post because it's not really related to this one... -> http://www.ranorex.com/forum/find-close ... t8492.html