Expect Exception in WPF Application

7 posts, 0 answers
  1. Wetzorke
    Wetzorke avatar
    25 posts
    Member since:
    Aug 2012

    Posted 19 Dec 2016 Link to this post

    Hello!

    Is there a way to verify that e.g. a Mouse-Click via FrameworkElement.User.Click() throws a particular exception within a WPF Application?

    Check within a Unit Test via e.g. MSTest's ExpectedExceptionAttribute or NUnit's Assert.Throws?

    Best wishes!

  2. Nikolay Petrov
    Admin
    Nikolay Petrov avatar
    308 posts

    Posted 22 Dec 2016 Link to this post

    Hello Christoph,

    I would suggest to use a try-catch block and to verify in the catch part the thrown exception using assert statement.

    Let me know if that makes sense in your scenario.

    Regards,
    Nikolay Petrov
    Telerik by Progress
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
  3. Wetzorke
    Wetzorke avatar
    25 posts
    Member since:
    Aug 2012

    Posted 23 Dec 2016 in reply to Nikolay Petrov Link to this post

    Hello Nikolay,

    thank you for your response.

    I couldn't get anything working from within a Unit Test:

    MSTest's ExpectedExceptionAttribute:

    [TestMethod]
    [ExpectedException(typeof(MyException))]
    public void MyTestMethod()
    {
        FrameworkElement button = ActiveApplication.MainWindow.Find.ByAutomationId("ButtonId");
        button.User.Click();
        button.User.Click();
    }

    Result Message: Test method UnitTests.MyUnitTests.MyTestMethod did not throw an exception. An exception was expected by attribute Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionAttribute defined on the test method.

     

    MSTest V2's Assert.ThrowsException<T>():

    [TestMethod]
    public void MyTestMethod()
    {
        FrameworkElement button = ActiveApplication.MainWindow.Find.ByAutomationId("ButtonId");
        button.User.Click();
     
        var exception = Assert.ThrowsException<MyException>(() => button.User.Click());
        Assert.AreEqual("Do not click twice.", exception.Message);
    }

    Result Message: Assert.ThrowsException failed. No exception thrown. MyException exception was expected.

     

    Here's the Click-Command:

    private void OnClick(object obj)
    {
        if (++count > 1)
        {
            throw new MyException("Do not click twice.");
        }
    }

     

    During both tests, the Application crashes because of the Exception (of course). But the Tests fail because they do not recognize this Exception.

    Is there a way to expect or extract an Exception within the WPF Application under test, without letting the Windows "Application has stopped working"-Dialog (like this) appear? (I want to avoid having to close any windows manually because I would like to have our TFS execute those Tests without getting cluttered).

    If possible, I also want to avoid having testing code or any kind of assertions in my production code, but keep this kind of verification in my UnitTest-Projects.

     

    I have attached the project to this post.

    Best wishes!

  4. Elena Tsvetkova
    Admin
    Elena Tsvetkova avatar
    938 posts

    Posted 28 Dec 2016 Link to this post

    Hi Christoph,

    Let me please interfere into this thread since my colleague is out of office for the holidays. Thank you for the provided details. I will review them and get back to you once I gather my observations. Thanks in advance for your understanding! 

    Regards,
    Elena Tsvetkova
    Telerik by Progress
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
  5. Elena Tsvetkova
    Admin
    Elena Tsvetkova avatar
    938 posts

    Posted 28 Dec 2016 Link to this post

    Hello Christoph,

    I managed to review your query and would like to place few more questions to clarify the behavior of your application. 

    As far as I understand the exception you need to catch is thrown when a button is clicked twice. What is the application behavior in such case - is there are popup window that informs the button is clicked twice?
    How does the application crashes? Is it related to the button click? 

    In general if the tested application crashes for some reason there is no way how to continue test execution against it. Please elaborate some further details on the expected behavior and what is the difficulty you face with the exception in question. 

    Thank you in advance for your time and cooperation! 

    Regards,
    Elena Tsvetkova
    Telerik by Progress
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
  6. Wetzorke
    Wetzorke avatar
    25 posts
    Member since:
    Aug 2012

    Posted 04 Jan 2017 in reply to Elena Tsvetkova Link to this post

    Hello Elena,

    thank you for your response.

    I wanted to expect an Exception to be thrown under certain conditions, which would actually crash the Application when started regularly (i.e. not from unit test nor from automation). I wanted to achieve something similar to an expected Exception via Assert.Throws, just like a regular .NET Unit-Test, without actually crashing the application when started from the Unit-Test via Automation.

    As far as I understand now this seems not possible.

    Best wishes!

  7. Elena Tsvetkova
    Admin
    Elena Tsvetkova avatar
    938 posts

    Posted 09 Jan 2017 Link to this post

    Hi Christoph,

    Your understanding is correct - there is no way for a Test Studio test to catch such an exception but not crash the application. 

    Please do not hesitate to contact us in case of any further questions you might have! 

    Regards,
    Elena Tsvetkova
    Telerik by Progress
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
Back to Top