Search code examples
c#hp-uftleanft

Expanding a UIObject with Native Class of TreeViewItem


Original Question

I have this expandable folder UIObject that I need to be able to expand to show all the subfolders. It can not be double clicked as this does not expand the folders. I saw from this documentation, https://admhelp.microfocus.com/leanft/en/14.02/NetSDKReference/HP.LFT.SDK~HP.LFT.SDK.Java.ITreeView.html as well as some others that there is a concept of an ITreeView and an ITreeViewNode.

How can I expand this element? I really just need some examples in code of how we can take an object, defeine it as a ITreeView and ITreeView node and expand it.

Result

Even though it is not the best solution, it is possible to do this using the Workaround suggested below and this is the method that made it happen

public void ExpandFolder(int index)
{
        IUiObject folder = ViewPage.FolderExplorer.Describe<IUiObject>(new UiObjectDescription
        {
            ObjectName = "TreeViewItem",
            Index = (uint)index
        });

        var expandButton = folder.AbsoluteLocation;
        expandButton.X = expandButton.X + 2;
        expandButton.Y = expandButton.Y + 4;

        Mouse.Click(expandButton);

        GeneralUtilities.Sleep(1);
}

In this case, there was a small drop down arrow to the left of the element. I couldn't identify that, so I identified the folder and manipulated the click. If anyone stumbles upon this and knows a more direct way to do this using LeanFT I would very much like to see an example. If not, and you are here trying to find help - I hope this helps you!


Solution

  • The theory

    In order to Expand and Collapse a Java ITreeView, these are the steps:

    1. Describe the ITreeView:

      ITreeView treeView = Desktop.Describe<IWindow>(new WindowDescription())
          .Describe<ITreeView>(new TreeViewDescription()
          {
              AttachedText = "Etc"
          });
      
    2. Get one of it's nodes:

      ITreeViewNode treeViewNode = treeView.GetNode("someNode");
      
    3. Expand or collapse it:

      treeViewNode.Expand(); treeViewNode.Collapse();
      

    The only expandable object is the ITreeViewNode (that is, this is the one that has the .Expand method), and the only way to get to the node is via an ITreeView, as shown above.

    In practice...

    You want to take an UIObject (I suspect this is what object identification center identified, right?) as an ITreeView, so that you can call Expand and Collapse on it?

    This is not possible.

    1. In these SDKs, every description is a generic element. In Java it's a UIObject, in Web it's a WebElement, etc..
    2. Those more unique, like a tree view, extend the generic one (UIObject) and adds one more identification property in the process
    3. In the ITreeView case, most probably it's the UIObject with NativeClass set as "javax.swing.JTree"

    If Object Identification Center didn't identify the expandable object as an ITreeView, it's because it isn't.

    Workaround

    If your goal is just to expand, and manually double clicking works, then you can:

    1. Identify the UIObject;
    2. Calculate the coordinates of the point you manually double click, relative to the upper left pixel of the UIObject. (E.G. 5 pixels down, 5 to the right)

      You can approximate it, go for a try and error, or use tools that can help.

    3. Use HP.LFT.SDK.Mouse class to double click that exact location:

      var loc = theUiButton.AbsoluteLocation;
      var p = new System.Drawing.Point(loc.X + 5, loc.Y + 5);
      
      Mouse.DoubleClick(p, MouseButton.Left);
      

      The reason why it doesn't work right now is because .DoubleClick, by default, performs double click in the center of the UIObject, and I have a hunch that's not where the expandable object is - so you need to provide fine tuning parameters.