Search code examples
seleniumcanvasgojs

Select Canvas Node using go.Diagram


I am able to click on the required canvas node, but also needed to select the node prior to clicking '+' button attached to it

//get a reference to the diagram object
const diagram = go.Diagram.fromDiv(document.getElementsByClassName("diagram-component")[0])
    
//using the diagram object find a node by name
const node = diagram.findNodesByExample({text: "capeutil"}).first()
    
//once you have the node you can expand it with the following command
node.findObject('ButtonButton').click(null, {part:node});

Need to help to select node by text

Selenium implementation

public void test_node_selection(String nodeName){
        sleep(5);
        JavascriptExecutor js = (JavascriptExecutor) context.getDriver();
        js.executeScript(
                "const diagram = go.Diagram.fromDiv(document.getElementsByClassName('diagram-component')[0]);" +
                        "const node = diagram.findNodesByExample({text: '" + nodeName + "'}).first()" +
                        "diagram.select(node);");
    }

Solution

  • If you have a reference to a node, you can select it by calling Diagram.select

    diagram.select(node);
    

    Note that this clears any other selection the Diagram may have.