Coded UI Test – Re-use and Customize the code generated with CUIT Builder using Visual Studio 2012
Posted by: Gouri Sohoni ,
on 2/7/2013,
in
Category Visual Studio
Abstract: In this article, we will discuss how to customize and re-use the code which gets automatically generated with CUIT Builder using Visual Studio 2012.
In previous articles, we have seen various aspects of working with Coded UI Test (CUIT). We have discussed starting from preliminary creation of CUIT to variations and best practices of it. We also have discussed how data driven Coded UI test can be created. In this article, we will discuss how to customize and re-use the code which gets automatically generated with CUIT Builder using Visual Studio 2012.
We will also customize the code created to suit our needs and explore how the code from the designer (UIMap.Designer.cs) file can be copied in the a partial class (UIMap.cs) and customized.
We will start by creating a Coded UI Test to start a browser (Internet Explorer), enter a url, provide search criteria and add assertion for the number of results found.
1. Create a Coded UI Test project using Visual Studio 2012
2. Rename the default name from “CodedUITest1.cs” to “CUITCustomize.cs” as follows
3. Change the method name
4. Now that we have changed the method name as well as the class name, we will add code by using Coded UI Test Builder.
5. Start Coded UI Test Builder
6. We will record actions. Start browser Internet Explorer and enter URL https://www.dotnetcurry.com
7. Provide search criteria for finding out how many articles are available for “Coded UI Test”.
8. Add assertion for the InnerText property.
9. Change the assertion criteria from Equal to to Contains
10. Generate separate method names for different actions and finally for assertion
11. The test method created looks as below
12. Stop the Coded UI Test Builder.
13. Start Test Explorer window if it is not already shown, and execute the test
14. The test execution is successful and we get following result
15. Now we will convert this normal Coded UI Test to Data Driven CUIT. Add a .csv file as a data file which will have search criteria and number expected for articles. Add the attributes to the method and the code and the data file looks as below
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV”,|DataDirectory|Searches.csv",
"Searches#csv",DataAccessMethod.Sequential),]
[DeploymentItem("Searches.csv")]
[TestMethod]
public void CodedUITestMethod_CustomizeCode()
{
this.UIMap.SearchStringParams.UICtl00qEditText = TestContext.DataRow["SearchString"].ToString();
this.UIMap.AssertNumberofSearchesExpectedValues.UIAbout44results016secPaneInnerText =
TestContext.DataRow["Number"].ToString();
this.UIMap.EnterURL();
this.UIMap.SearchString();
this.UIMap.AssertNumberofSearches();
}
16. Observe the change in the code where the parameter for search criteria is taken from data source. Similarly there is a change in the assert expected values.
17. Now we need to change the code for assertion where instead of searching for string value, we want to check the minimum number of articles by converting to integer.
18. We cannot change the method for assertion in the UIMap.Designer.cs. If you change this file, the changes to the file will be overwritten. This file gets re-generated from UIMap.uitest. If we want to change the method AssertNumberofSearches, we will go ahead with the following approach
Copy the complete code for the method and put as a part of other partial class file. We cannot have the same method name in the same class so change the name of the method and customize the code as required. The original method looks as below
public partial class UIMap
{
public void AssertNumberofMinSearches()
{
#region Variable Declarations
HtmlDiv uIAbout44results016secPane =
this.UIDotnetcurrycomSearchWindow.UIGoogleSearchFrameFrame.UIGoogleCustomSearchDocument.UIAbout44results016secPane;
#endregion
// Verify that the 'InnerText' property of 'About 44 results (0.16 seconds)' pane contains '44'
StringAssert.Contains(uIAbout44results016secPane.InnerText, this.AssertNumberofSearchesExpectedValues.UIAbout44results016secPaneInnerText,
"number of results not matching");
}
19. The Customized code looks as follows
public partial class UIMap
{
public void AssertNumberofMinSearches()
{
#region Variable Declarations
HtmlDiv uIAbout44results016secPane =
this.UIDotnetcurrycomSearchWindow.UIGoogleSearchFrameFrame.UIGoogleCustomSearchDocument.UIAbout44results016secPane;
#endregion
string str = uIAbout44results016secPane.InnerText;
string[] words = str.Split(' ');
if (Convert.ToInt16(words[1]) < Convert.ToInt16(this.AssertNumberofSearchesExpectedValues.UIAbout44results016secPaneInnerText))
{
Assert.AreEqual(true, true);
}
else
{
Assert.AreEqual(true, false);
}
}
}
20. Now when we use this method in the calling code, the complete code looks as below
While executing the CUIT, it will be iterated as many number of times as we have records in the data file
In this article, we discussed how we can re-use the code generated by using Coded UI Test Builder in the designer file. We also discussed how the code can be customized. It is not possible to change the code in the designer file as it gets overwritten every time. We need to add the code in the other partial class file provided. We can only copy those methods from UIMap.Designer.cs to UIMap.cs which needs to be modified keeping the remaining code intact.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Gouri is a Trainer and Consultant on Azure DevOps and Azure Development. She has an experience of three decades in software training and consulting. She is a graduate from Pune University and PGDCA from Pune University. Gouri is a Microsoft Most Valuable Professional (MVP) - Developer Technologies (Azure DevOps), Microsoft Certified Trainer (MCT) and a Microsoft Certified Azure DevOps Engineer Expert. She has conducted over 150 corporate trainings on various Microsoft technologies. She is a speaker with Pune User Group and has conducted sessions on Azure DevOps, SQL Server Business Intelligence and Mobile Application Development. Gouri has written more than 75 articles on Azure DevOps, TFS, SQL Server Business Intelligence and SQL Azure which are published on
www.sqlservercurry.com and
www.dotnetcurry.com. You can connect with her on
LinkedIn.