我正在尝试单击外部Windows应用程序上的按钮。以下代码成功找到元素,将父窗口置于焦点,然后“手动”单击按钮
这样可行......
Process tProcess = Process.GetProcesses().FirstOrDefault(x => x.MainWindowTitle.StartsWith("MainWindowName"));
if (tProcess != null)
{
TestStack.White.Application application = TestStack.White.Application.Attach(tProcess.Id);
var tWindow = application.GetWindow(SearchCriteria.ByAutomationId("SubWindowName"), InitializeOption.NoCache);
SearchCriteria searchCriteria = SearchCriteria.ByAutomationId("btnCalibrate");
var calibrateBtn = tWindow.Get<TestStack.White.UIItems.Button>(searchCriteria);
tWindow.Focus();
var clickablePoint = calibrateBtn.AutomationElement.GetClickablePoint();
Mouse.Instance.Click(clickablePoint);
}
问题是Mouse.Instance.Click(clickablePoint);
移动光标,理想情况下我不想移动光标。
我的初始代码尝试使用以下
直接点击按钮Process tProcess = Process.GetProcesses().FirstOrDefault(x => x.MainWindowTitle.StartsWith("MainWindowName"));
if (tProcess != null)
{
TestStack.White.Application application = TestStack.White.Application.Attach(tProcess.Id);
var tWindow = application.GetWindow(SearchCriteria.ByAutomationId("SubWindowName"), InitializeOption.NoCache);
SearchCriteria searchCriteria = SearchCriteria.ByAutomationId("btnCalibrate");
var calibrateBtn = tWindow.Get<TestStack.White.UIItems.Button>(searchCriteria);
tWindow.Focus();
calibrateBtn.Click();
}
但每次都会出现以下错误
TestStack.White.AutomationException
HResult=0x80131500
Message=Cannot perform action on Button. AutomationId:btnCalibrate, Name:Calibrate, ControlType:button, FrameworkId:WinForm,
Source=TestStack.White
StackTrace:
at TestStack.White.UIItems.UIItem.PerformIfValid(Action action) in c:\TeamCity\buildAgent\work\89a20b30302799e\src\TestStack.White\UIItems\UIItem.cs:line 254
at TestStack.White.UIItems.UIItem.Click() in c:\TeamCity\buildAgent\work\89a20b30302799e\src\TestStack.White\UIItems\UIItem.cs:line 231
at BetfairStreamingAPI.RadForm1.radLabelBetTime_Click(Object sender, EventArgs e) in D:
有谁知道为什么第二种方法会抛出此错误,是否可以修复此错误,以便可以在不手动移动光标的情况下单击该按钮?
编辑:尝试设置切换状态的屏幕截图
答案 0 :(得分:3)
此特定问题的解决方案似乎是使用.RaiseClickEvent()
而不是.Click()
以下代码可以使用
Process tProcess = Process.GetProcesses().FirstOrDefault(x => x.MainWindowTitle.StartsWith("MainWindowName"));
if (tProcess != null)
{
TestStack.White.Application application = TestStack.White.Application.Attach(tProcess.Id);
var tWindow = application.GetWindow(SearchCriteria.ByAutomationId("SubWindowName"), InitializeOption.NoCache);
SearchCriteria searchCriteria = SearchCriteria.ByAutomationId("btnCalibrate");
var calibrateBtn = tWindow.Get<TestStack.White.UIItems.Button>(searchCriteria);
calibrateBtn.RaiseClickEvent();
}
从白色文档中不清楚何时/为什么这是首选。我在此链接https://github.com/TestStack/White/commit/7b6d4dbc0008c3375e2ebf8810c55cb1abf91b60
上找到了方法RaiseClickEvent答案 1 :(得分:2)
EDIT2
我想你可能找到了一些有趣的东西。由于您的按钮状态为Indeterminate,因此在点击它之前打开它可能是值得的:
calibrateBtn.State = ToggleState.On;
EDIT1 好吧,让我们一起解决这个问题。
该行动失败只有两个原因:
如果您执行类似
的操作Console.WriteLine(calibrateBtn.IsOffScreen.ToString());
你应该看到
true
如果是这样,请在点击之前尝试:
var pattern = calibrateBtn.AutomationElement.GetCurrentPattern(System.Windows.Automation.InvokePattern.Pattern);
(pattern as System.Windows.Automation.InvokePattern).Invoke();