With the help of the Drag
method in TestComplete, we are able to drag-and-drop any object onto the predefined location in terms of pixels vertically and horizontally (on condition, of course, that the object supports the drag-and-drop behavior); however, there is no built-in possibility to drag one object onto or over another.
We can create a function to tackle such a feat by using coordinates of the objects. The task at hand is writing a script that would drag the Find window in the Notepad and place the same on the center of the main window, regardless of its dimensions and initial whereabouts.
The process of creating a function for dragging a window consists of the following steps:
Drag
method takes four parameters: coordinates inside the window on which the mouse-click is expected to be made at the point of dragging (X and Y) and offset coordinates by width and height for dropping (offsetX
and offsetY
). In our case, the object that is being dragged-and-dropped is the Find window which is shown in the following screenshot:function testDragDrop() { var pNotepad = Sys.Process("notepad"); var wNotepad = pNotepad.Window("Notepad"); wNotepad.Activate(); wNotepad.Keys("Some text"); wNotepad.Maximize(); wNotepad.MainMenu.Click("Edit|Find..."); var wFind = pNotepad.Window("*", "Find"); var offsetX = wNotepad.Width/2 + wNotepad.ScreenLeft - wFind.ScreenLeft - wFind.Width/2; var offsetY = wNotepad.Height/2 + wNotepad.ScreenTop - wFind.ScreenTop - wFind.Height/2; wFind.Drag(wFind.Width/2, 10, offsetX, offsetY); wNotepad.Restore(); offsetX = wNotepad.Width/2 + wNotepad.ScreenLeft - wFind.ScreenLeft - wFind.Width/2; offsetY = wNotepad.Height/2 + wNotepad.ScreenTop - wFind.ScreenTop - wFind.Height/2; wFind.Drag(wFind.Width/2, 10, offsetX, offsetY); wFind.Close(); }
With the help of the Width
, Height
, ScreenLeft
, and ScreenTop
properties of both of the controls elements (in our use case, we are talking about windows), we calculate the distance at which it is necessary to relocate the object to have it appear over the center of another object in play.
As seen from this example, the code for calculation of the coordinates is re-iterated, and this is why it is better to extract it into a separate function. The function would accept two parameters: the object that is being dragged, and the pivotal object onto which the drag-and-drop is targeted.
Here is an example of universal function which can be used for dragging objects:
function dragObject(objDrag, objTo) { var offsetX = objTo.Width/2 + objTo.ScreenLeft - objDrag.ScreenLeft - objDrag.Width/2; var offsetY = objTo.Height/2 + objTo.ScreenTop - objDrag.ScreenTop - objDrag.Height/2; objDrag.Drag(objDrag.Width/2, 10, offsetX, offsetY); }
Our initial code could be simplified by replacing the blocks from the three lines of code, which calculate coordinates and evoke the Drag
method, with the following singular function:
dragObject(wFind, wNotepad);
The Y parameter has been hard-coded as being equal to 10, since we have been at that point in the window. To drag other objects, it makes sense to replace this value for half the height of the object that is being dragged (objDrag.Height/2
), so that mouse-click would always target the center of the object.
3.129.24.180