Get all elements of a certain type

Ask general questions here.
jjorgens
Posts: 14
Joined: Wed Apr 01, 2009 7:32 pm

Get all elements of a certain type

Post by jjorgens » Tue Jan 27, 2015 7:35 pm

Is there a way to get all elements of a certain type. For example, if I get the main window, is there a way to say give me all the buttons on that form even the descendants and children ? In the example below, I want to find the element and then get a list of all the buttons child or descendant it has. I should get a list of 3 buttons back.

Code: Select all

Element (Main Form)
|- Child (Button)
|- Child/Descendant 2
|   \- Descendant 21 (RadioButton)
|   \- Descendant 22 (Button)
|- Child (Text)
\- Child/Descendant 4
    \- Descendant 41
        \- Descendant 411 (Button)

Code: Select all

form = Ranorex.Host.Local.FindSingle<Ranorex.Form>(new RxPath("/form[@automationid='someform']"),
                        new Duration(timeOut));

# This only returns all the buttons at the child level. I want all buttons in the tree. Is this possible?
form.find("Button") 

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

Re: Get all elements of a certain type

Post by krstcs » Tue Jan 27, 2015 7:43 pm

Find<T>() should work, just make sure your XPath is correct and would capture all desired elements.

In the following, Find() should return all buttons that match the path "//button" under the form element.

Code: Select all

List<Button> buttonList = new List(form.Find<Button>("//button"));
In yours, you don't include the "//" (which basically says find the next thing at ANY level below). So mine ("//button") says find a button at any level, where yours ("button") says "find a button in the next level".
Shortcuts usually aren't...

jjorgens
Posts: 14
Joined: Wed Apr 01, 2009 7:32 pm

Re: Get all elements of a certain type

Post by jjorgens » Tue Jan 27, 2015 8:07 pm

That did work thanks! It seems to be finding every element that is a button on everything not just the form I am searching on, is that how it should work? I would expect it to only find buttons on the element I am looking at not everything.

I figured it out, I used ".//button" and that gave me everything under the element where "//button" gave me everything.

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

Re: Get all elements of a certain type

Post by krstcs » Tue Jan 27, 2015 8:18 pm

It will find ALL elements that match the XPath that are descendants of the element you are searching from, in this case "form".

If you need it to be more specific, then you should search from a parent element that contains only the elements you want, and add attributes to the XPath statement that will further filter the returned elements.

For instance, if you only want buttons that say "OK", you would use "//button[@text='OK']". Again, you must make sure that the path is specific enough to find only the ones you want, but general enough to find ALL of the ones you want. It's a delicate balancing act. You can also use Regular Expressions if you need to match a pattern. Or you can add a variable to the path that will change the path based on what the variable contains.

XPath is the KEY to understanding how to effectively use Ranorex, so learn it well.
Shortcuts usually aren't...

jjorgens
Posts: 14
Joined: Wed Apr 01, 2009 7:32 pm

Re: Get all elements of a certain type

Post by jjorgens » Tue Jan 27, 2015 8:21 pm

That makes sense thanks for your time and help with this.

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

Re: Get all elements of a certain type

Post by krstcs » Tue Jan 27, 2015 9:35 pm

You are welcome!
Shortcuts usually aren't...

Bacon
Posts: 3
Joined: Wed Jan 27, 2016 3:23 pm

Re: Get all elements of a certain type

Post by Bacon » Wed Jan 27, 2016 3:33 pm

I don't want to open a new thread, so here is my question to the same topic:

I'm trying to get all elements of type link. I want to write them in a list like:

Code: Select all

    List<Button> buttonList = new List(form.Find<Button>("//button"));
my code is like:

Code: Select all

List<Link> alcoholic = new List(Ranorex.Host.Local.Find<Link>(".//link"));
I get this error code:
Argument '1': Konvertierung von 'System.Collections.Generic.IList<Ranorex.Link>' in 'Ranorex.Core.Element' nicht möglich. (CS1503) - C:\Users\T01_Login.cs:71,42
Thank you.

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

Re: Get all elements of a certain type

Post by Support Team » Thu Jan 28, 2016 3:15 pm

Hi Bacon,

I would suggest using the following code to get a list of all elements of a specific type:

Code: Select all

IList<Type> list = Host.Local.Find<Type>(".//<Type>");
Detailed information about the Find method can be found in the API Documentation.

If you need more information, please do let me know.

Sincerley,
Johannes
.
Image

Bacon
Posts: 3
Joined: Wed Jan 27, 2016 3:23 pm

Re: Get all elements of a certain type

Post by Bacon » Tue Feb 02, 2016 9:33 am

Thanks for your answer.

Now I'm one step closer...
but my list is still empty.

Within my objective repository i find 9 elements with my xpath.
But when i use it in my code:

Code: Select all

IList<Link> list = Host.Local.Find<Link>("/dom[@domain='example']//ul/li/h2/a[@tagname='a']");
or

Code: Select all

IList<Link> list = Host.Local.Find<Link>(".//ul/li/h2/a[@tagname='a']"); 
i don't find anything.

I use following code to test if the list is filled up:

Code: Select all

