Iterate over elements found for rooted folder

Experiences, small talk, and other automation gossip.
tkikalishvili
Posts: 9
Joined: Tue Nov 14, 2017 5:05 pm

Iterate over elements found for rooted folder

Post by tkikalishvili » Wed Sep 05, 2018 4:43 pm

Hey,

I can have multiple AccountListItem in my project which shows account data in it. Each accountListItem contains PlatformName text, URL text etc.
So as a solution I chose to have AccountListItem as rooted folder and inside it all the items related to accounts(see screenshot).

When I have multiple accounts Spy shows that multiple elements have been found for this rooted folder.
multipleAcc.JPG
Now I want to iterate over all the accounts I have and access items under it, smth like this described in code below:

Code: Select all

IList<AccountListItemFolder> thisAccountList = repo.Accounts.AccountListItem;
            
            foreach(AccountListItemFolder account in thisAccountList)
            {
            	Report.Info(account.PlatformNameLbl.TextValue);
            }
but unfortunately, I get an error that AccountListItemFolder cannot be implicitly converted to System.Collections.Generic.IList<AccountListItemFolder>.

I tried many different ways for casting but it doesn't work.

And if I try

Code: Select all

IList<ListItem> a = Host.Local.Find<ListItem>(repo.Accounts.AccountListItem.AbsoluteBasePath);
foreach(ListItem account in a)
            {
            	account.Click();
            }
I can iterate over listItems, but I lose access to account.PlatformNameLbl because it becomes listItem and not AccountListItemFolder.

and last point, I also tried this code:

Code: Select all

IList<AccountListItemFolder> a = Host.Local.Find<AccountListItemFolder>(repo.Accounts.AccountListItem.AbsoluteBasePath);
but as a result I get that AccountListItemFolder cannot be used as type parameter 'T' in generic type or method because there is no implicit conversion to Ranorex.Adapter.

Has anyone of you solved this kind of problem? How can I iterate over rooted folder?
You do not have the required permissions to view the files attached to this post.
Last edited by tkikalishvili on Thu Sep 06, 2018 12:29 pm, edited 1 time in total.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Iterate over elements found for rooted folder

Post by Support Team » Wed Sep 05, 2018 5:45 pm

Hi tjujalishvili,

You need to create an adapter for each item returned from a repository element. The CreateAdapters<T>() method will return an IList of elements for you to iterate over and is available on the repository Info object (add "Info" to the end of the repository object in your code). Check out the example code below that will look through a table's cells:

Code: Select all

var repo = SampleSolutionRepository.Instance;
IList<TdTag> allCells = repo.HTMLTables.AllCellsInfo.CreateAdapters<TdTag>();
foreach (TdTag cell in allCells) 
{
	//Do Stuff
}
Also, since you are using a rooted folder, you may need to do MyRootedFolder.SelfInfo.CreateAdapters instead of MyRootedFolderInfo.CreateAdapters.

I hope this helps!

Cheers,
Ned

tkikalishvili
Posts: 9
Joined: Tue Nov 14, 2017 5:05 pm

Re: Iterate over elements found for rooted folder

Post by tkikalishvili » Thu Sep 06, 2018 9:45 am

Hi Ned,

Thanks for quick answer.

it did not help because problem is passing AccountListItemFolder(which is type of RootedFolder) as a type parameter <T> because there is no implicit reference conversion from AccountListItemFolder to Ranorex.Adapter.

Code: Select all

IList<AccountListItemFolder> a = repo.Accounts.AccountListItem.SelfInfo.CreateAdapters<AccountListItemFolder>();
I think it will be reproducible in your example as well, if you put custom rooted folder type which is created by you instead of TdTag.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Iterate over elements found for rooted folder

Post by Support Team » Wed Sep 12, 2018 6:30 pm

Hi tjujalishvili,

You are correct in that a rooted folder is slightly different. My apologies for overlooking this in your screenshot! To access the actual element of a rooted folder, you must append .Self or .SelfInfo (we need .SelfInfo specifically for the CreateAdapators<T>() method).
1.png
2.png
Please let me know if there is anything else I can clarify!

