Coded UI Test (CUIT) using Visual Studio 2010

Posted by: Gouri Sohoni , on 5/10/2011, in Category Visual Studio, VSTS & TFS
Views: 53985
Abstract: In this article, we will see how Coded UI Test can be created in Visual Studio 2010 by recording the action using CUIT Builder, how to add assertion and how CUIT can be executed. In the article, we will also see another way of creating CUIT by first executing the test case by using Microsoft Test Manager 2010, recording the actions, convert the actions to Code using Visual Studio 2010 and executing it.

Till the previous version of Visual Studio and its various tools for testing, there was no way a User Interface could be tested as automated tests. With Visual Studio 2010, a brand new feature has been added called Coded UI Test or commonly known as ‘CUIT’. This new type of unit test is available with Visual Studio 2010 Ultimate or Premium version. It provides functional testing for the user interface and the code gets created with the help of high level languages like C# or VB.NET.

You can create Coded UI Test only by using Visual Studio 2010. One way is by using the existing manual recording and the other is by creating a recording using CUIT Builder. If we have already executed our manual test case with the help of Microsoft Test Manager 2010 (a new installable and a separate UI for manual testing with Visual Studio 2010 Ultimate or Microsoft Test Professional 2010), we will have the actions recorded based on which code can be generated.

 

Update: Part II and Part III of this article can be found here

Coded UI Test (CUIT) - Convert Manual Test Case to Code using Visual Studio 2010  and 

In this article, we will discuss how to create our own action recordings, add assertions as and when required and convert all these actions to its code, with the help of CUIT Builder. We have direct support for recording the actions to technologies like Windows Application, Web Application, and WPF Application and also to Silverlight, with the help of feature pack. In case we want to extend support to any third party application, various APIs have been exposed which can be extended as and when required.

image

