Use of Assertions in Coded UI

Assertions play a very major role in Automation testing. As whenever a functionality is completed, its necessary for a tester to verify that whether the completed functionality is working properly or not and to check that we need a proof of it as For example: – If a form is filled and submitted a successful message appears as a confirmation that the form is submitted successfully.

Assertions are just a value or a statement placed in a program just to verify that the running coded or function performs with true value. If in case a false value is found at run-time, then an assertion is set to result as fail, which causes the automated script to abort.

Assertion in Coded UI is presented with various methods with the assert classes as shown below:-

  1. AreEqual
  2. AreEqual<>
  3. AreNotEqual
  4. AreNotEqual<>
  5. AreNotSame
  6. AreSame
  7. Equals
  8. Fail
  9. Inconclusive
  10. IsFalse
  11. IsInstanceOfType
  12. IsNotInstanceOfType
  13. IsNotNull
  14. IsNull
  15. IsTrue
  16. ReferenceEquals
  17. ReplaceNullChars

Assertions can be added in two ways:-

  1. Assertions adding to Coded UI Test script generation
  2. Adding an assertion using “Hand Coding” Coded UI script

First, we will see how to add an assertion in Coded UI test script generation. We will take an example for explaining this scenario as, after launching any website will assert login page username and its entered value.

  1. Start with launching Coded UI test builder.
  2. Select option as “Generate Code for Coded UI Test” >> Use Coded UI Test Builder
Test Builder

3. After selection, the Test builder will be launched, open the website you want to generate the script for.

4. Now, click on the circle with the dot inside (third button) and drag and drop on the control you want to validate.

Control Option

5.  As the control is fetched from the test builder, a window with all the details of that particular selected area will get displayed.User can see all properties of the selected control and can add an assertion to any of the property shown.

Add Assertion Option

6. On this window, an option to “Add Assertion” is present in the top left corner.Click on the add assertion button, a form to add assertion will get appeared.

Assertion

7.  Select the Comparator and Comparison value you want, also add a message and click on “OK” button.

8.  As the assertion is added now Click on generate code button, enter proper method name for the Validation code.For example :- ValidateUserName.

Test Method

9. The code is ready to validate Username, in this way you can create code for any control, message or value you want.

Now using hand coding Coded UI  you can write your own code for assertion using predefined methods.

For understanding assertion methods custom coding is required. As mentioned in my previous blog of “IIntroduction to Coded UI Hand-coding” we have launched the website and used custom coding for entering text in the textbox and passing the values in the textbox.

Continuing the same example we will now see how we can assert the entered text and validate the enter Username is correct or not.

Here we will discuss the most commonly used assertions in coded ui like :-

1. Below is the Validate text Input function will tell us how to check the entered text value.

public static void VaildateTextBoxInput(string ExpectedText, string TextBoxID)

{

HtmlEdit txtBox = new HtmlEdit(br);

txtBox.FilterProperties[HtmlEdit.PropertyNames.ClassName] = “HtmlTextBox”;

txtBox.SearchProperties[HtmlEdit.PropertyNames.Id] = TextBoxID;

UITestControlCollection utcc = txtBox.FindMatchingControls();

HtmlEdit txt = (HtmlEdit)utcc[0];

string ActualTxt = (string)txt.GetProperty(HtmlEdit.PropertyNames.Text);

 Assert.AreEqual(ExpectedText, ActualTxt);

OR

if (ActualTxt.ToLower().Trim() != ExpectedText.ToLower().Trim())

{

Assert.Fail(“Expected Text is not found in Textbox: Actual Value is ” + ActualTxt + ” and Expected is ” + ExpectedText + “);

}

}

catch (PlaybackFailureException e)

{

 Assert.Fail(e.Message);

}

}

You can use Assert.AreEqual() or if () statement both to check the text is same or not.

Assert.Fail() will tell us about if in any case the method gets failed, it will tell us the reason for failed result.

Message

Result Message: Assert.Fail failed. Expected Control : ” UserName Textbox” not Available on page.

 

2. Using the above function, now we can use the method name and pass the values from TestMethod and can run the script to validate the entered text.

[TestMethod]

public void Login()

{

CommonFunctions.GotoHomePage (“https://www.facebook.com/login/”);

CommonFunctions.Login (“test.login@gmail.com”,”LoginPassword”);

CommonFunction.VaildateTextBoxInput(test.login@gmail.com“,”MainContentPlaceHolder_loginBox_Username”)

CommonFunction.Submit(“LoginButton”);

}

By using these two method assertions in coded ui can be performed, but the preferred method is to use hand coding as you can make the script dynamic, also the written code is reusable in other projects.

Leave a comment