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