开发者

Unit testing with UIAutomation

Consider that I have a MainWindow that has the following button declared in its XAML:

<Button x:Name="MyButton" Command="{Binding SomeCommand}">Click me!</Button>

I want to use UIAutomation inside a Test Project to create a new Window and press this button, however, when doing so I get the following error:

COM object that has been separated from its underlying RCW cannot be used.

For the record, I am not using any COM-objects.

This is how my Test Class is layed out:

[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();
    }
}

Is this not how you are suppose to 开发者_如何学Pythonuse UIAutomation inside a unit test?


    [TestCleanup]
    public void TearDown()
    {
        Dispatcher.CurrentDispatcher.InvokeShutdown();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