[Coded UI Test(CUIT)] 手動撰寫拖曳的方法

[Coded UI Test(CUIT)] 手動撰寫拖曳的方法

用錄製的方式錄製拖曳功能測試的時候,會發現到拖曳步驟時會停住不會動,但是輕輕移動滑鼠它就會正常進行,用手動撰寫程式碼就不會有這個問題了:


Playback.PlaybackSettings.WaitForReadyTimeout = 0;
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.Disabled;

try
{
    //把DragItem從(47,12)移動到(35,4)的位置
    //確保要拖曳至的物件是可以點選的
    tTargetItem.EnsureClickable(); 
    int x = tDragItem.BoundingRectangle.X;
    int y = tDragItem.BoundingRectangle.Y;
    int w = tDragItem.BoundingRectangle.Width;
    int h = tDragItem.BoundingRectangle.Height;

    Mouse.StartDragging(tDragItem, new Point(47, 12), MouseButtons.Left, ModifierKeys.None);
    //讓Mouse稍微移動以觸發Dispatcher進行工作
    Mouse.Move(new Point(x + (w / 2) + 1, y + (h / 2)));  

    Mouse.StopDragging(tTargetItem, new Point(35, 4));
}
finally
{
    //還原WaitForReadyTimeout
    Playback.PlaybackSettings.WaitForReadyTimeout = waitForReadyTimeOut;
    Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.UIThreadOnly;
}

參考來源:Does Dragging action on WPF controls hangs