Add Assertions in Coded UI Test

Assertions can be added in a Coded UI Test using the Coded UI Test Builder. The Coded UI Test Builder can be launched from

1. Generate Code from Coded UI Test Dialog:


2. Context Menu in Coded UI Test Method



3. From the Test Menu


In all cases, it launches the Coded UI Test Builder.


Drag the cross-hair icon to select a UI Control.

NOTE that you can use the Win+I key (Press the Windows Key and I simultaneously) also to get the details of the focused control.  This is required for context menus and other controls which depend on mouse focus to stay on screen.


When a UI Control is selected, the Add Assertions dialog comes up with the properties of the selected control.


Assertions can be added for any of these properties, by right-clicking on the desired property.


e.g:- The dialog above adds an assertion that the DisplayText property is equal to “Start”.


After adding the assertions, click on the Generate Code button to generate a method which includes all the assertions added. It is recommended that mnemonic names be given to assertion methods.


Actions in Add Assertions Dialog


1. Add and assertion

2. Sort the Properties

3. Refresh Properties and highlight control in Application Under Test.

4. Navigate to parent, first child or peer controls in the Application Under Test.


5. Show the UI Control Map (by clicking on << icon)



In the UI Control Map, we can

6. Add control to UI Map

7. Delete a Control from the UI Map

8. Rename a control

9. Copy reference to the control – This copied the fully qualified name to the clipboard. You can then paste this in the Visual Studio editor and write code to retrieve its properties, add assertions or perform actions on the control.

Other features of UI Control Locator

  • When the cross-hair is being dragged, the Add Assertions window becomes completely transparent. I can drop the cross-hair on a control which is behind it also.
  • Even though all controls are given default names based on their implementation, it is recommended that you rename them as appropriate. This will enhance the maintainability of your test.
  • Sometimes, it may not be possible to directly focus on the control you are interested in. In this case, drop the crosshair (or use Win+I) to locate a nearby control. Use the navigation buttons on the UI Control Locator to navigate to the selected control neighbors.  I have found it to be extremely useful for locating tables or table rows on an Html page.
  • If you rename (or delete) an already saved control, its definition is replaced in the UI Control Map. But any assertions which referenced the old control are not replaced. It is the user’s responsibility to ensure that all the old references are replaced. (Compiler will help you with error messages.)
  • The top level object in the UI Control Map is named UIMap.

Skip to main content