Coded UI Test: Convert Normal CUIT to Data Driven CUIT using Visual Studio 2010

Posted by: Gouri Sohoni , on 6/11/2011, in Category Visual Studio
Views: 54042
Abstract: In this article, we will discuss how a normal Coded UI Test can be converted to Data Driven Coded UI Test in Visual Studio 2010. We will also discuss various data sources which can be used to provide data to the test method.

In the previous articles, we have discussed How to create CUIT by using CUIT Builder, How to create CUIT by converting manual to automated and Variations and Editing with CUIT. In this article, we will discuss how existing CUIT can be converted to take up data from any Data Source.

We will use existing CUIT which records actions for Windows Application. This is a part of Employee Management System’s Windows functionality. The HR person can add, modify and view employees in the organization. This application involves Login form which requests for User Name and Password. After a successful login, the HR can view the Employee’s record by entering EmployeeId. This id is given as a constant value (while recording) which we will pick up from various data sources, by adding the DataSource attribute to the test method.

Following are the methods created for the application under test.

cuit-code-snippet

Let us see how to data bind the Coded UI Test step by step.

 

Step 1: Select the CUIT named CUITWinApp from the Test View. Test View can be seen from Test – Windows and Test View Option. Select the method and select properties for the method, Click on the ellipse button for the property of Data Connection String

cuit-data-connection-string

Step 2: Clicking on the button automatically starts the New Data Source Wizard for us where we can select Data Provider from a table, .csv file or .xml file as can be seen from the screenshot.

cuit-data-dource-wizard start

Step 3: Let us connect to a table from SQL Server with following Employee data in it:

cuit-data

Step 4: Select Database from the wizard and click on the Next button. Click on ‘New Connection’ and provide the following details.

cuit-sql-connection

Step 5: Now what remains to select is the table name which is having the required data.

cuit-data-dource-wizard start

Step 6: Now that the data binding is done, we can see the attributes added to the test method.

cuit-datasource-attr

Observe the TestMethod attribute remains as it. Along with it, we can view the DataSource, DataAccessMethod getting added. The provider complete connection string with table name and the data access method can be seen. The data access method can either be sequential or random.

Now when we execute this CUIT, it will get executed as many number of times as there are records in the table (3 in our case). The data access can also be random. The DataRow from TestContext can point to the column number as shown or the column name in quotes.

Step 7: Now that the connection string is provided, we will have to specify that the data is coming from the Data Source and not as a constant value which was recorded with CUIT Builder. With every method added using CUIT Builder, a method with “Params” as ending word gets automatically added. This is the method which will be helpful. We will need to make the following change as shown below. With this code snippet, we can view the column name instead of column index.

image

 

Step 8: Now we will execute the CUIT from the Test View after refreshing the view. The Test Results window is as shown below:

cuit-test-result-sql

The details of test run are as shown below:

cuit-test-results-details sql

Here all the 3 iteration can be seen as passed.

Step 9: We will now create a .csv file and repeat the process. We will have to first add the file to the solution and provide data to it. We will add a New Item to the solution, select text file, specify the extension as .csv and enter the following data

cuit-data csv

Step 10: Select the method in Test View and change the Data Connection String property. The data source wizard now is connected to the .csv file

cuit-data-source-wizard-to csv

Step 11: Once the connection string is changed, we need to change the value for the column name in the code. Here instead of the column name, the index is used. The complete code snippet looks like the following:

cuit-code-snippet csv

The DataSource attribute now points to the .csv file and also the name of the file can be seen.

Step 12: On executing ,we get result as failed, as one of the record entries is not available in the table. The detailed test result displays that the test failed for one record

cuit-test-result csv

Step 13: Similarly the data can be obtained from an XML file after creating the file and adding it to the current solution. We just need to change the data connection string for the xml data later.

