我正在尝试单击外部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:有没有人知道第二种方法抛出这个错误的原因,以及是否有可能修复这个错误,这样就可以在不手动移动光标的情况下单击按钮?
编辑:尝试设置togglestate的屏幕截图

发布于 2018-02-28 02:03:19
这个特殊问题解决方案似乎是使用.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方法
发布于 2018-02-27 05:57:58
EDIT2
我想你可能发现了一些有趣的东西。由于您的按钮状态是不确定的,因此在单击它之前将其打开可能是值得的:
calibrateBtn.State = ToggleState.On;
EDIT1好的,让我们一起解决这个问题。
该操作失败的原因只有两个:
该按钮未启用,我猜情况不会是这样: the
如果你做像这样的事情
Console.WriteLine(calibrateBtn.IsOffScreen.ToString());您应该会看到
true如果是这样,请在单击之前尝试以下操作:
var pattern = calibrateBtn.AutomationElement.GetCurrentPattern(System.Windows.Automation.InvokePattern.Pattern);
(pattern as System.Windows.Automation.InvokePattern).Invoke();https://stackoverflow.com/questions/48995704
复制相似问题