Skip to main content

Coded UI

Now, we will try to understand the code step by step.

The following statement launches the browser and navigates to https://accounts.google.com
There are lots of testing automation tools available to test user Interface and functionality of an application.
Coded UI Test (CUIT) is an automation tool which was made available as part of the Visual Studio 2010 update. It uses Visual Studio IDE to write scripts and recording can be done using Visual Studio through Coded UI test builder.

What is Coded UI Test (CUIT)?

Automated test cases that drive application from its user interface is known as Coded UI test automation. These tests incorporate functional testing of the UI controls. They let you check that the entire application, including its UI, is working accurately. Coded UI is usually utilised to automate manual tests.

Why Coded UI?

  • supports various technologies like WPF, Windows Based Desktop Application, Web Services(SOAP etc), Window Phone Applications, Web Applications (HTML, SilverLight, HTML5)· functional testing
  • code generation in C#/VB
  • recording and playback feature
  • tests can be run on Remote Machines with the help of Tests Agents
  • Strong support for Synchronisation. The Playback Engine allows settings such as "WaitForControlReady"

System Requirement

Visual Studio Ultimate, Premium and Enterprise can provide full support for Coded UI Test.

How to Create Coded UI Tests?

1.       Record and Playback (Test builder)
2.     Hand coding / User coding

Hand Coding/ User Coding with Coded UI C#

To understand hand coding/user coding using Coded UI, we will be creating a simple project to launch Google and search for gmail and click on search and open that link using Visual Studio Enterprise 2015 and C#.
  •   Create a new project in Visual Studio and select “Coded UI Test Project” from Test Templates.
          Give it a meaningful name and click ok.
  •        Click Cancel on “Generate Code for Coded UI Test” Dialog as we are not going to record actions rather going to write our own code.

  •        A Solution with name –Sample1 containing class CodedUITest1.cs is created. You can rename the class as per requirement.
  •        In the below figure we can see some Microsoft Namespaces which are described below.
a  
  •     Microsoft.VisualStudio.TestTools.UITesting –It contains the class UITestControl which helps in locating any control on User Interface whether the control is Windows Control, WPF or Html Control
  •     Microsoft.VisualStudio.TestTools.UnitTesting – This namespace provides attributes which are used to establish a calling order –AssesblyInitializeAttribute,AssemblyCleanUpAttribute, defines members used for Data Driven Testing –DataSourceAttribute, DataAccessMethod,attributes used to identify test class ,test method-TestClassAttribute,TestMethodAttribute, contain Assert Classes and related Exceptions, TestContext Class,Test Configuration Classes,Attributes used to generate reportsand classes used with private accessors.
  •     Microsoft.VisualStudio.UITest.Extension –Defines exception classes when control is blocked , abstract base class for recorder and playback and many more classes , interfaces and enums which can be found here.
  •     Microsoft.VisualStudio.TestTools.UITesting.Keyboard -It Contains static methods for performing keyboard actions like SendKeys to enter text in an Input Control
  •        Under CodedUITestMethod1, we will be writing our script for the given test steps.
  1.      Navigate to "https://accounts.google.com"
  2.      Enter Google user name. Click Next
  3.      Enter Password. Click Next Again.
  4.      Verify Google Account Page is opened.
  •    The Script for above test steps is given below:

       Now, we will try to understand the code step by step.
       The following statement launches the browser and navigates to https://accounts.google.com
    BrowserWindow browserWindow = BrowserWindow.Launch(new   System.Uri("https://accounts.google.com"));.

       Next, we will try to enter google user name in input box.
       To access HtmlControls in our code , we need to add a namespace -   Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;
       The following code finds an HtmlEdit with ID –identifierId

    HtmlEdit emailEdit = new HtmlEdit(browserWindow);
    emailEdit.SearchProperties.Add(HtmlEdit.PropertyNames.Id, "identifierId");     
  •       The constructor for UITestControl class accepts an argument which specifies the searchLimitContainer.  The search for the UITestControl will be limited to all the child controls of searchLimitContainer. In the code above, we are limiting search to all children of browserWindow.
  •       SearchProperties will be used to find the control.  In the code above, we search based on Name and Id. It is possible to discover the various properties of a control using the HtmlProperties class. HtmlEdit will contain all properties applicable to an edit box.
  •      Using Keyboard Class’s SendKeys method , text can be entered in the provided control.
  •      Similarly, Mouse actions are performed using the Mouse class. It provides static methods for all possible operations with a Mouse.
  •      Assert Class is used to verify a particular statement and If it is not validated , the test case fails at that point.
  •     Following code is used to verify if Google Account Page opens by verifying the existence of Google Account Hyperlink.
              HtmlHyperlink accountHyperlink = new HtmlHyperlink(browserWindow);
              accountHyperlink.SearchProperties.Add(HtmlHyperlink.PropertyNames.Id, "sdgBod");
       Assert.IsTrue(accountHyperlink.Exists,"Google Account Page does not open.");

  •      The properties of various controls can be searched by “Coded UI Test Builder”. You can open CUIT builder by right clicking inside the script and hovering the Mouse over “Generating Code for Coded UI Test” and then click on “Use Coded UI Test Buider” or you can either use shortcut “Ctrl +\, Ctrl+C”

  •     You will able to see a rectangular dialog on lower right corner of your screen.

  •        To check the properties of a control , hover the mouse over the control and use (Ctrl + Shift +I).

  •     Right Click inside the Test Method and choose Run Tests.

  •     The code will be compiled and the test is run.

Comments