我需要自动执行一些程序的常见活动,例如Microsoft Word。测试不是用于商业用途,而是用于回归测试。
到现在为止,我只需移动鼠标,然后在按钮上方单击鼠标左键。 但是,我们的程序会验证按钮是否处于“按下”状态。当自动按下按钮并单击鼠标时,该按钮不会按下(按钮不会“下沉”)
为什么?如何使鼠标自动点击使按钮被“按下”,就像正常的鼠标点击一样?
在这种情况下,BTW,我指的是快速工具栏中的“保存”按钮。
我使用的代码非常简单 - 首先我找到了我要点击的对象,然后使用了这段代码;
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状态,没有移动到UIAutomation生成的UI对象,因此基于UIAutomation的自动化不会调用此状态。 换句话说,我需要找到一种方法来使用UIAutomation代码将MSAA信号发送到UIAutomation元素(包含IAccessible接口)。问题是,它只是反过来支持(将UIAutomation信号作为MSAA信号屏蔽到MSAA元素,这不支持MSAA-only信号,例如“输入状态按下”)答案 0 :(得分:1)
我不知道你是否使用这种方式,但它可能对点击部分有帮助
public void ClickOnPoint(IntPtr wndHandle )
{
//for handle you can use (this.Handle)
//you can assign point directly or recalibrate as you did already
Point buttonPoint(100, 500)
// store old pos if needed
POINT oldPoint;
GetCursorPos(out oldPoint);
// get screen coordinates
ClientToScreen(wndHandle, ref buttonPoint);
// set cursor on coords, and press mouse
SetCursorPos(buttonPoint.X, buttonPoint.Y);
//you can change the timing
mouse_event(0x00000002, 0, 0, 0, UIntPtr.Zero); // left mouse button down
mouse_event(0x00000004, 0, 0, 0, UIntPtr.Zero); // left mouse button up
// return mouse
SetCursorPos(oldPoint.X, oldPoint.Y);
}
答案 1 :(得分:1)
也许使用SendInput结帐。
在下面的帖子中,我发布了几个使用SendInput的类。
How to move the cursor or simulate clicks for other applications?
注意:如果我延迟,我可以看到保存按钮变为橙色。我没有看到它没有延迟就变成橙色。
public static void LeftClick()
{
DoMouse( NativeMethods.MOUSEEVENTF.LEFTDOWN, new Point( 0, 0 ) );
System.Threading.Thread.Sleep( 200 );
DoMouse( NativeMethods.MOUSEEVENTF.LEFTUP, new Point( 0, 0 ) );
}
答案 2 :(得分:0)
好吧,我找不到我要找的答案,但这似乎无论如何都有效;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
Thread.Sleep(100);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
public static void RightMouseClick(int xpos, int ypos)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN, xpos, ypos, 0, 0);
Thread.Sleep(100);
mouse_event(MOUSEEVENTF_RIGHTUP, xpos, ypos, 0, 0);
}
我不确定Derek或Za7ef的答案是多么不同,因为我试过它们。我已经将代码更改了几次,因此它可能有不同的原因。不管怎样,谢谢!