Mouse DragAndDrop not working

Sep 6, 2011 at 9:42 PM

So I have a very complex application I am trying to drive with White, and so far have had a lot of success.  However, there is one area in my UI that requires a drag-and-drop operation, and I can see the mouse cursor moving to the right places in the UI (after I changed the DragStepCount in the App.config to > 100), but it never brings the item with it.  Do I have to do something to click the mouse first?  Shouldn't the DragAndDrop() method do that for me? 

Thinking it was my complex application, I wrote a simple app where I can drag a tree node onto a text box and have it show the node name.  Drag and drop manually works fine, but I can't get the White application to drag anything.  Again, I can see the mouse moving to the right places, but when the drag happens, there's no icon change on the mouse and no dropping on the text box.  What am I doing wrong?  See code below:

                Window calcWindow = app.GetWindow("StatCalc");
                Tree dragDropTree = calcWindow.Get<Tree>(SearchCriteria.ByAutomationId("dragDropTreeView"));
                TreeNode numbersNode = dragDropTree.Node("Numbers");
                if (!numbersNode.IsExpanded())
                    numbersNode.Expand();

                TreeNode otherNumbersNode = calcWindow.Get<TreeNode>(SearchCriteria.ByText("Other numbers"));
                if (!otherNumbersNode.IsExpanded())
                    otherNumbersNode.Expand();

                TreeNode itemToDrag = otherNumbersNode.Nodes[1];

                TextBox tb = calcWindow.Get<TextBox>(SearchCriteria.ByAutomationId("textBox1"));

                Mouse.Instance.DragAndDrop(itemToDrag, tb);

Thanks!

Sep 11, 2011 at 11:10 AM

Hi,

I am having the same problem. I used .Net Reflector to see what the draganddrop method is doing and I think the problem is that the method assumes the draganddrop is on the same window. I would expect that the method would try to focus on the target uiitem's window. And it doesn't happen. Hope that helps you somehow. Anyway, I think you should file a new issue on this subject. I know I will vote for it since I am having the same issue.

best regards,

Itzik

Sep 11, 2011 at 2:14 PM

Hi,

I have found a workaround for this issue:

Let's suppose you want to drag some item to a target window. Here is what you can do:

//Point the mouse for drag

Mouse.Instance.Location = draggedItem.ClickablePoint;

Mouse.LeftDown();

//Move the mouse a little down
Mouse.Instance.Location = new Point(draggedItem.ClickablePoint.X, draggedItem.ClickablePoint.Y + 1);

//Move to the new window
targetWindow.Focus();

//Set the point to drop
Mouse.Instance.Location = targetWindow.ClickablePoint;

//Drop
Mouse.LeftUp();

Hope that helps,

Itzik

Coordinator
Oct 2, 2011 at 6:49 PM

I am curious about one thing. Do you absolutely need

Mouse.Instance.Location = new Point(draggedItem.ClickablePoint.X, draggedItem.ClickablePoint.Y + 1);

Does it not work without it. I want to get to the bottom of why drag and drop doesn't work for people, although I do have unit tests which prove the same.

Apr 11, 2013 at 1:31 PM
Hi Itzik, Thanks a ton!

That fixed my issue.

With the built-in Mouse.DragAndDrop, I observed some erratic behavior.. I finally gave up on it when the cursor would move to the target location but the drop wouldn't happen (the test appears to freeze) until I manually moved the mouse. Once I move it, the drop registers and everything moves forward.

Replaced it with your block of code and it works consistently.
private void ManualDragAndDrop(UIItem draggedItem, UIItem targetItem)
        {
            //Point the mouse for drag

            Mouse.Instance.Location = draggedItem.ClickablePoint;

            Mouse.LeftDown();

           __ //Move the mouse a little down__
            Mouse.Instance.Location = new Point(draggedItem.ClickablePoint.X, draggedItem.ClickablePoint.Y + 1);

            //Move to the new window
            targetItem.Focus();

            //Set the point to drop
            Mouse.Instance.Location = targetItem.ClickablePoint;

            //Drop
            Mouse.LeftUp();
        }
Coordinator
Apr 14, 2013 at 4:27 AM
Could you provide me with some code which will freeze.

I recall hitting this issue, but think I have fixed it in the latest TestStack.White. But would like to verify, and put your fix in if it is still needed.
Apr 16, 2013 at 6:38 PM
Tried it with the latest nuget package v0.10.3.118. Still the same, the built in version worked 3/5 times. Twice it needed a push or froze for some time before the drop happened.

With the code snippet posted above, it continues to work 100%.

Can't post the app code/source due to IP issues. However if you'd like a LogStructure output for the drop/source, I could do that... Let me know what can help.

Once again, thanks for taking up White.
Coordinator
Apr 17, 2013 at 1:19 AM
I don't need your full source. But I would like to create a ui test which shows the issue, so I can protect against regressions.

Sent from my Windows Phone

From: [email removed]
Sent: ‎17/‎04/‎2013 1:39
To: [email removed]
Subject: Re: Mouse DragAndDrop not working [white:271672]

From: gishu_pillai

Tried it with the latest nuget package v0.10.3.118. Still the same, the built in version worked 3/5 times. Twice it needed a push or froze for some time before the drop happened.

With the code snippet posted above, it continues to work 100%.

Can't post the app code/source due to IP issues. However if you'd like a LogStructure output for the drop/source, I could do that... Let me know what can help.

Once again, thanks for taking up White.
Oct 25, 2013 at 9:45 PM
Your solution worked like a charm! Thanks for share!