Dynamically create Adapter from RepoItemInfo

Class library usage, coding and language questions.
tvu
Posts: 195
Joined: Tue Apr 07, 2015 10:47 pm

Dynamically create Adapter from RepoItemInfo

Post by tvu » Thu Nov 12, 2015 12:08 am

I want to create a generalize method to set an adapter's attribute based on the RepoItemInfo.

Normally, I would do something like this:

Code: Select all

Ranorex.Text textAdapter = itemInfo.CreateAdapter<Ranorex.Text>(true);
Ranorex.Input inputAdapter = itemInfo.CreateAdapter<Ranorex.Input>(true);
But, how do you dynamically get the intended Adapter type at run time? I thought RepoItemInfo.GetType() would do the trick, but it doesn't.

The below thread shows me how to do it using the RxPath, but I was wondering if we can do it directly from the RepoItemInfo object.
http://www.ranorex.com/forum/dynamic-id ... t2753.html

The below code snippet is what I would like to do, but obviously it won't work:

Code: Select all

public void SetAttribute(RepoItemInfo itemInfo, string attribute, string value)
{
    if (itemInfo.Exist())
    {
        Ranorex.Adapter itemAdapter = itemInfo.CreateAdapter<itemInfo.GetType()>(true);
        itemAdapter.Element.SetAttributeValue(attribute, value);
    }
}
Thanks for the help!

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

Re: Dynamically create Adapter from RepoItemInfo

Post by odklizec » Thu Nov 12, 2015 8:23 am

Hi,

Check this post...
http://www.ranorex.com/forum/how-to-cre ... tml#p31227
I think this is exactly what you looking for.

Just replace Element in method 'CreateCorrectAdapterFromElement' with Ranorex.Core.Repository.RepoItemInfo and change this line:

Code: Select all

unknownTagElement.PreferredCapability.DisplayName;
with this:

Code: Select all

unknownTagElement.Element.PreferredCapability.DisplayName;
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

tvu
Posts: 195
Joined: Tue Apr 07, 2015 10:47 pm

Re: Dynamically create Adapter from RepoItemInfo

Post by tvu » Thu Nov 12, 2015 7:55 pm

Thanks odklizec, but this won't work. The RepoItemInfo class doesn't have an Element property and doesn't have a method to return an Element object so we can't do itemInfoObject.Element.PreferredCapability.DisplayName.

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

Re: Dynamically create Adapter from RepoItemInfo

Post by Support Team » Thu Nov 12, 2015 10:39 pm

If all you want to have is a generic adapter object in order to invoke Get/SetAttributeValue on, then just use the Unknown adapter:
Ranorex.Adapter itemAdapter = itemInfo.CreateAdapter<Unknown>(true);
itemAdapter.Element.SetAttributeValue(attribute, value);
The Unknown adapter does not require any specific capabilities, so you can always create that adapter for every element.

For more information have a look at the following section in the Ranorex User Guide:
http://www.ranorex.com/support/user-gui ... apter.html

Regards,
Alex
Ranorex Team
.
Image

tvu
Posts: 195
Joined: Tue Apr 07, 2015 10:47 pm

Re: Dynamically create Adapter from RepoItemInfo

Post by tvu » Thu Nov 12, 2015 11:16 pm

Thanks Alex! That works perfectly.