Forum Discussion

mikef's avatar
mikef
Staff
29 days ago

SE Tip : Drag-and-drop one object to another

Hi all,

A common challenge in automation is achieving precise drag-and-drop actions, especially when dealing with dynamic or responsive user interfaces. This question comes up regularly in different ways - how do I refine the drag-and-drop action in TestComplete?

TestComplete's built-in Drag action  is designed to drag a specific Alias object from a given point to another point, but at a pixel offset (i.e. drag Alias....Button by X/Y pixels). 
While useful as a "jumping off point", this approach can be problematic for obvious reasons (Dynamic UIs, changing screen resolutions, inconsistent offsets) leading to brittle tests.

Fortunately, TestComplete method parameters offer a high degree of customisation.  By evaluating and utilising exposed properties like ScreenTop/ScreenLeft, we can create more robust and adaptable drag-and-drop actions.
This allows us to instead dynamically reference the coordinates of a target object, or better still use exposed values in simple calculations, like figuring out the offset value for the Drag action. 

This Python script example calculates the offset using the common ScreenTop and ScreenLeft positions of both objects then passes the difference to the Drag action, allowing us to drag one given object to another given object with much more flexibility : 

def dragToObject(clientObj, targetObj): 
    # Using ScreenLeft property to drag horizontally; ScreenTop for vertical 
    fromObjectTop = aqObject.GetPropertyValue(clientObj, "ScreenTop") 
    fromObjectLeft = aqObject.GetPropertyValue(clientObj, "ScreenLeft") 
    
    toObjectTop = aqObject.GetPropertyValue(targetObj, "ScreenTop") 
    toObjectLeft = aqObject.GetPropertyValue(targetObj, "ScreenLeft") 
    
    dragY = toObjectTop-fromObjectTop 
    dragX = toObjectLeft-fromObjectLeft 
    
    Log.Message("Dragging "+aqConvert.IntToStr(dragX)+"px horizontally and"+aqConvert.IntToStr(dragY)+"px vertically") 
    
    clientObj.Drag(-1, -1, dragX, dragY)

You can then even utilise this in your KeywordTests, by changing the input parameter Mode to Onscreen Object, which enables the Object Picker : 

Now you have a way to drag one object to another - for example a value into a table?

Hope this gets the creative juices going - can you think of other ways you might handle dynamic values in other Action methods?

Regards,
Mike
TestComplete Solutions Engineer

  • Hi Mikef,

    Thanks for posting this solution. I was looking for something similar in Python without using the built-in Drag function that depends heavily on the coordinates. This method worked like a charm. Thanks a ton for sharing it. 

    • mikef's avatar
      mikef
      Staff

      Very welcome!

      You could expand this in other ways, such as using a built-in object method like FindChildByXpath, which also returns an Object type from a given Xpath. Then you could use a similar script, passing in Xpath values instead of specific alias objects to do the same job.