How to use org.eclipse.swtbot.swt.finder.widgets.SWTBotTree.getNode(String nodeText,int index) method?

205 views Asked by At

I am using this method in my framework and it started giving me exception like " org.eclipse.swtbot.swt.finder.exceptions.AssertionFailedException: assertion failed: The index (1) was more than the number of nodes (1) in the tree."

Following is the structure:

<RootNode>
    <ChildNode1>
        <GrandChildNode1/>
    </ChildNode1>
    <ChildNode2/>
</RootNode>

Below is the code. If I print the node at index 2 in "isTreeNodeWithIndexPresent" method, it gives correct as "ChildNode2". It only thrown exception when goes through the getNode method.

public boolean isTreeNodeWithIndexPresent(final String node, final int nodeIndex) {
    boolean status = false;
    if (node.length() > 0) {
        final SWTBotTreeItem[] botTreeItem = tree.getAllItems();
        for (final SWTBotTreeItem swtBotTreeItem : botTreeItem) {
            status = swtBotTreeItem.getNode(node, nodeIndex).isVisible();
        }
    }
    return status;
}.

    public SWTBotTreeItem getNode(final String nodeText, final int index) {
            List<SWTBotTreeItem> nodes = getNodes(nodeText);
            Assert.isTrue(index < nodes.size(),
                    MessageFormat.format("The index ({0}) was more than the number of nodes ({1}) in the tree.", index, nodes.size()));
            return nodes.get(index);
        }
1

There are 1 answers

0
Marteng On

SWTBotTreeItem#getNode(nodeText, index) returns the node with the given text that is a direct child of the item the method is called on.

The index can be used in case the tree item has multiple nodes with the same name. For example, if you have two children with name "foobar" you can access the second node with item.getNode("foobar", 1) (and the first with item.getNode("foobar", 0) or item.getNode("foobar")).