Welcome to MSDN Blogs Sign in | Join | Help

Tutorial – Coded UI Test [Beta2]

Overview

This tutorial guides you through the creation of a Coded UI Test in Visual Studio 2010.

Steps

1. In Visual Studio 2010, click on Test menu and choose New Test

image

2. In the Add New Test dialog, choose Coded UI Test and click OK.

image

NOTE:- You can choose to create a Visual C# test project or a Visual Basic test project.

3. In the New Test Project dialog, change the Test Project name and click Create.

image

4. In the Generate Code for Coded UI Test dialog, choose Record actions, edit UI Map or add assertions

image

5. The Coded UI Test Builder will start up.

image

6. Click on Start Recording button to start the recorder.

Note: There is a slight delay when the recorder starts. Ensure that the button changes to the Pause Recording before recording test steps.

image

7. Now perform the desired test steps.

For the purposes of this tutorial, we are going to test the Add function in Calculator.

NOTE: Coded UI Test works on Web (IE 7 & 8), WinForms & WPF apps. I am choosing Calculator as an application which anyone who is evaluating has access to.

The Test steps are

a. Launch Calculator

b. Click 7

c. Click the + sign

d. Click 2

e. Click = button

Note that a text description of actions will be shown in the Recorded Actions window.The Recorded Actions window can be pinned if you want to see the actions being recorded. It will become semi-transparent when you move focus away from it.

image

8. Click on the Generate Code button and change the name of the method to TestAdd.

image

Click on the Add and Generate button to add this method to the project.

9. Now drag the cross-hair and drop it on result field in the calculator. A blue rectangle is drawn around the selected control.

image

10. Properties of the selected control are shown in the Coded UI Test Builder Window. Right click on the Display Text field and choose Add Assertion.

image

11. In the Add assertion for DisplayText dialog, click OK.

image

12. Click on the Generate Code Button and change the Method Name to AssertSum.

image

Click on the Add and Generate button to add this method to the project.

16. Close the Coded UI Test Builder by clicking on the X button.

17. The Coded UI Test is now opened within the Visual Studio IDE.

image

NOTE the following characteristics about the generated code.

a. The CodedUITest1 class is tagged with a Coded UI Test attribute.

b. The CodedUITetMethod1 method is tagged with a Test Method attribute.

c. in the CodedUITestMethod1, the methods we created are invoked.

 

18. Note the following in the Solution Explorer.

a. All required references for Coded UI Test are added automatically.

b. 5 files are added to the Test Project

i. CodedUITest1.cs - main Coded UI Test class, Test Methods and Assertions.

ii. UIMap.uitest – XML Representation of the UI Map (a container of UI Controls) and the recorded actions.

iii. UIMap.Designer.cs - Code generated for the UIMap

iv. UIMap.cs – empty file where UIMap customizations may be added.

iv. UserControls.cs - Specialized classes representing various UI Controls referenced in the Test.

image

19. Right-click inside the Coded UI Test Method and choose Run Tests

image

20. The code is compiled and then the test is run.

21. All actions recorded earlier will now be played back.

22. After the test run is completed, right-click on the Test Run and choose View Test Results Details.

image

image

Congratulations. You have successfully created and run a Coded UI Test.

Published Thursday, November 05, 2009 3:02 AM by Mathew Aniyan

Comments

# re: Tutorial – Coded UI Test [Beta2]

Thanks for doing this Mathew  -  I have successfully created tests using the new Beta 2 version.  But can you help me with one issue?

In the application I am testing I am using context menus (i.e. right-click pop-up menus).  I can record these fine in the test.  But how to I add an assertion about those pop-up menus e.g. to assert that a pop-up menu item exists and/or is enabled?  I can't figure out how to access the pop-up menu when I am in the AddAssertion mode of the recorder?

Friday, November 06, 2009 3:23 AM by rpawson

# re: Tutorial – Coded UI Test [Beta2]

You can use the {Win}+I key to capture popup menus when in the Add Assertion mode of Coded UI Test Builder.

Friday, November 06, 2009 3:28 AM by Mathew Aniyan

# re: Tutorial – Coded UI Test [Beta2]

Thanks  -  I later spotted the  {Win}+I answer on another of your blog posts.  Still took me a while to get it working.  The trick I missed was that you have to bring up the context menu within the test recording, then stop the recording (which causes the context menu to disappear again); then bring the same context menu back up again before selecting the menu item with {Win}+I in order to do an assert on it.  Obvious once you know, but not so obvious to figure out from scratch.

One other question:  I think that Coded UI Test is a great facility, but is there any possibility of being able to use it from within VS2010 Professional? (I don't currently use TFS, so I'll be reluctant to pay out all that money for Premium edition just to get this test tool)

Friday, November 06, 2009 6:05 AM by rpawson

# re: Tutorial – Coded UI Test [Beta2]

Friday, November 06, 2009 7:17 AM by Juan Trujillo

# re: Tutorial – Coded UI Test [Beta2]

Great post, Mathew, thanks very much!

I have a question for you, if you have a spare couple of minutes. I've been having some trouble getting the Coded UI Test Builder to identify objects in an interface properly.  Here's the scenario:

There are two WinEdits on this interface (I'll dub them "Edit1" and "Edit2").  The goal is to Click in Edit1 and enter some text.  What it keeps doing during playback is Clicking in Edit2 instead.  Now, I've used the Crosshairs to identify the properties of both WinEdits and here's the strange part: In the UI Control Map, both are listed as the same control, but the "ControlName" changes depending on which WinEdit I most recently identified.  What's worse is that if I identify Edit1 and then hit "refresh" in the Coded UI Test Builder, it changes the "ControlName" from "Edit1" to "Edit2".

I am simply using the Test Builder incorrectly?  Any input you might have would be much appreciated.  Or, if there is someone else that you think would be better for me to contact, please feel free to mention it (and I apologize if I have wasted your time).

Thanks very much!

Monday, November 16, 2009 9:45 AM by falkdj

# re: Tutorial – Coded UI Test [Beta2]

I am unable to repro this problem on my test app.

Is it possible to send me the application?

If not, please send me the logs.

To enable logs,

a. Open %VSINSTALLDIR%\Common7\IDE\CodedUITestBuilder.exe.config.

b. Change the Trace level to 4.

The line should look like

<add name="EqtTraceLevel" value="4" />

c. Open VS, run Coded UI Test Builder, drag the cross hair on the controls you are facing issues with. Perform the steps required to repro the problem.

d. Close Coded UI Test Builder.

Zip up the files under %TEMP%\UITestLogs and send to me.

Wednesday, November 18, 2009 3:42 AM by Mathew Aniyan

# re: Tutorial – Coded UI Test [Beta2]

Hi !!

I installed MS-VS 2010 and start work with test projects.But surprisingly i get the following error (or whatever).....

I run the "Coded UI Tester"  for generate code for a simple calculator. In order to do that in the  Generate Code for Coded UI Test dialog, i select Record actions, edit UI Map or add assertions. Then accordingly i get Coded UI Test Builder.

Then i did  some calculations and generate the code for that. To set assertions i include  ' answer text box ' using cross-hair...

Up to this stage i think all i have done is correct.

But to add an assertion to that text box, the property list does not show the property 'Display Text' for me.....!!!!!!!!!!

Actually i got this name "Display Text" from another E-BOOK !! In my case list doesn't show even the name of the property !!!

So , now 'm using different properties and add assertions in the code manually.I'm sure this is not the best way to do this.

Is this a common error ?? If not why 'm getting this ???

thanks

Tuesday, November 24, 2009 10:19 PM by azumi
Anonymous comments are disabled
 
Page view tracker