拖放WinAppDriver无效 [英] Drag and drop in WinAppDriver doesn't work

查看:89
本文介绍了拖放WinAppDriver无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在WPF应用程序中自动化拖放行为的测试.一个自定义控件被拖到另一个控件上:

我尝试了几种方法:

动作拖放

  var moduleControl = mainWindow.GetToolboxModuleControl(moduleName);var actions = new Actions(_session);actions.DragAndDrop(moduleControl,mainWindow.WorkspaceControl).Perform(); 

点击并按住操作

  var moduleControl = mainWindow.GetToolboxModuleControl(moduleName);var actions = new Actions(_session);actions.ClickAndHold(moduleControl).MoveByOffset(200,0).Release().Perform(); 

驱动程序鼠标操作(来自解决方案

找到了答案.

WinAppDriver不会移动鼠标,而是传送"鼠标.这意味着鼠标光标不会一直以有限的速度一直拖动到目标,而是从开始位置跳到结束位置而没有任何中间位置.

在这种特殊情况下,它会引起问题.实际发生的情况:

  1. 光标移动到第一个元素的中心.MouseMove和其他鼠标移动事件在第一个元素上触发. DragDrop.DoDragDrop 方法未执行,因为鼠标左键按钮没有被按下.
  2. 按下鼠标左键.MouseDown,Click和其他相关事件在第一个元素上触发. DragDrop.DoDragDrop 方法是未执行,因为没有鼠标移动.
  3. 光标没有跳到第二个元素就跳到了第二个元素.MouseMove和其他鼠标移动事件在第二秒触发仅元素.由于没有在第一个元素上触发MouseMove事件按下鼠标左键,拖放过程将永远不会开始.

解决方案很简单:将光标移动到第一个元素边界内,然后跳到第二个元素边界或更改事件的开始和拖放过程(例如,MouseDown而不是MouseMove).我选择了后者:

 < UserControl ...MouseDown ="ToolboxModule_OnMouseDown"> 

  private void ToolboxModule_OnMouseDown(对象发送者,MouseButtonEventArgs e){base.OnMouseDown(e);var data = new DataObject();data.SetData("ModuleDescription",DataContext);DragDrop.DoDragDrop(this,data,DragDropEffects.Copy);} 

I try to automate tests of a drag and drop behavior in a WPF application. One custom control is dragged on another:

Drag and drop behavior implemented in the usual WPF way:

<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. 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. 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. 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. DragDrop.DoDragDrop method is not executed, because left mouse button is not pressed.
  2. Left mouse button is pressed. MouseDown, Click and other related event are firing on first element. DragDrop.DoDragDrop method is not executed, because there is no mouse movement.
  3. Cursor jumped to second element without touching the first. MouseMove and other mouse movement events are firing on second element only. Since no MouseMove event is firing on first element with left mouse button pressed, the drag and drop process never starts.

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

这篇关于拖放WinAppDriver无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