Site Loader
Auckland, New Zealand
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
  • Finding of Element
  • Performing operation in Element like enter text, click etc
  • Setting a property for the element
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

Post Author: Karthik kk

4 Replies to “Exception handing in Coded UI Testing”

  1. how to resolve the below error:
    The web page could not be accessed ,if the page is refreshing ,please wait until the page refresh and perform action on it

  2. Hi karthik,

    I need to validate in a wintreeitem whether the rroot node is bold or unbold.
    Is this possible in coded ui testing ?

  3. I don’t think the exception handing on Assert is necessary. The assert throws an exception of its own when not true and thats what codedui uses to abort the test.

Leave a Reply to Karthik kk Cancel reply

Your email address will not be published. Required fields are marked *