Cheers,
Ned
You do not have the required permissions to view the files attached to this post.

tkikalishvili
Posts: 9
Joined: Tue Nov 14, 2017 5:05 pm

Re: Iterate over elements found for rooted folder

Post by tkikalishvili » Fri Sep 21, 2018 2:41 pm

Hi Ned,

I think we still didn't understand each other so I will try to explain on your example.

The problem is in //Do Stuff, there I cannot do the stuff which I want to do.

In your example you have LbCategory as rooted folder and under it you have listItems Movie,Music and Other.

In code you iterate over the list of LbCategories

Code: Select all

foreach(List list in AllList)
{
	//Imagine I want to to do here something like this
	Validate.IsTrue(list.Movie.textValue == "Gladiator");
}
How would you do it? As the list has type of List instead of LbCategory, you cannot access list.Movie.

Regards,
Tornike

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Iterate over elements found for rooted folder

Post by Support Team » Fri Sep 21, 2018 4:54 pm

Hi Tornike,

Before I further explain this method of looping over elements, be aware this type of loop should only be used if the list of elements is truly dynamic and/or unpredictable. If you know exactly what should exist, it is advised to create a repository element for each expected item and validate them individually outside of a loop. Nonetheless, continue reading for more information on how to use this loop.

My previous example will only iterate over each list returned from LbCategory and will not do anything with the child listitem elements. To iterate over the listitems, it is recommended to create a repository element with an RxPath that returns all list items. Once that is completed, you can iterate over each listitem. Please see below for an example of this (but with option elements instead of listitems).

Example AUT:
https://www.ranorex.com/web-testing-examples/vip/

Example RxPath: (returns 6 items)

Code: Select all

/dom[@pageurl='https://www.ranorex.com/web-testing-examples/vip/']//select[#'Category']/option
Example Code:

Code: Select all

var repo = Sample_ProjectRepository.Instance;
IList<OptionTag> allOptions = repo.VIPDom.AllOptionsInfo.CreateAdapters<OptionTag>();
foreach (OptionTag option in allOptions)
{
	Report.Info("Selecting option: " + option.Element.GetAttributeValueText("innertext"));
	option.Select();
}
I hope this helps clarify everything! If you continue to experience issues, please provide a Ranorex Snapshot so I may better understand your environment and I will be happy to help you automate these elements.

Cheers,
Ned

tkikalishvili
Posts: 9
Joined: Tue Nov 14, 2017 5:05 pm

Re: Iterate over elements found for rooted folder

Post by tkikalishvili » Mon Sep 24, 2018 1:51 pm

Thank you for your answer Ned.

Yes, the content is dynamic.

The approach which you suggested regarding having array of ListItems itself will put me into another issue, how to verify that they are under the correct parent and not under some other parent.

But anyways, I found out what I was interested in, that I cannot create adapter for custom rooted folder and need to search for some other solution.

Regards,
Tornike

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Iterate over elements found for rooted folder

Post by Support Team » Mon Sep 24, 2018 5:09 pm

Hi tkikalishvili,

You can use CreateAdapter and CreateAdapters on any repository info object, this includes rooted folders. When using a rooted folder, you must use .SelfInfo to access the rooted folders info object (e.g. repo.MyRootedFolder.SelfInfo).

I suspect I still may not have a full grasp on what you are trying to achieve. If you are able to provide a Ranorex Snapshot of these dynamic elements, I will be able to better understand your environment and may be able to better answer your questions.

Kind Regards,
Ned

Shakib
Posts: 32
Joined: Tue Jan 06, 2015 7:22 am

Re: Iterate over elements found for rooted folder

Post by Shakib » Wed Sep 26, 2018 10:28 am

Hi,

Try to use the below code :
var thisAccountList = repo.Accounts.AccountListItem.FindChildren<Text>();

foreach(Text account in thisAccountList)
{
Report.Info(account.TextValue);
}