In the diagram shown above, you can view that the lowest level is for the technology specific layer. The second layer is the abstraction layer which helps in abstracting rest of the code from different technologies. Over it, you can see record and playback. First the steps are recorded and later filtered (depending upon rule) and converted to code (which can be with high level languages like C# or VB.NET as already mentioned). The playback engine comes with an API and property provider. This will give the information about various properties supported by the control and also for browser specific things like navigation. The top layer currently supports two clients. One is Microsoft Test runner 2010, which provides the Fast Forward functionality. The second is Visual Studio 2010 which provides Coded UI which will generate code, after recording actions. Last option is to add hand written code.

Let us take an example of recording the actions by starting browser, entering URL and other actions. The feature of CUIT involves intent aware recording. Eg: In case we want to start MS Excel, clicked on the Start button, entered Excel in Search and then selected Microsoft Excel 2010,  then the recording will only be done while Excel is launched.

Let us see some steps:

Step 1. With Visual Studio 2010 launched, create a new project with the template of Test Project as shown beliw. The default UnitTest1.cs file added can be deleted. You can select a language of your choice.

cuit-test-project

Step 2. Click on the Test menu, Select New Test and select the template of Coded UI Test, provide the name for .cs file and then a dialog with 2 options will be displayed as follows:

cuit-creation-methods

Step 3. Let us select the radio button for “Record actions, edit UI maps or add assertions”. We will start seeing the CUIT Builder on the right side bottom corner of the screen as shown. Click on the icon for starting the recording

cuit-builder

Step 4. Click on the start button and start the browser Internet Explorer. Enter the url as http://www.ssgsonline.net in the address bar. Take the mouse to the ALM assessment services (wait for a couple of sec) and hover over Consulting Services. Click on the icon to pause recording from CUIT Builder and then click on ‘show recorded steps’. Observe that the mouse hovering is also been recorded as action.

cuit-mouse-hover

Step 5. Resume the recording and click on the first product for eLearning. Let us generate code for all previous actions. Click on the icon to pause recording and then click on generate code. The default name for the recorded method is seen which can be changed to something meaningful.

Step 6. Just recording the actions may not be useful as we also need to validate certain actions. This is similar to using Assert in unit test where we can equate the actual and expected value. Let us add assertion in terms of the price for the product. Click on ‘Add assertion’ icon and then add assert method so that the price USD 23.00 is taken as the value for passing or failing the test.

cuit-add-assertion

Step 7. Resume the recording, close the browser and generate code by giving a suitable name. Close the CUIT Builder so that the control goes back to the Visual Studio. We can now see the 3 methods (we created the recording for) added as follows:

cuit-code-generated

Observe the attribute for the method.

Step 8. Open the Solution Explorer and view the different references added. The 4 main components which get added with CUIT can be seen below

cuit-components

All the actions recorded will be in UIMap.Designer.cs. In case you want any custom code to be added, then it is recommended to put it in the partial class file called UIMap.cs. UIMap.uitest file will show all the xml equivalent and the .cs file will have the calling code as is shown in previous step.

Step 9. The code generated for the method can be viewed by selecting the method, right clicking on it and select the option of ‘Go To Definition’

cuit-code

Step 10. Now that our Coded UI Test code is created, we can execute our CUIT from Test View. Test View Windows can be viewed by selecting Test menu, windows option followed by Test View. We need to refresh the view so as to see the newly added tests. The execution of the test will show all the actions we have recorded and finally give the result in the Test Results area.

The new actions can be recorded when required by right clicking on the empty area in code window and selecting ‘Use Coded UI Test Builder’ from ‘Generate Code for Coded UI Test’ option. With feature pack 2 installed, we can even have Coded UI Test Editor which can give following feature when selected with UIMap.UITest file and Open selected

cuit-editor

 

Conclusion

In this article, we have seen how Coded UI Test can be created by recording the action using CUIT Builder, how to add assertion and how CUIT can be executed. In the article, we have also seen another way of creating CUIT by first executing the test case by using Microsoft Test Manager 2010, recording the actions, convert the actions to Code using Visual Studio 2010 and executing it.

Give me a +1 if you think it was a good article. Thanks!
Recommended Articles
Gouri Sohoni is a Microsoft MVP, Trainer and consultant for over two decades. She is a Microsoft Certified Trainer (MCT), MCITP and has conducted several corporate trainings on Microsoft technologies that include Visual Studio 2010 (ALM), SQL Server 2008 BI, SQL Server 2008 developer track, SQL Server 2005 BI, SQL Server 2005 developer track etc.




Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Meg Ryan on Tuesday, May 24, 2011 3:02 PM
Hello!!!

This was a really great post. I will keep visiting here.

Thanks for such a nice post.

Meg

Comment posted by manjunath on Tuesday, June 14, 2011 6:03 AM
hi it was good explanation,
1. can tell me how to validate (check) whether the applciation is closed after click on close button.
2. if the applciaton is failed my main window is not closed. ie if asseration fails next it is not executing the close operation.
please give me reply.
Comment posted by cyp on Friday, November 18, 2011 6:33 AM
very good post. The only problem is that running a simple automation that clicks an existing, visible button sometimes fails saying that the button does not exist.
Comment posted by purushothama p on Monday, April 09, 2012 6:22 AM
hi gouri,

may i know how to record the recorded coded ui test
Comment posted by Gouri Sohoni on Thursday, April 12, 2012 11:42 PM
Hi purushothama, Coded UI test can be created wither by recording the actions using Microsoft Test Manager or by using CUIT builder (from Visual Studio)
Comment posted by Mahesh on Thursday, July 26, 2012 5:55 AM
CodedUITest1.cs file not visible to me in solution explorer.

Please help
Comment posted by Gouri Sohoni on Thursday, July 26, 2012 7:27 AM
Hi Mahesh,
Let me know in details what steps you have followed.
Comment posted by sreedhar on Monday, July 30, 2012 7:44 AM
HI ,
It is really Help me for a Automation
Thank u Very Much
Comment posted by rajesh on Tuesday, July 31, 2012 12:46 AM
very good post, thank you for your information.
please let me know where can i get the advanced concepts examples
Comment posted by Leah on Tuesday, September 04, 2012 4:25 AM
Hi Gouri, How can the view in Step 10 be displayed? Thanks!
Comment posted by Gouri Sohoni on Wednesday, September 05, 2012 10:05 PM
Hi Leah,
The editor view which is shown can be obtained after installing Feature Pack 2
Comment posted by Leah on Monday, September 10, 2012 4:29 AM
Thanks Gouri. I realized that after some digging.. and unfortunately for me MSDN subscription is required.
Comment posted by kalyansantosh on Wednesday, October 03, 2012 12:03 AM
Im a learner for coded ui test. can u please guide me for one flow of the applciation.
In my application i have child items like in menu bar we have child items
like  
1 (if we place the cursor on 1 it displays 2 )
2  (if we place the cursor on 2 it displays3,4,5,6 )                 3
                                                                                     4
                                                                                      5


now the query is i want to click 3
while rcording it is identified.but in playback it is not working
Comment posted by Sravan on Tuesday, October 09, 2012 7:19 AM
HI Gouri,

the above article is really nice but i am not using recording and play back can you please let me know how to launch the window based application.it will be a great help for me.
i know how to launch web based application using CUIT.i want to know for window based application.
Comment posted by Tung on Tuesday, February 26, 2013 4:06 AM
I try Visual Studio 2010 Premium Edition (bundled with Coded UI feature),
I create a blank new Test Project in C# and add new CodedUITest in the project, then it prompted popup modal windows and I choose the 'Record actions, ...'
But I immediately find the following error...
"There was an error generating XML document"
I'm working in Win7 64 bit

Do anyone meet this issue? any one have idea on how to fix this...?

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel