Creating 1st CodedUI Test :
Today we will see how to create and execute a simple CodedUI test. We will follow the steps as illustrated below.
Step 1 -a) Select File>New>Project then the below New Project pop up window will open.
-b) Select “Test” project template and click on “OK” button.(Specify the project name and project location as per your choice.)
Step 2- The Visual studio will be now loaded with the “TestProject1”. Now click on solution explorer.
View the assemblies references as shown below add required reference.
Step 3- Add reference of the following inbuilt assemblies. To add reference right click on the reference and then click on the “.NET” tab present in Add reference pop up window, then add the below reference.
Step 4- Now Right click on TestProject1 and select Add > CodedUI test as shown in below fig.
Step 5- The CodedUITest1 file will be generated and pop up – To Record actions or mapping controls will appear as shown below:
**Here we have selected first option assuming that we don’t know the ID’s of the controls. Hence we will map the control Id as per the test script requirement.
Step 6-The coded UI Test Builder will appear as soon as we click “OK” button.
Step 7- Now we will see how to map a control. Here we took example of the Calculator and we will check for the Addition operation.
-a) To map control click on “Crosshair” control present in CodedUI builder and drag it to the Control on screen of which ID you want to Map.
-b) The other way to map control is just put the mouse pointer on the control and press “Window logo key + I key” from keyboard.
-c) Next step is to add the control to UI Map file. Hence click the button add control as highlighted in red square box in below fig.
-d) Now click on Generate button. This will generate the control definition code in UIMap.designer.cs file. Also this file contains the code for recorded actions as well.
In our example we have mapped the textbox where the result/or the selected digits are shown.
Similarly we will map the calculator buttons: “1”,”2”,”+” and “=”
Step 8- After mapping done let’s move to create method for automation of the script.
Add the below piece of code in the CodedUITest1.cs file
NOTE: Here code may throw error if you mapped the controls in different sequence. So please check the mapping if the code throws error like ”control not found”. And use the mapping available as in UIMap.designer.cs file.
[TestMethod]
public void CheckAdditionFunctionality()
{
try
{
//add using Microsoft.VisualStudio.TestTools.UITesting.WinControls; namespace
LaunchCalculator();
//Here playback.wait() method added just for understanding purpose for first time user..
//to understand the actions automated in slow motion. You can remove playback.wait() method
WinButton btn1 = this.UIMap.UICalculatorWindow.UIItemWindow.UIItem1Button;
WinButton btn2 = this.UIMap.UICalculatorWindow.UIItemWindow1.UIItem2Button;
WinButton btnAdd = this.UIMap.UICalculatorWindow.UIItemWindow2.UIAddButton;
WinButton btnEquals = this.UIMap.UICalculatorWindow.UIItemWindow3.UIEqualsButton;
WinText txtResult = this.UIMap.UICalculatorWindow.UIItem0Window.UIItem0Text;
Mouse.Click(btn1);
Playback.Wait(10000);
Mouse.Click(btnAdd);
Playback.Wait(10000);
Mouse.Click(btn2);
Playback.Wait(10000);
Mouse.Click(btnEquals);
Playback.Wait(10000);
if (txtResult.DisplayText.Trim() != "3")
{
throw new AssertInconclusiveException("Addition failed!!");
}
}
catch(Exception e) {
throw new AssertInconclusiveException(e.Message.ToString());
}
}
/* LaunchCalculator method() */
public void LaunchCalculator()
{
System.Diagnostics.Process.Start("calc");
}
Step 9- Compile the code and then execute/run the test method. For running a test methods just places the mouse pointer anywhere inside the test method and right click the mouse button. Right click menu will be shown out of which select “Run Test” menu item. The test method will start execution.
Step 10- Once you click on “Run Test” then don’t do any other task on desktop. Just wait until the test method executes. If the TestMethod executed successfully then the Test Results floating window will be shown as below:
That’s it. And here is your first CodedUITest done..... !!!
Note: Please click on the images to view enlarged version.
Note: Please click on the images to view enlarged version.
.bmp)
.bmp)
.bmp)
.bmp)
.bmp)
.bmp)
.bmp)
.bmp)
.bmp)
No comments:
Post a Comment