H
H
h1_0ne2019-10-03 20:20:46
C++ / C#
h1_0ne, 2019-10-03 20:20:46

How to get information about whether an exception is currently thrown?

I do UI tests using WinAppDriver. When an exception occurs on the form, the tests are considered passed. Is there some way to check if an exception is thrown on the form or not? Or maybe there are other frameworks that would throw an exception in a similar situation?

[TestMethod]
        public void TestMethod1()
        {
            session.FindElementByAccessibilityId("button1").Click();
        }

private void button1_Click(object sender, EventArgs e)
        {
            throw new Exception();
        }

5d962dbdbce0d070452006.png5d962deb7a803807758656.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Yudakov, 2019-10-03
@AlexanderYudakov

[TestMethod]
public void TestMethod1()
{
    var button = session.FindElementByAccessibilityId("button1");
    try
    {
        button.Click();
    } catch(Exception) {
        // Тест пройден.
        return;
    }

    throw new Exception("Тест не пройден.");
}

L
Lelushak, 2019-10-05
@Lelushak

No way. You have an unhandled exception thrown on another (UI) thread.
The only way is to catch the exception in the click handler and then store information about it in some variable, or even the exception itself. And then access this variable from the test.
But in general, you can't do that. Unit tests should not interact with the UI, they are for unit testing . Your task is solved through integration testing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question