Page 1 of 1

Dynamically create Adapter from RepoItemInfo

Posted: Thu Nov 12, 2015 12:08 am
by tvu
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!

Re: Dynamically create Adapter from RepoItemInfo

Posted: Thu Nov 12, 2015 8:23 am
by odklizec
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;

Re: Dynamically create Adapter from RepoItemInfo

Posted: Thu Nov 12, 2015 7:55 pm
by tvu
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.

Re: Dynamically create Adapter from RepoItemInfo

Posted: Thu Nov 12, 2015 10:39 pm
by Support Team
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

Re: Dynamically create Adapter from RepoItemInfo

Posted: Thu Nov 12, 2015 11:16 pm
by tvu
Thanks Alex! That works perfectly.