Dragging one object into another

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.

Getting ready

Launch the Notepad application (C:Windows otepad.exe).

How to do it...

The process of creating a function for dragging a window consists of the following steps:

  1. The 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:
    How to do it...
  2. The X parameter inside the window, would be set equal to half the width of the Find window, while the Y parameter would be equal to 10 (the height of the heading of the window, tantamount to 21 pixels by default; so target coordinate will be in the middle of the window caption).
  3. The offset parameters should be calculated, taking the dimensions of both of the windows into account (the main Notepad window and the Find window).
  4. To figure out the distance along the horizontal line at which the window is to be relocated on the screen, we need to obtain the coordinates of the center of the main window, and add to it the distance from the border of the window down to that of the screen. Then we subtract the difference from the border of the Find window down to the beginning of the screen, subtracting half the width of the Find window (as we will be dragging the Find window by its middle, as the pivotal point), as shown in the following screenshot:
    How to do it...
  5. Similarly, the vertical offset is to be calculated by using the height of the window and the vertical distance.

    Tip

    If you have a hard time understanding the logic of the calculations, you may feel lead to draw schematic view of the location of the windows on the screen, and thus, shape up the formula by cracking numbers, and then transform those numbers into the meaningful variables.

  6. As a result of this, we can write up the following function. This function would open the Notepad and maximizes it over the whole screen, then opens the Find window and drags it to relocate onto the center of the main window. After this, the function restores the original size of the main window, and again, places the Find window on the center.
    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();
    }

How it works...

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.

There's more...

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);

Note

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.24.180