Page 1 of 1

Accessing advanced, dynamic data in user code

Posted: Mon Jul 03, 2017 3:16 pm
by lawrence
Hi all

In Ranorex spy (version 7.1. AUT: Java, SWT) I can see the advanced, dynamic properties of a TreeItem:
Spy.PNG
What I would like to do is to access the property "DisplayName" in "Data" of the "Dynamic" section in an user code method. I've managed to get the "Data" attribute from the element associated with the Tree Item, but I don't know how to get the "DisplayName" property from the data object. Here's the code I'm working on:
IList<TreeItem> treeItems = repo.AJApp.OverviewAreas.Bookmarks.Self.Find<TreeItem>(".//treeitem", 1000); 
IEnumerator<TreeItem> treeIterator = treeItems.GetEnumerator();
bool itemFound = false;
while (treeIterator.MoveNext() && !itemFound){
    TreeItem item = treeIterator.Current;
//I've managed to get the data object and can see in the debugger that it has a property called "Display Name"
    Object data = item.Element.GetAttributeValue("Data");
    //String displayNameText = <how can I get DisplayName from data>
    if(bookmarkText.Equals(displayNameText)){
         itemFound = true;
         item.DoubleClick();
       	 Report.Success("Bookmark for" + bookmarkText + " found and clicked");
    }
}
if(!itemFound){
    Validate.Fail("No bookmark with text  " + bookmarkText + " found");
}
In the debugger I can see that the "DisplayName" property exists:
debug.PNG
Many thanks for your help!

Cheers,
Lawrence

Re: Accessing advanced, dynamic data in user code

Posted: Mon Jul 03, 2017 3:42 pm
by Vaughan.Douglas
Have you tried something like this?
IList<TreeItem> treeItems = repo.AJApp.OverviewAreas.Bookmarks.Self.Find<TreeItem>(".//treeitem", 1000); 
IEnumerator<TreeItem> treeIterator = treeItems.GetEnumerator();
bool itemFound = false;
while (treeIterator.MoveNext() && !itemFound){
    TreeItem item = treeIterator.Current;
//I've managed to get the data object and can see in the debugger that it has a property called "Display Name"
    List data = item.Element.GetAttributeValue("Data");
    String displayNameText = data("displayName");
    if(bookmarkText.Equals(displayNameText)){
         itemFound = true;
         item.DoubleClick();
       	 Report.Success("Bookmark for" + bookmarkText + " found and clicked");
    }
}
if(!itemFound){
    Validate.Fail("No bookmark with text  " + bookmarkText + " found");
}
  • Changed the data var from Object to List
  • Use the "displayname" key to return the item
Although now that I think about it, it may be something more like this:
String displayNameText = data.item("displayName");

Re: Accessing advanced, dynamic data in user code

Posted: Mon Jul 03, 2017 4:08 pm
by lawrence
Vaughan.Douglas wrote: Changed the data var from Object to List
This doesn't compile,

Code: Select all

item.Element.GetAttributeValue("Data")
returns an object and not a list.
More specifically, as I can see in the debugger, it actually returns an object of type "JavaObjectWrapper", but not sure how to handle this type...

Re: Accessing advanced, dynamic data in user code

Posted: Mon Jul 03, 2017 4:39 pm
by lawrence
Managed to find a solution by using Ranorex.Plugin library (containing JavaObjectWrapper). In case anybody is interested here's the code:

Code: Select all

[UserCodeMethod]
       public  static void SelectBookmark(String bookmarkText){
       	if(!repo.AJApp.LesezeichenTabInfo.Exists(1000)){
	 	 	repo.AJApp.Navigation.GlobalActions.Lesezeichen.Click();       	
	    }
       	IList<TreeItem> treeItems = repo.AJApp.OverviewAreas.Lesezeichen.Self.Find<TreeItem>(".//treeitem", 1000); 
       	IEnumerator<TreeItem> treeIterator = treeItems.GetEnumerator();
       	bool itemFound = false;
       	
       	while (treeIterator.MoveNext() && !itemFound){
       		TreeItem item = treeIterator.Current;
       		JavaObjectWrapper dataWrapper = (JavaObjectWrapper)item.Element.GetAttributeValue("Data");
       		Object displayNameText = dataWrapper.GetProperty("DisplayName");
       		
       		if(displayNameText != null && bookmarkText.Equals(displayNameText.ToString())){
       			itemFound = true;
       			item.DoubleClick();
       			Report.Success("Bookmark for" + bookmarkText + " found and clicked");
       		}
       	}
       	if(!itemFound){
         		Validate.Fail("No bookmark with text  " + bookmarkText + " found");
         }  
       }


Thanks anyway for the help!

Lawrence

Re: Accessing advanced, dynamic data in user code

Posted: Mon Jul 03, 2017 4:51 pm
by Vaughan.Douglas
I missed a step when looking at the image, sorry. Also this is just sudo code, I don't know if it will actually compile as is.
Object data = item.Element.GetAttributeValue("Data");  
    String displayNameText = data.PropertyNames("displayName");
you should be able to add your Data object to a watch and from there drill in.