Forum Discussion

asavoia's avatar
asavoia
Occasional Contributor
5 years ago

problem with a drag command

hi everyone, I have a problem with the drag command.
since I don't want to use coordinates, is it possible to use drag in some alternative method?
I don't want to use the coordinates because I wouldn't want to have problems if I have to change the screen resolution where they run my test.

  • The drag command in testcomplete needs co-ordinates.  You need to know the object to be dragged and the destination and that is, for the Drag method, co-ordinate based.

     

    but!  Never fear... I've got a code for that.

     

    In Javascript:

     

    function dragCalculatedDistance(startObject, destinationObject) {
        var startPoint, targetPoint, dragX, dragY;
        // Drag from the center of object A to the center of object B
    
        startPoint = startObject.WindowToScreen(startObject.Width/2, startObject.Height/2);
        targetPoint = destinationObject.WindowToScreen(destinationObject.Width/2, destinationObject.Height/2);
    
        dragX = targetPoint.X - startPoint.X;
        dragY = targetPoint.Y - startPoint.Y;
    
        startObject.Drag(-1, -1, dragX, dragY); 
    }

    As noted in the comments, this will drag from roughly the center of the starting object to the center of the destination object.

  • BenoitB's avatar
    BenoitB
    Community Hero

    Do you drag on same object, on different object ?

    You can use coordinates based upon object ScreenLeft and ScreenTop properties.

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      The drag command in testcomplete needs co-ordinates.  You need to know the object to be dragged and the destination and that is, for the Drag method, co-ordinate based.

       

      but!  Never fear... I've got a code for that.

       

      In Javascript:

       

      function dragCalculatedDistance(startObject, destinationObject) {
          var startPoint, targetPoint, dragX, dragY;
          // Drag from the center of object A to the center of object B
      
          startPoint = startObject.WindowToScreen(startObject.Width/2, startObject.Height/2);
          targetPoint = destinationObject.WindowToScreen(destinationObject.Width/2, destinationObject.Height/2);
      
          dragX = targetPoint.X - startPoint.X;
          dragY = targetPoint.Y - startPoint.Y;
      
          startObject.Drag(-1, -1, dragX, dragY); 
      }

      As noted in the comments, this will drag from roughly the center of the starting object to the center of the destination object.

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    As per the documentation for the .Drag() method, initial coordinates are "relative to the object, it is not a screen coordinate." The distance to execute dragging for is also calculated as a relative distance between two objects.

    So, (as it was mentioned by BenoitB), proper use of relevant .ScreenLeft/.ScreenTop/.Left/.Top properties of the required objects will provide you with resolution-agnostic code for .Drag().

     

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Thank you for helping all!

       

      Hi asavoia! Please mark the asnwer that helped you solve the issue as a solution:smileyhappy: