23 June 2010

QTP Help Guide: Using the Object Drag and Drop functions

Have you ever come across a web user interface which has the GUI divided in columns and you can resize the columns by holding the right or the left border with the Left Mouse Button Down and drag it to resize it in whatever manner you want. Well, in most cases these columns are re-sizable Html frames. Also, you may have noticed that some websites have layers or widgets that can be repositioned across the website GUI by a simple mouse drag and drop function. If you try to use Record and Play, you may not have much control over the user-interface of the application. I mean you do not have the freedom to vary the position of the drop based on some varying parameter.

QTP allows you to perform such actions using the Drag and Drop functionalities. To understand the functionality in a better way, we will take the example of the iGoogle website. You can add a number of widgets on this website and reposition them across the screen. To have a clear picture of the Application Under Test (AUT) and the automation that we will be performing here as an example, have a look at the video. We are going to drag a widget from one place to another within iGoogle.

Step 1. Create a repository with the desired object captured.

Step 2. Writing the code

x = Browser("iGoogle").Page("iGoogle").WebElement("Fifa Player Stats Widget").GetROProperty("x")
y = Browser("iGoogle").Page("iGoogle").WebElement("Fifa Player Stats Widget").GetROProperty("y")
Print x
Print y
'Now that you have the x and y co-ordinates above
'we can easily drag the object from this co-ordinate to the desired one
Browser("iGoogle").Page("iGoogle").WebElement("Fifa Player Stats Widget").Drag x,y
Wait(2)
'this statement simulates the left mouse button down pick action
Browser("iGoogle").Page("iGoogle").WebElement("Fifa Player Stats Widget").Drop x-2000,y-250
'this statement simulates the left mouse button up drop action
'the x and y co-ordinate for the drop sction depends upon the desired position
'desired position on the screen and can be manipulated

The above code derives the x and y co-ordinates of the object using the GetROProperty. Further, you must note that the x and y co-ordinates retrieved here is the upper left most point of the object so you need to manipulate the Drop x and y co-ordinates accordingly. It is very important that you capture the right object to perform the drag and drop functionalities.