cancel
Showing results for
Did you mean:

## problem with a drag command

SOLVED
Highlighted
Occasional Contributor

## problem with a drag command

hi everyone, I have a problem with the drag command.
since I don't want to use coordinates, is it possible to use drag in some alternative method?
I don't want to use the coordinates because I wouldn't want to have problems if I have to change the screen resolution where they run my test.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

## Re: problem with a drag command

The drag command in testcomplete needs co-ordinates.  You need to know the object to be dragged and the destination and that is, for the Drag method, co-ordinate based.

but!  Never fear... I've got a code for that.

In Javascript&colon;

```function dragCalculatedDistance(startObject, destinationObject) {
var startPoint, targetPoint, dragX, dragY;
// Drag from the center of object A to the center of object B

startPoint = startObject.WindowToScreen(startObject.Width/2, startObject.Height/2);
targetPoint = destinationObject.WindowToScreen(destinationObject.Width/2, destinationObject.Height/2);

dragX = targetPoint.X - startPoint.X;
dragY = targetPoint.Y - startPoint.Y;

startObject.Drag(-1, -1, dragX, dragY);
}```

As noted in the comments, this will drag from roughly the center of the starting object to the center of the destination object.

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
4 REPLIES 4
Highlighted

## Re: problem with a drag command

Do you drag on same object, on different object ?

You can use coordinates based upon object ScreenLeft and ScreenTop properties.

Un sourire et ça repart

Highlighted
Community Hero

## Re: problem with a drag command

The drag command in testcomplete needs co-ordinates.  You need to know the object to be dragged and the destination and that is, for the Drag method, co-ordinate based.

but!  Never fear... I've got a code for that.

In Javascript&colon;

```function dragCalculatedDistance(startObject, destinationObject) {
var startPoint, targetPoint, dragX, dragY;
// Drag from the center of object A to the center of object B

startPoint = startObject.WindowToScreen(startObject.Width/2, startObject.Height/2);
targetPoint = destinationObject.WindowToScreen(destinationObject.Width/2, destinationObject.Height/2);

dragX = targetPoint.X - startPoint.X;
dragY = targetPoint.Y - startPoint.Y;

startObject.Drag(-1, -1, dragX, dragY);
}```

As noted in the comments, this will drag from roughly the center of the starting object to the center of the destination object.

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Highlighted
Community Hero

## Re: problem with a drag command

Hi,

As per the documentation for the .Drag() method, initial coordinates are "relative to the object, it is not a screen coordinate." The distance to execute dragging for is also calculated as a relative distance between two objects.

So, (as it was mentioned by @BenoitB), proper use of relevant .ScreenLeft/.ScreenTop/.Left/.Top properties of the required objects will provide you with resolution-agnostic code for .Drag().

Regards,
/Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
Highlighted
Community Manager

## Re: problem with a drag command

Thank you for helping all!

Hi @asavoia! Please mark the asnwer that helped you solve the issue as a solution

Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
New Here?
Announcements
Join TechCorner Challenge!
Want a fun and easy way to learn TestComplete? Try solving weekly TechCorner challenges and get into the Leaderboard!

Challenge Status

How to execute remote test and obtain results via Test Runner REST API

Participate!

Comparing content of HTML table with Excel file data

Participate!

Compare HTML table with Excel file and correct data in the file

Participate!

Retrieve Data from a Web Page

Participate!
Top Kudoed Authors
Interests
Latest Topics
Top Kudoed Posts