[英]Drag and drop in WinAppDriver doesn't work
I try to automate tests of a drag and drop behavior in a WPF application. 我尝试自动测试WPF应用程序中的拖放行为。 One custom control is dragged on another:
一个自定义控件被拖到另一个控件上:
Drag and drop behavior implemented in the usual WPF way : 以常规WPF方式实现的拖放行为:
<UserControl ...
MouseMove="ToolboxModule_OnMouseMove">
private void ToolboxModule_OnMouseMove(object sender, MouseEventArgs e)
{
base.OnMouseMove(e);
var data = new DataObject();
data.SetData("ModuleDescription", DataContext);
if (e.LeftButton == MouseButtonState.Pressed)
DragDrop.DoDragDrop(this, data, DragDropEffects.Copy);
}
<UserControl ...
Drop="WorkspaceView_OnDrop" AllowDrop="True">
private void WorkspaceView_OnDrop(object sender, DragEventArgs e)
{
var dropped = e.Data.GetData("ModuleDescription");
var viewModel = (WorkspaceViewModel)DataContext;
if (viewModel.ChainVm.AddModuleCommand.CanExecute(dropped))
viewModel.ChainVm.AddModuleCommand.Execute(dropped);
}
But when I try to automate this with WinAppDriver, the drag and drop does not work. 但是,当我尝试使用WinAppDriver自动执行此操作时,拖放操作将不起作用。 Cursor shape is not changed and nothing happens.
光标形状没有改变,什么也没有发生。
I've tried several approaches: 我尝试了几种方法:
Actions Drag and Drop 动作拖放
var moduleControl = mainWindow.GetToolboxModuleControl(moduleName);
var actions = new Actions(_session);
actions.DragAndDrop(moduleControl, mainWindow.WorkspaceControl).Perform();
Actions click and hold 点击并按住动作
var moduleControl = mainWindow.GetToolboxModuleControl(moduleName);
var actions = new Actions(_session);
actions.ClickAndHold(moduleControl).MoveByOffset(200, 0).Release().Perform();
Driver mouse operations (from example ) 驱动程序鼠标操作 (来自示例 )
_session.Mouse.MouseMove(moduleControl.Coordinates, 50, 50);
_session.Mouse.MouseDown(null);
_session.Mouse.MouseMove(mainWindow.WorkspaceControl.Coordinates, 100, 100);
_session.Mouse.MouseUp(null);
Driver mouse operations with delays 驱动程序鼠标操作有延迟
_session.Mouse.MouseMove(moduleControl.Coordinates, 50, 50);
Thread.Sleep(1000);
_session.Mouse.MouseDown(null);
Thread.Sleep(1000);
_session.Mouse.MouseMove(mainWindow.WorkspaceControl.Coordinates, 100, 100);
Thread.Sleep(1000);
_session.Mouse.MouseUp(null);
Nothing works. 什么都没有。 Any ideas what could be wrong and how to fix it?
有什么想法可能是错误的,以及如何解决?
When I try to move the app window by dragging it's title bar via WinAppDriver, it successfully moves the window. 当我尝试通过WinAppDriver拖动其标题栏来移动应用程序窗口时,它将成功移动该窗口。 So the dragging operations technically work, but not in the case of dragging a control within the window.
因此,拖动操作在技术上是可行的,但在窗口内拖动控件的情况下不起作用。
Found the answer. 找到了答案。
WinAppDriver doesn't move the mouse, but "teleport" it. WinAppDriver不会移动鼠标,而是“传送”鼠标。 It means mouse cursor is not dragged all the way to the target with finite speed, it is being jumped from start to end position without any intermediate positions.
这意味着鼠标光标不会一直以有限的速度一直拖动到目标,而是从开始位置跳到结束位置而没有任何中间位置。
In this particular case it causes the problem. 在这种特殊情况下,它会引起问题。 What actually happens:
实际发生的情况:
DragDrop.DoDragDrop
method is not executed, because left mouse button is not pressed. DragDrop.DoDragDrop
方法。 DragDrop.DoDragDrop
method is not executed, because there is no mouse movement. DragDrop.DoDragDrop
方法。 Solutions are simple: move the cursor within first element boundaries before jump to the second or change the event, where drag and drop process starts (MouseDown instead of MouseMove, for example). 解决方案很简单:将光标移动到第一个元素的边界内,然后再跳到第二个元素的边界,或者更改事件的拖放过程(例如,MouseDown而不是MouseMove)。 I chose the latter:
我选择了后者:
<UserControl ...
MouseDown="ToolboxModule_OnMouseDown">
private void ToolboxModule_OnMouseDown(object sender, MouseButtonEventArgs e)
{
base.OnMouseDown(e);
var data = new DataObject();
data.SetData("ModuleDescription", DataContext);
DragDrop.DoDragDrop(this, data, DragDropEffects.Copy);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.