Simulating linking nodes

I am trying to test linking between 2 nodes,
using the robot class I try to link between two ports.

Instead of creating the link gojs just move the start node to the position of the other node.
what am I doing wrong?

let robot = new GoJsRobot(diagram);
let startNode = diagram.findNodeForKey(SUB_PROCESS_NODE_KEY);
let otherNode = diagram.findNodeForKey(SERVICE_TASK_NODE_KEY);

                let startPort = startNode.ports.first();
                let otherPort = otherNode.ports.first();
                let fromPoint = {
                    x: startPort.getDocumentPoint(Spot.Center).x,
                    y: startPort.getDocumentPoint(Spot.Center).y
                };
                let toPoint = {
                    x: otherPort.getDocumentPoint(Spot.Center).x,
                    y: otherPort.getDocumentPoint(Spot.Center).y
                };
                robot.mouseDown(fromPoint.x, fromPoint.y, 0, {});
                robot.mouseMove(toPoint.x, toPoint.y, 200, {});
                robot.mouseUp(toPoint.x, toPoint.y, 250, {});

I just tried modifying the extensions/Robot.html sample by modifying the node template:

              $(go.Node, "Auto",
                { portId: "", fromLinkable: true, toLinkable: true, cursor: "pointer" },
                $(go.Shape, "Rectangle",
                  { fill: "lightgray" }),
                $(go.TextBlock,
                  { margin: 3 },
                  new go.Binding("text", "key"))),

so that the center of each node’s port will be linkable. Note how in the original sample, the center of the port would pick the TextBlock, not the Shape, so the robot would not be doing a mouse-down on a port but on the text.

Remove the initial link between Lambda and Mu, and then this code works:

    function linkNodes() {
      var from = myDiagram.findNodeForKey("Lambda");
      var to = myDiagram.findNodeForKey("Mu");
      if (!from || !to) return;
      var frompt = from.port.getDocumentPoint(go.Spot.Center);
      var topt = to.port.getDocumentPoint(go.Spot.Center);
      robot.mouseDown(frompt.x, frompt.y, 0);
      robot.mouseMove((frompt.x+topt.x)/2, (frompt.y+topt.y)/2, 200);
      robot.mouseUp(topt.x, topt.y, 400);
    }

thanks for the demo, it helped.
my problem was that OI have mouseEnter and mouseLeave events that shows/hide the ports.

the solution I made is to call
startNode.mouseEnter(null,startNode,null);

and
startNode.mouseLeave(null,startNode,null);

that solve the problem

I think it would be better to call robot.mouseMove before the robot.mouseDown call. That would be more general than assuming the Node had a GraphObject.mouseEnter event handler. Same goes for the mouseLeave event handler.

mousemove did not work,
this is strange because it should.

Is there away to simulate node resize?

The problem is that a lot of the behavior is asynchronous. In this case, you need to give the system a chance to update its Adornments.

    function resizeNode() {
      var node = myDiagram.findNodeForKey("Lambda");
      if (!node) return;

      // select the node and make sure the handles appear
      myDiagram.commit(function(d) { d.select(node); });

      // find the bottom-left corner handle of the Resizing Adornment
      var adornment = node.findAdornment("Resizing");
      if (!adornment) return;
      var handle = null;
      adornment.elements.each(function(h) {
        if (h.alignment.equals(go.Spot.BottomLeft)) handle = h;
      });
      if (!handle) return;

      // now drag that handle leftwards and downwards
      var frompt = handle.getDocumentPoint(go.Spot.Center);
      var topt = frompt.copy().offset(-30, 40);
      robot.mouseDown(frompt.x, frompt.y, 100);
      robot.mouseMove((frompt.x+topt.x)/2, (frompt.y+topt.y)/2, 200);
      robot.mouseUp(topt.x, topt.y, 400);
    }

Oh, and if you didn’t want to select the node programmatically, as I did above in the call to Diagram.commit, you could do it robotically:

      robot.mouseDown(node.location.x+1, node.location.y+1, 0)
      robot.mouseUp(node.location.x+1, node.location.y+1, 10)
      myDiagram.commit(function() {});

The call to commitTransaction (which is called by Diagram.commit), or something else to make sure the Adornments are visible, is still needed.

Works like a charm. Many thanks

A post was split to a new topic: Using Robot to draw a new link