string mystring = Convert.ToString(list.Count);
Ranorex.Report.Success(mystring);
I also tried <LinkTag>

Thank you again.

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

Re: Get all elements of a certain type

Post by lucian.teodorescu » Tue Feb 02, 2016 2:04 pm

Hi Bacon,

My suggestion is to replace all '<Link>' from your code with '<Ranorex.ATag>' (i.e. IList<Ranorex.ATag>)

Give it a try and let us know if this solved the issue.

Edit: What I suggest is to always use the same type of element when populating lists/finding items.
e.g.

Code: Select all

IList<someType> listOfItems = Host.Local.Find<someType>("xPath which should end with /AdapterOfTheSameType[@attribute='attribute_value']")
In your code you are looking for elements of type 'a', so you should use ATag or Ranorex.ATag as type of iList and element searched.
Lucian Teodorescu
NetSun Software

Bacon
Posts: 3
Joined: Wed Jan 27, 2016 3:23 pm

Re: Get all elements of a certain type

Post by Bacon » Tue Feb 02, 2016 2:45 pm

'<Ranorex.ATag>' worked fine.
Thank you very much.

Peadar_H
Posts: 18
Joined: Tue Jun 02, 2015 8:48 pm

Re: Get all elements of a certain type

Post by Peadar_H » Fri Apr 22, 2016 5:31 pm

Hello, hope I can add to this thread with a similar question please, I think this is the right topic. I'm an extreme novice at test automation (no coding experience) and am lucky enough to have Ranorex Professional license and am currently running v. 5.4.4.2

If possible I would like to execute a similar 'method' as described above to return ALL objects on a particular form and somehow export the list of elements and an attribute, e.g. the Inner Text of each element, should one exist. Basically, Ranorex would search or scan(?) a page and dump all elements meeting this criteria to an external report?

As mentioned, I'm not sure where this 'method' would live or how it would be integrated into my project (as user code, I would imagine but I'm not sure how to set up this module, either as standalone or nested in a recording). To give you a sense of my inexperience, when I read the previous answers in the thread, I don't understand the logic inherent in the solutions enough to apply the syntax or code 'overhead' (setup within project/module) to my own problem. I have used some C# user code in other scenarios (handling popups) with a little success but not enough to understand the answers above.

Not sure this is possible but thanks for any info/help!

-PH

* * * EDIT * * *

I found this related post but I'm not sure how to implement this solution in my project (for example, I'm not sure which placeholder values in the code snippet are static and which are meant to be replaced with values from my own project, or where to place this code to begin with) - thank you again for any help:

http://www.ranorex.com/forum/capture-in ... t5616.html

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

Re: Get all elements of a certain type

Post by Support Team » Mon Apr 25, 2016 2:53 pm

Hello Peadar_H,

Please have a look at the source code below, which searches all elements within the corresponding form. After searching the elements, the data is stored to a CSV file on the desktop.

In order the code works for your solution, please adjust the name of the repository, the name of the form item and the path of the CSV file. Furthermore, replace <Attributname> with the attribute you want to export (for example InnerText).

Code: Select all

//Stringbuilder, which is used to export the data to a CSV file
var csv = new StringBuilder();
			
//Initialize the repository
ExportAllElementsRepository repo = ExportAllElementsRepository.Instance;
			
//Save all elements within the form-item to a list
var elements = repo.TestedApp.Self.Find(".//?");
			
//Add data to CSV file
foreach ( Element e in elements )
{					
   var newLine = string.Format("{0};{1}", e.ToString(),e.GetAttributeValue("<Attributename>"));
   csv.AppendLine(newLine);
}
			
//Write data to any location
System.IO.File.WriteAllText(@"C:\Users\Testuser\Desktop\elements.csv", csv.ToString());
I would suggest using a separate code module to implement this functionality. The code module could be located at any part of your test suite after the application started and the corresponding elements are visible. For example, you could place the module to the setup region of your test suite.
test_suite.png
test_suite.png (6.56 KiB) Viewed 4437 times
If you need further assistance or if you have any questions about the code, please do let me know.

Sincerely,
Johannes
.
Image

Peadar_H
Posts: 18
Joined: Tue Jun 02, 2015 8:48 pm

Re: Get all elements of a certain type

Post by Peadar_H » Fri Apr 29, 2016 2:27 pm

Thank you Johannes! I will try that and see how I get on - much appreciated.

Peadar_H
Posts: 18
Joined: Tue Jun 02, 2015 8:48 pm

Re: Get all elements of a certain type

Post by Peadar_H » Fri Apr 29, 2016 3:40 pm

Johannes where does this code live in the code module template that's created? Do I paste it after the...

{
Mouse.DefaultMoveTime = 300;
Keyboard.DefaultKeyPressTime = 100;
Delay.SpeedFactor = 1.0;
}

...section? Thanks again. -PH

* * * EDIT * * *

Wow, I got it to work. I'm not sure I'm doing it correctly but will post what I did in case this helps any other novices: I put it in the parameterless constructor(?) near the top rather than in the body of the class. Another error appeared for escape characters with the .csv file path so I searched S. Overflow to resolve this too. Thank you Johannes!