Site Loader
Auckland, New Zealand
In this post we will start our discussion about one of the most commonly used feature in coded UI testing as well as any testing for that matters, which is Assertions. According to Wikipedia, the definition of assertion is “An assertion is a predicate (a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. If an assertion evaluates to false at run-time, an assertion failure results, which typically causes execution to abort.” We have already discussed about working with assertions in coded UI test builder in our previous post, we also discussed the various methods available with Assert class as shown below In this post, we are going to discuss Some of the most commonly used assertion method like
  • AreEqual
  • AreNotEqual
  • Contains
  • AreSame etc
To understand some of the Assertion methods, we are going to write a custom method, which will get the text from text box, which we will then assert to check if the value is matching the expected value. You can watch the complete discuss in the video below Here is the complete code snippet for our GetText() method
public string GetText(string propertyvalue)
{
    HtmlEdit edit = new HtmlEdit(ParentWindow);
    edit.SearchProperties[HtmlEdit.PropertyNames.Name] = propertyvalue;
    return edit.GetProperty("Text").ToString();
}

Thanks for watching the video and reading the article. Please let me know if there is anything need to be improved in this article. Thanks, Karthik KK

Post Author: Karthik kk

22 Replies to “Assertions in Coded UI Testing”

  1. Hi Karthik,

    I have been reading your posts regularly. Currently i am working on Coded UI and your posts helped me a lot to come to the conclusion where i was facing issues.

    I really appreciate knowledge sharing approach. Keep on posting.

  2. Hi Karthik, many thanks for all the CodedUI Video Tutorials!
    I’m seeking your kind assistance for inserting/adding “assertion” to windows/desktop application. Would really appreciate your reply!

    Regards,
    Z Reza

    1. I dont think they are different for windows application, except the identification property of the control will change.

      Do you expect something other than that ?

      Please let me know.

      Thanks,
      Karthik KK

  3. In my application I have launch web browser then I get a windows pop where I have to enter user id and password and then it directs me to a web application. So technically I have to handle a MSAA pop up to get to my HTMLControls.

    The challenge I am facing is when I enter user id and password manually, in one instance I get my application home page.

    Through coded ui when I handled the pop up by entering user id and password and hit OK button it still waits on the Pop UP, why is not redirecting to my web page.

    And also note that my TestMethod is passed, it is not in failed state :(.

    Record and Play back is working fine but something is wrong when I am hand coding the same.

    Are there any suggestions to handle this ?

    1. Two things to note.

      1. Did the code really performed the click operation to login (Check it, else try to execute javascript code to perform click operation)
      2. Did you performed any assertion to validate the valid page check.

      Thanks,
      Karthik KK

  4. == Check Box selection failure due to lack of property & values. ==

    Hello Karthik.

    I’m trying to create an Automation CodedUI Testing script (using Visual Studio Premium 2013) where I’m trying to click/select a check box(s) for desktop application. Cross Hair fails to identify/fetch the check box’s property & values (the highlight, once in a blue moon catches the check box and stays on top of it for a fraction of a second!!!). So upon discussing the issue with Devs’ they delivered the procedure codes names for few nodes in procedure codes.

    My question is – how can I click/select those check box(s)?

    Note: I have never been exposed to MS Visual Studio until now.

    Would really appreciate your reply Karthik!
    Thanks & Regards,
    Z Reza

  5. Hi Karthik,
    Iam new to CodedUI and your post help me a lot.Thanks for your post.
    Now iam stuck on the how to dynamically get the actual value for assertion based on input while pass more columns through csv file.

  6. Hi Karthik,

    Do you have any idea how can I export test logs for my assertions?
    Currently I am using Console.WriteLine (“some message”) and is displayed in the VS Output. But to further use this, I would like to export the “some message” so that when it fails, there is a more user friendly error message shown; Currently we are exporting action logs only and when management tries to run the codedUI script, the error message is too much for them. It would be easier if we can export a much more readable test log.

    Any inputs is greatly appreciated!

    Thank You as usual! 🙂

  7. Hi Karthik,

    I’m using Coded-UI on VS 2015 Enterprise edition. The issue that I have is; every time an assertion fails the subsequent actions / assertions will be ignored.

    To omit this I used multiple [TestMethod]s and used only 1 assertion in 1 [TestMethod]. Still it will not display a 2nd assertion failure (in the HTML logger) even if the 2nd assertion is inside another [TestMethod] .

    So basically if an assertion fails it will report the error in the HTML logger and move to the 2nd [TestMethod]. If an assertion fails in the 2nd [TestMethod], it will not display that in the HTML logger but display up to the last action before the 2nd Assertion failure. Therefore someone might think the actions in the 2nd [TestMethod] are successful.

      1. Hi Karthik,

        Thanks a lot for the reply.
        Suppose you have 20 assertions to be tested in 1 test case. Ideally what we will expect is, to check for all 20 and report what are missing. Say 5 out of 20 are missing so 5 assertions fail. Since Coded-UI ignores all actions and / or assertions upon the 1st assertion failure I thought of placing 20 assertions inside 20 [Test Methods] in 1 Coded-UI project. So that I can have 1 project per test case, which has 20 [Test Methods] and each will have 1 assertion . But if this is also not working the way I want what approach should I take? Please advise me. Surely I cannot create a Project each per assertion?

        1. You are right again, you cannot create test method for each assertion though 🙂

          Well, it turns out that, you can create a method which sets the flag (true/false) if there happens any issue and stores the value in a collection.

          If the flag is true (meaning something went wrong), then check the collection and see which one is failing.

          Assert just the flag and report what else is there in collection as missing (as in your case)

          Hope it make sense !!

          Thanks,
          Karthik KK

  8. Hi Karthik,

    Thanks for the reply. It makes sense, But don’t you think Coded_UI needs to facilitate its users to execute all the assertions regardless of failures. (I’m not sure why a feature request is not made)
    e.g.
    # Tools like QTP has an option “On Error Resume Next” we used this execute the entire test by logging assertion failures in the middle.
    # Then SoapUI has this feature too.

    If I ask a feature request from Microsoft will you be able to use your fan base to get me enough votes?

    For your perusal I have shared this on
    http://stackoverflow.com/questions/35361739/coded-ui-test-explorer-and-html-log-treat-multiple-test-method-failures-differe

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/3d3a5ebf-d3ee-4627-a6b2-ae888e25ff7a/codedui-reports-only-1-assertion-failure-when-you-have-multiple-test-methods-vs-enterprise-2015?forum=vstest

  9. Hi Karthik,

    I need to get the text from Password Textbox control. I have tried Text property but password Textbox control doesn’t have Text property.

    Can you please help me how to get the Text from password Textbox control?

Leave a Reply to Karthik kk Cancel reply

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