【发布时间】:2014-01-19 23:12:19
【问题描述】:
我需要自动化一些程序的常见活动,例如 Microsoft Word。这些测试不是用于商业用途,而是用于回归测试。
到目前为止,我只是移动鼠标,然后在按钮上方单击鼠标左键。 但是,我们的程序会验证按钮是否处于“按下”状态。当自动化按下鼠标点击按钮时,按钮不会按下(按钮不会“下沉”)
这是为什么呢?如何使自动鼠标单击使按钮像普通鼠标单击一样被“按下”?
在这种情况下,顺便说一句,我指的是快速工具栏中的“保存”按钮。
我使用的代码很简单——首先我找到我想要点击的对象,然后使用这段代码;
public void MouseClick(params string[] args)
{
AutomationElement automationElement = elementUtils.FindObjectFullPath(args[1], args[4], args[5]);
string action = args[args.Length - 1];
if (automationElement == null)
return;
FocusApplicationExp(automationElement);
try
{
int x = (int)automationElement.GetClickablePoint().X;
int y = (int)automationElement.GetClickablePoint().Y;
MouseMoveExp(x, y);
MouseAction(action, automationElement);
}
catch (Exception)
{
Console.WriteLine("Failed to get clickable point for " + automationElement.Current.Name + " - Trying to get coordinates of bounding rectangle");
tl.AddLine("Failed to get clickable point for " + automationElement.Current.Name + " - Trying to get coordinates of bounding rectangle");
res.AddLine("Failed to get clickable point for " + automationElement.Current.Name + " - Trying to get coordinates of bounding rectangle");
int x = (int)(automationElement.Current.BoundingRectangle.Right - automationElement.Current.BoundingRectangle.Width / 2);
int y = (int)(automationElement.Current.BoundingRectangle.Bottom - automationElement.Current.BoundingRectangle.Height / 2);
MouseMoveExp(x, y);
MouseAction(action, automationElement);
return;
}
}
private void MouseAction(string action, AutomationElement automationElement)
{
switch (action)
{
case "Left":
Console.WriteLine("Left clicking on: " + automationElement.Current.Name);
tl.AddLine("Left clicking on: " + automationElement.Current.Name);
AutomationUtility.DoMouseClick();
res.AddLine("Left clicked on: " + automationElement.Current.Name);
break;
case "Right":
Console.WriteLine("Right clicking on: " + automationElement.Current.Name);
tl.AddLine("Right clicking on: " + automationElement.Current.Name);
AutomationUtility.DoMouseRightClick();
res.AddLine("Right clicked on: " + automationElement.Current.Name);
break;
case "Double":
Console.WriteLine("Double clicking on: " + automationElement.Current.Name);
tl.AddLine("Double clicking on: " + automationElement.Current.Name);
AutomationUtility.DoMouseDoubleClick();
res.AddLine("Double clicked on: " + automationElement.Current.Name);
break;
default:
break;
}
}
public static void DoMouseClick()
{
Mouse.Click(MouseButton.Left);
}
我确实尝试了一些变化,例如在按钮按下和按下之间设置一些延迟,以防事件发生但太快。但是即使我按住按钮,按钮也永远不会进入“按下”状态(当然是在自动化中)
更清楚一点 - 自动化确实有效,它确实点击了按钮,它确实会导致单词保存。这里的问题是使按钮在视觉上被按下(并且在程序方面,改变它的状态)。
我不确定这是否意味着什么,但识别按钮状态和事件的系统是“UX”,并且基于 Win32 的标识符确实以某种方式找到了“按下”状态。
编辑:过去几天我一直在阅读它,事情变得越来越清晰。 我使用的自动化是基于 UIAutomation,但是按钮状态“Pressed”是一个 MSAA 状态,它没有移动到 UI 对象的 UIAutomation 生成,所以基于 UIAutomation 的自动化不会调用这个状态。 换句话说,我需要找到一种方法来使用 UIAutomation 代码将 MSAA 信号发送到 UIAutomation 元素(包含 IAccessible 接口)。问题是,它只支持另一种方式(将屏蔽为 MSAA 信号的 UIAutomation 信号发送到 MSAA 元素,它不支持 MSAA-Only 信号,例如“Enter state Pressed”)
【问题讨论】:
-
如您所见,这台机器的点击速度太快了。我们看不到您尝试过的替代方案。这对于 UI 测试应该无关紧要,重要的是 Word 看到鼠标点击。
-
问题是,我们的程序(不是自动化)收集事件,并使用它们来了解是否执行了操作。在这种情况下,鼠标单击名称为“保存”和父级“快速工具栏”的按钮,对象状态:已按下、可聚焦。所以我需要自动点击鼠标来像人类一样工作。
标签: c# testing ms-word automation simulation