考虑到我的MainWindow
在其XAML中声明了以下按钮:
<Button x:Name="MyButton" Command="{Binding SomeCommand}">Click me!</Button>
我想在测试项目中使用UIAutomation创建一个新窗口并按下此按钮,但是,这样做时会出现以下错误:
COM object that has been separated from its underlying RCW cannot be used.
对于记录,我没有使用任何COM对象。
这就是我的测试类的布局:
[TestClass]
public class UnitTest1
{
public UnitTest1()
{ }
private MainWindow _window;
[TestInitialize]
public void TestInitialize()
{
_window = new MainWindow();
_window.Show();
}
[TestCleanup]
public void TestCleanup()
{
_window.Close();
}
[TestMethod]
public void TestMyButton()
{
var myButton =
AutomationElement.RootElement.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.AutomationIdProperty,
"MyButton"));
var pattern =
myButton.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
pattern.Invoke();
}
}
这不是你想在单元测试中使用UIAutomation的方式吗?
答案 0 :(得分:4)
[TestCleanup]
public void TearDown()
{
Dispatcher.CurrentDispatcher.InvokeShutdown();
}