Conclusion: In this article, we discussed how a normal Coded UI Test can be converted to Data Driven Coded UI Test. We also discussed various data sources which can be used to provide data to the test method.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

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!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
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.


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Viplav on Thursday, June 23, 2011 3:35 AM
Hi Gouri,
Good to find this article.Even I have some article on this same topic ,may be you can browse through them at My Blog : http://www.csesupport.blogspot.com , but one challenge I would really love to explore and also like you explore on is in today's development architecture , we have the Mashup technology of developement, wherein we find the hieracrchy issue blocking smooth automation script run on different environment(Builds). The reason I feel is the object hieracrhy itself is data bound and hence the object recosgnition fails in many cases. I would like to know if Coded ui Has any ways of providing a resolution to the problem ?
Comment posted by Priyabrata Pattnaik on Monday, June 18, 2012 8:27 AM
Hi Gouri,
Thank you so much for posting this article and it was very helpful. I was justwondering if you could help me in Coded UI Testing for SharePoint 2010 application? I am very new to the Coded UI tool and I have no experience at all. But my work I need to perform this task by myself. So just wondering if you can help me into it, would be really appreciated. I have no idea from where I should start from? How to learn the Coded UI tool.I have Viual Studio 2010 premium installed in my laptop. So please help me with this, if you can.

Thanks & Regards
Priyabrata
Comment posted by Gouri Sohoni on Friday, June 29, 2012 2:33 AM
Hi Priyabrata,
There are a lot of posts in this by Mathew which will help you start with CUIT. http://blogs.msdn.com/b/mathew_aniyan/
Comment posted by Sandeep K on Monday, September 24, 2012 9:55 AM
Hi Priyabrata,
Thank you for posting this article,it was very helpful.I have an automation scenario where thje user enters search criteria in a Textbox and the results are displayed in a GridView,sometimes i get a single result in the gridview and sometimes i get mutiple results.I am facing issues adding assertions or validations and i not sure how to validate dynamic controls.Could you please guide and share some sample code.
Thanks and Regards
Sandeep K

Comment posted by Dharmender on Friday, November 23, 2012 5:15 AM
Hi Gauri,
I am new to Coded UI. I want to test data on my page. Say I have a Div which intern have sub Div`s and this forms  table kind of structure with each cells as Div. now My each cell is filled with data from database. And this is shown on UI.Now I capture the top Div i.e parent Div using cross hair. nad I generate an assertion. Now to compare this data using a dataset which I have directly got from database. My idea was to get the Div structure from Ui using crosshair and convert it into either xml or dataset, so that I can easily compare it with my existing dataset.. Can you please help me in this. I am noy able to get a proper hierarchy when I capture the Parent Div using cross hair...
Comment posted by Dharmender on Friday, November 23, 2012 5:36 AM
Hi Gauri,
I am new to Coded UI. I want to test data on my page. Say I have a Div which intern have sub Div`s and this forms  table kind of structure with each cells as Div. now My each cell is filled with data from database. And this is shown on UI.Now I capture the top Div i.e parent Div using cross hair. nad I generate an assertion. Now to compare this data using a dataset which I have directly got from database. My idea was to get the Div structure from Ui using crosshair and convert it into either xml or dataset, so that I can easily compare it with my existing dataset.. Can you please help me in this. I am noy able to get a proper hierarchy when I capture the Parent Div using cross hair...
Comment posted by Ramesh on Tuesday, January 8, 2013 7:29 AM
Hi Gouri,
Thank you so much for posting such a great article. But, I am facing problem with Coded UI data driven. I have CSV file with 4 records. I am calling this file to my test project. When I run my test, I am not receiving all 4 records from the CSV file. I am receiving one by one record and passing to my application and executing the data and closing application. Again starting new instance and getting second record and executing then closing. This is happening until 4 records finished. So, please help me how to get all my test data at a time and pass to my method using for loop.

Regards
Ramesh
Comment posted by Ramesh on Tuesday, January 8, 2013 7:38 AM
Hi Gouri,
Thank you so much for posting such a great article. But, I am facing problem with Coded UI data driven. I have CSV file with 4 records. I am calling this file to my test project. When I run my test, I am not receiving all 4 records from the CSV file. I am receiving one by one record and passing to my application and executing the data and closing application. Again starting new instance and getting second record and executing then closing. This is happening until 4 records finished. So, please help me how to get all my test data at a time and pass to my method using for loop.

Regards
Ramesh