ExecuteAutomation

Exception handing in Coded UI Testing

In this post, we will start working with Error/Exception handing in Coded UI Testing. In the last post, we discussed about Assertions in coded UI testing. In Assertions we saw how coded UI testing stops its execution without moving forward to the next operation if the Assertion fails. Even though the behavior is expected, in some situation we would like our test to move forward if there happens any failure due to Validation Thus in this topic, we will discuss how to handle Error/Exception in code, if there happens any failure only during Validation process but not during
Note Please don’t try to add exception handling which perform certain operations in the UI, since if that operation exception itself is handled, then that may result in unexpected behavior of test.

Error/Exception handling

We are going to handle Error/Exception in Coded UI testing using try .. catch block of C#. Here is the complete video of the above explanation Here is the code snippet of the same code which we discussed in previous post on Assertion in coded UI testing
public string GetText(string propertyvalue)
{
    try
    {
        HtmlEdit edit = new HtmlEdit(ParentWindow);
        edit.SearchProperties[HtmlEdit.PropertyNames.Name] = propertyvalue;
        return edit.GetProperty("Text").ToString();
    }
    catch (UITestControlNotFoundException e)
    {
        return "";
    }
}
Code Snippet to Wrapper the Assert.AreEqual method
public void Equals(string expected, string actual, string message)
{
    try
    {
        Assert.AreEqual(expected, actual, message);
    }
    catch (AssertFailedException e)
    {
        Console.WriteLine(e.Message);
    }
}
Thanks for watching the video and reading the post !!! Please leave your comments and let me know if there is anything need to be improved in this post. Thanks, Karthik KK