cancel
Showing results for
Did you mean:

## [TechCorner Challenge #2] Dragging One Element to Another

SOLVED
Highlighted
Community Manager

## [TechCorner Challenge #2] Dragging One Element to Another

Hi Community! Here's one more interesting challenge for you to complete!

Drag and drop is quite a simple task that is performed in TestComplete by using the Drag action. However, it might be that the destination changes its position on the screen, so you need to recalculate the destination coordinates every time.

Task: Create a function that takes two objects as parameters and drags the first object to the center of the second one.

Difficulty:

Have fun!🙂

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
Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

## Dragging One Element to Another

Task: Create a function that takes two objects as parameters and drags the first object to the center of the second one.

This is a solution created for [TechCorner Challenge #2]

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

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
Tags (1)
2 REPLIES 2
Highlighted
Community Hero

## Dragging One Element to Another

Task: Create a function that takes two objects as parameters and drags the first object to the center of the second one.

This is a solution created for [TechCorner Challenge #2]

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

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
Tags (1)
Highlighted
Community Manager

## Re: Your Input Needed - Dragging One Element to Another

Yes, this is perfect, thank you @tristaanogre !🙂

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

Compare images using the Region Checkpoint

See replies!

Converting UTC TimeDate in an Excel file

See replies!

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

Participate!

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

Participate!
Top Kudoed Authors
Interests
Latest Topics
Top Kudoed Posts