Introduction
Coded UI is a tool that Software
Test Automation Engineers can use to create fully automated tests to validate
the functionality and behavior of the User Interface of Windows or Web
Applications Under Test (AUT). In this tutorial, you will learn how to automate
test cases in Coded UI without using record and play back
This is the first of several parts of the tutorial. In part 1 I will
demonstrate how you can use coded UI to automate the Windows Calculator. We are going to use Coded UI to find the
Calculator controls and interact with Calculator user interface controls. We
will include namespaces such as mouse support in our test script.
Prerequisites:
1. Microsoft
Windows PC
2. Visual
Studio Ultimate, Visual Studio Premium
3. Basic understanding
of C#
During this tutorial we will do the following:
·
Create a Coded UI test project
·
Use the Coded UI Test Builder to drive the user interface
·
Add assertion statements to the test script
·
Execute the test
Before we begin, you need to install Visual Studio. For this
tutorial I used Visual studio 2012 Ultimate. You may install VS 2012 Ultimate
or Premium. I will assume that you know how to install Visual Studio or can get
an installation instruction online. Now
that you have installed Visual Studio let’s get started.
Step 1. Create a Coded
UI Test Project
·
Launch Visual Studio 2012 (or whatever version you
have installed)
·
Click File à New à Project
·
From the New Project dialog select Test under Templates à Visual C#
·
Select Coded UI
Test Project in the middle pane (see the screenshot below)
·
Enter the text CalculatorTest
in the Name field
·
Select a location on your local PC to save your
project
·
Click the OK
button
Visual Studio will generate the Coded UI template needed for
the Coded UI test. Click the Cancel button on the “Generate Code for Coded UI Test” dialog box, to close it without
generating any code.
Step 2. Create the UI Map and script to drive the test
·
Launch the Windows Calculator
·
Ensure that the Calculator is set to the basic view
·
In Visual Studio, right-click CalculatorTest (the project name) and select Add à New Folder
·
Name the new folder BasicCalculatorUIMap
·
Right-click the BasicCalculatorUIMap folder and select
Add à Class
·
In the “Add New
Item –CalculatorTest” dialog box
o Select Visual C# Items à Test from the Installed (left) pane
o Select Coded UI Test Map from the middle pane
o Enter “CalculatorUIMap.uitest”
in the Name field (without the
quotes)
o Click the Add button (See the screenshot below)
The Add New Item – CalculatorTest
dialog box is closed and the Coded UI
Test Builder is displayed (at the bottom right corner as shown in the
screenshot below)
·
Click the Cross
hair and drag it over the Calculator button labelled “0”
until it is highlighted
·
Repeat the above step for every control on the
calculator application
·
Click the Code
generator icon to save the controls in the CalculatorUIMap.uitest class
·
Close the Coded
UI Test Builder when you are finished (click the X button)
Step 3. Write supporting functions to be used in our test
cases
·
Add the following to the using section
using System.Diagnostics;
using System.Runtime.InteropServices;
using
CalculatorTest.BasicCalculatorUIMap.CalculatorUIMapClasses;
·
Write the below code for launching the calculator
private void launchTheWindowsCalculator()
{
Process process = new Process();
process.StartInfo.FileName = "Calc.exe";
process.Start();
System.Threading.Thread.Sleep(1000);
}
·
Write the following function for closing the
calculator
private void CloseTheCalculator()
{
//get access to the UIMap class we created in step 2
CalculatorUIMap calcUIMap = new CalculatorUIMap();
//now
get the position of the calculator close button and click on it with the mouse
var calcCloseButton =
calcUIMap.UICalculatorWindow.UICalculatorTitleBar.UICloseButton.BoundingRectangle.Location;
var xCoordinate =
calcCloseButton.X + 3;
var yCoordinate =
calcCloseButton.Y + 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
}
Step 4. Start coding the test cases.
For this tutorial, I will write 4 test cases to demonstrate
how to use Coded UI to automate your test cases. I will automate the following simple test
cases:
1.
Add the
positive numbers 5 and 8 and verify the result is 13
2.
Subtract 3
from 9 and verify the result is 6
3.
Divide 8 by
2 and verify the result is 4
4.
Multiply 9
and 3 and verify the result is 27
Below is a
listing of the Coded UI code to implement the above test cases (you will find a
zip file of the code here):
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using System.Diagnostics;
using System.Runtime.InteropServices;
using
CalculatorTest.BasicCalculatorUIMap.CalculatorUIMapClasses;
namespace CalculatorTest
{
/// <summary>
/// Summary description for CodedUITest1
/// </summary>
[CodedUITest]
public class CUITCalcFunctionalTests
{
public CUITCalcFunctionalTests()
{
}
[TestMethod]
public void testCase1_AddPositive5and8()
{
int expectedResult = 13;
int actualResult = AddPositive5and8();
Assert.AreEqual(expectedResult, actualResult);
}
[TestMethod]
public void testCase2_Subtract3From9()
{
int expectedResult = 6;
int actualResult = subtract3From9();
Assert.AreEqual(expectedResult, actualResult);
}
[TestMethod]
public void testCase3_Divide8By2()
{
int expectedResult = 6;
int actualResult = divide8By2();
Assert.AreEqual(expectedResult, actualResult);
}
[TestMethod]
public void testCase4_Multiply9And3()
{
int expectedResult = 27;
int actualResult = multiply9And3();
Assert.AreEqual(expectedResult, actualResult);
}
//*********************************
Supporting functions ***************************************************
private int AddPositive5and8()
{
CalculatorUIMap calcUIMap = new CalculatorUIMap(); //get access to the UIMap class we created in step 2
int result = 0;
//launch
the application under test
launchTheWindowsCalculator();
try
{
var num5Button =
calcUIMap.UICalculatorWindow.UIItemWindow9.UIItem5Button.BoundingRectangle.Location;
var num8Button =
calcUIMap.UICalculatorWindow.UIItemWindow14.UIItem8Button.BoundingRectangle.Location;
var addButton =
calcUIMap.UICalculatorWindow.UIItemWindow2.UIAddButton.BoundingRectangle.Location;
var eqSignButton = calcUIMap.UICalculatorWindow.UIItemWindow3.UIEqualsButton.BoundingRectangle.Location;
var calcTextDisplay =
calcUIMap.UICalculatorWindow.UIItem0Window.UIItem0Text;
var clearButton =
calcUIMap.UICalculatorWindow.UIItemWindow20.UIClearButton.BoundingRectangle.Location;
var xCoordinate = 0;
var yCoordinate = 0;
//start
by clearing the claculator so we have a clean slate
xCoordinate = clearButton.X +
3;
yCoordinate = clearButton.Y +
3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//click
the button labelled 5 on the claculator
xCoordinate = num5Button.X + 3;
yCoordinate = num5Button.Y + 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//click
the calculator add button
xCoordinate = addButton.X + 3;
yCoordinate = addButton.Y + 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//
click the calculator button labelled 8
xCoordinate = num8Button.X + 3;
yCoordinate = num8Button.Y + 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//click
the equals sign on the calculator
xCoordinate = eqSignButton.X +
3;
yCoordinate = eqSignButton.Y +
3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//get
the result of the calculation displayed on the calculator screen
result = System.Convert.ToInt32(calcTextDisplay.DisplayText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
calcUIMap = null;
//close
the application
CloseTheCalculator();
}
return result;
}
private int subtract3From9()
{
CalculatorUIMap calcUIMap = new CalculatorUIMap(); //get access to the UIMap class we created in step 2
int result = 0;
//launch
the application under test
launchTheWindowsCalculator();
try
{
var num3Button =
calcUIMap.UICalculatorWindow.UIItemWindow6.UIItem3Button.BoundingRectangle.Location;
var num9Button =
calcUIMap.UICalculatorWindow.UIItemWindow15.UIItem9Button.BoundingRectangle.Location;
var subtractButton = calcUIMap.UICalculatorWindow.UIItemWindow7.UISubtractButton.BoundingRectangle.Location;
var eqSignButton =
calcUIMap.UICalculatorWindow.UIItemWindow3.UIEqualsButton.BoundingRectangle.Location;
var calcTextDisplay = calcUIMap.UICalculatorWindow.UIItem0Window.UIItem0Text;
var clearButton =
calcUIMap.UICalculatorWindow.UIItemWindow20.UIClearButton.BoundingRectangle.Location;
var xCoordinate = 0;
var yCoordinate = 0;
//start
by clearing the claculator so we have a clean slate
xCoordinate = clearButton.X +
3;
yCoordinate = clearButton.Y +
3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//press
9 on the calculator
xCoordinate = num9Button.X + 3;
yCoordinate = num9Button.Y + 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//press
the minus sign
xCoordinate = subtractButton.X
+ 3;
yCoordinate = subtractButton.Y
+ 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//press
the number 3
xCoordinate = num3Button.X + 3;
yCoordinate = num3Button.Y + 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//press
the equals sign
xCoordinate = eqSignButton.X +
3;
yCoordinate = eqSignButton.Y +
3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//get
the result of the calculation displayed on the calculator screen
result = System.Convert.ToInt32(calcTextDisplay.DisplayText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
calcUIMap = null;
//close
the application
CloseTheCalculator();
}
return result;
}
private int divide8By2()
{
CalculatorUIMap calcUIMap = new CalculatorUIMap(); //get access to the UIMap class we created in step 2
int result = 0;
//launch
the application under test
launchTheWindowsCalculator();
try
{
var
num8Button =
calcUIMap.UICalculatorWindow.UIItemWindow14.UIItem8Button.BoundingRectangle.Location;
var num2Button =
calcUIMap.UICalculatorWindow.UIItemWindow5.UIItem2Button.BoundingRectangle.Location;
var divideButton =
calcUIMap.UICalculatorWindow.UIItemWindow31.UIDivideButton.BoundingRectangle.Location;
var eqSignButton =
calcUIMap.UICalculatorWindow.UIItemWindow3.UIEqualsButton.BoundingRectangle.Location;
var calcTextDisplay =
calcUIMap.UICalculatorWindow.UIItem0Window.UIItem0Text;
var clearButton =
calcUIMap.UICalculatorWindow.UIItemWindow20.UIClearButton.BoundingRectangle.Location;
var xCoordinate = 0;
var yCoordinate = 0;
//start
by clearing the claculator so we have a clean slate
xCoordinate = clearButton.X +
3;
yCoordinate = clearButton.Y +
3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//press
the button 8
xCoordinate = num8Button.X + 3;
yCoordinate = num8Button.Y + 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//press
the button for divide sign
xCoordinate = divideButton.X +
3;
yCoordinate = divideButton.Y +
3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//press
the button 2
xCoordinate = num2Button.X + 3;
yCoordinate = num2Button.Y + 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//press
the equals sign
xCoordinate = eqSignButton.X +
3;
yCoordinate = eqSignButton.Y +
3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//get
the result of the calculation displayed on the calculator screen
result = System.Convert.ToInt32(calcTextDisplay.DisplayText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
calcUIMap = null;
//close
the application
CloseTheCalculator();
}
return result;
}
private int multiply9And3()
{
CalculatorUIMap calcUIMap = new CalculatorUIMap(); //get access to the UIMap class we created in step 2
int result = 0;
//launch
the application under test
launchTheWindowsCalculator();
try
{
var num9Button =
calcUIMap.UICalculatorWindow.UIItemWindow15.UIItem9Button.BoundingRectangle.Location;
var num3Button =
calcUIMap.UICalculatorWindow.UIItemWindow6.UIItem3Button.BoundingRectangle.Location;
var multiplyButton =
calcUIMap.UICalculatorWindow.UIItemWindow11.UIMultiplyButton.BoundingRectangle.Location;
var eqSignButton =
calcUIMap.UICalculatorWindow.UIItemWindow3.UIEqualsButton.BoundingRectangle.Location;
var calcTextDisplay =
calcUIMap.UICalculatorWindow.UIItem0Window.UIItem0Text;
var clearButton =
calcUIMap.UICalculatorWindow.UIItemWindow20.UIClearButton.BoundingRectangle.Location;
var xCoordinate = 0;
var yCoordinate = 0;
//start
by clearing the claculator so we have a clean slate
xCoordinate = clearButton.X +
3;
yCoordinate = clearButton.Y +
3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//press
9
xCoordinate = num9Button.X + 3;
yCoordinate = num9Button.Y + 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//press
the multiplication sign
xCoordinate = multiplyButton.X
+ 3;
yCoordinate = multiplyButton.Y
+ 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//press
3
xCoordinate = num3Button.X + 3;
yCoordinate = num3Button.Y + 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//press
the equals sign
xCoordinate = eqSignButton.X +
3;
yCoordinate = eqSignButton.Y +
3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
//get
the result of the calculation displayed on the calculator screen
result = System.Convert.ToInt32(calcTextDisplay.DisplayText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
calcUIMap = null;
//close
the application
CloseTheCalculator();
}
return result;
}
private void launchTheWindowsCalculator()
{
Process process = new Process();
try
{
process.StartInfo.FileName = "Calc.exe";
process.Start();
System.Threading.Thread.Sleep(1000);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
process = null;
}
}
private void CloseTheCalculator()
{
CalculatorUIMap calcUIMap = new CalculatorUIMap(); //get access to the UIMap class we created in step 2
try
{
//now
get the position of the calculator close button and click on it with the mouse
var calcCloseButton =
calcUIMap.UICalculatorWindow.UICalculatorTitleBar.UICloseButton.BoundingRectangle.Location;
var xCoordinate = calcCloseButton.X + 3;
var yCoordinate = calcCloseButton.Y + 3;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCoordinate, (int)yCoordinate);
Mouse.Click();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
calcUIMap = null;
}
}
Coded UI recognizes the public methods tagged with [TestMethod] as the tests; so for each
test case I created a public method with the appropriate tag. These methods
call their supporting functions that do the actual work of implementing the
test steps of the test case. Each supporting function returns a value to their
calling module, which then use returned value in the assertion statement to
compare to the expected value.
Step 5. Run the tests
·
To execute the Coded UI tests in Visual Studio you may
do one of the following (see 2 screenshots):
o Select TESTàRun àAll Tests
o Press
CTRL+R,A
o Select Run All from the Test Explorer
o Alternatively
You could highlight one or more tests in the Test Explorer, right-click the highlighted test, and click Run Selected Tests from the context
menu
No comments:
Post a Comment