【发布时间】:2018-01-03 02:32:20
【问题描述】:
编辑:在形成这个问题时,我没有注意到目标窗口是一个模态窗口,所以我尝试的方法是错误的。我应该一直使用 GetModal,而不是 GetWindow。将其留在这里作为未来旅行者的潜在参考
我正在尝试使用 TestStack.White 为我继承的大型 WPF 应用程序编写一些测试;我有一个创建子窗口的情况,我需要获取对它的引用。如果我遍历对 Application.GetWindows 的调用结果,我可以找到它,但我无法通过我能想象的 Application.GetWindow 的任何化身找到它。
这在以下示例中进行了说明(其中 name 是一个字符串)。该窗口在 foreach 循环中找到(只要我在迭代之前让线程休眠一秒钟,以便在单击其他内容后给窗口时间创建..)老实说,这个练习的全部目的只是为了得到摆脱这个 Thread.Sleep 代码气味,所以我想使用 GetWindow 及其内置的等待。
Thread.Sleep(1000);
foreach (Window window in app.GetWindows())
{
if (window.AutomationElement.Current.Name == name)
{
Assert.AreEqual(window.Title, name); // passes.. they match
Assert.AreEqual(window.AutomationElement.Current.Name, name); // passes.. they match
var aIdCheck = window.AutomationElement.Current.AutomationId; // empty string
}
}
try // this fails.. (after 30s)
{
var testGetWindow = app.GetWindow(name);
}
catch (Exception ex) { }
try // this fails too... (after 5s)
{
var testGetWindow = app.GetWindow(SearchCriteria.ByNativeProperty(AutomationElement.NameProperty, name), InitializeOption.NoCache);
}
catch (Exception ex) {}
try // you guessed it.. fail..
{
var testGetWindow = app.GetWindow(SearchCriteria.ByText(name), InitializeOption.NoCache);
}
catch (Exception ex) {}
【问题讨论】:
标签: c# wpf white-framework