简体   繁体   English

拖放WinAppDriver无效

[英]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. 光标形状没有改变,什么也没有发生。

WinAppDriver拖放

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: 实际发生的情况:

  1. Cursor teleported to the center of first element. 光标移动到第一个元素的中心。 MouseMove and other mouse movement events are firing on first element. MouseMove和其他鼠标移动事件在第一个元素上触发。 DragDrop.DoDragDrop method is not executed, because left mouse button is not pressed. 由于未按下鼠标左键,因此不执行DragDrop.DoDragDrop方法。
  2. Left mouse button is pressed. 按下鼠标左键。 MouseDown, Click and other related event are firing on first element. MouseDown,Click和其他相关事件在第一个元素上触发。 DragDrop.DoDragDrop method is not executed, because there is no mouse movement. 由于没有鼠标移动,因此不会执行DragDrop.DoDragDrop方法。
  3. Cursor jumped to second element without touching the first. 光标跳到第二个元素而没有碰到第一个。 MouseMove and other mouse movement events are firing on second element only. MouseMove和其他鼠标移动事件仅在第二个元素上触发。 Since no MouseMove event is firing on first element with left mouse button pressed, the drag and drop process never starts. 由于在按下鼠标左键时第一个元素上没有触发MouseMove事件,因此拖放过程永远不会开始。

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.

相关问题 Seleniums拖放不适用于AngularJS - Seleniums Drag & Drop doesn't work with AngularJS 使用 winappdriver 最大化桌面窗口无法正常工作 - Maximizing desktop window with winappdriver doesn't work correctly 实施拖放后,CheckOnClick不起作用 - CheckOnClick doesn't work when drag-and-drop is implemented Listview和RichTextBox之间的拖放不起作用 - Drag & Drop between Listview and RichTextBox doesn't work 通过BHO从outlook拖放到Internet Explorer可以在x32 / 86机器上运行 - Drag/drop from outlook to internet explorer via BHO doesn't work on x32/86 machines 在visual studio express 2013中“拖放形成”计时器不起作用 - “drag and drop to form” timer in visual studio express 2013 doesn't work jQuery 当我添加 FriendlyUrl 程序集时,拖放不起作用 - jQuery Drag and Drop doesn't work when I add in FriendlyUrl assembly MonitorFromWindow(DefaultToNearest)在拖动期间不起作用 - MonitorFromWindow(DefaultToNearest) doesn't work during drag Outlook拖放-ActiveExplorer()。Selection()不包含所有项目 - Outlook Drag and Drop - ActiveExplorer().Selection() doesn't contain all items 为什么我的自定义控件不接收拖放事件? - Why doesn't my custom control receive drag/drop events?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM