Archive for the ‘Software Testing’ Category

What are Coded-UI Tests?

Coded-UI tests provide a mechanism to automatically execute and validate a test case or workflow that operates at user-interface layer. These tests “drive” the application like a normal user with a keyboard and mouse.

The tests are created in Visual Studio Ultimate or Premium edition and have been supported since VS 2010. A good description of these tests is detailed here.

In this article we’ll look at creating a Coded-UI test for a common S3D task – Refreshing a session file. It will be a step-by-step walkthrough, so I would suggest having VS 2010+ and S3D ready so that you can follow along.

Phase 1

Start SmartPlant-3d and define your workspace using your filter. Then click on File->Save, and save your session file to a desired location. Here I’m just saving it to the Documents folder.




Phase 2

Start Visual Studio with admin option and choose to create a Class library project of type “Test” and then select “Coded UI Test Project” as the template to use.


At this stage you will be prompted with a dialog asking about how to create the UI test. As we will be recording actions, choose the “Record action” option.


This will start the “UIMap – Coded UI Test Builder” and it will show up as a small tool pallet in the lower right corner of the screen. This tool allows you to record actions like clicking buttons, typing text, etc. and for identifying control and their properties.


Phase 3

Now click the red record button and the tool will change the button to indicate that it’s recording actions. Do note that you should be careful to record only actions that are absolutely required and not pollute the recording with unneeded actions/gestures/etc.

Now click on Start (Windows Start) menu and then click on SmartPlant 3D link as shown below:


At this stage S3D will load up and “New” template dialog will show up as shown below:


Move the mouse to the UI Builder and click on the first icon from left to pause the recording. Next click on “Recorded Actions” icon and you should see something like this:


If you see any extra recorded steps, click on it and then press the Delete key to remove the action. Let’s go ahead and create a method for these set of actions and call it “LaunchS3D”.

Phase 4

Click on the “Generate Code” icon and type in the name, “LaunchS3D”, along with a  method description and then click on “Add and Generate” button:


Visual Studio will generate the action method and switch back to UI Builder so that you can start recording the next set of actions.

Phase 5

So at this stage we have a method to launch S3D and show the “New” template dialog. So now we need to click on the top-right “X” in this dialog to close it.

In other words, we need a way to identify the “X” control. The way you do in Coded-UI test is to “add” the “X” to it’s “UI Map”, so that VS can create a representation of that control in code.

So go ahead and move your mouse over the “X” to highlight it and then press CTRL + I and you should see this:


You can see that the UICLoseButton control has been identified by VS and added to it’s UI MAP. Also you can see that UINewWindow is the parent of UICloseButton. So if we need to close the UICloseButton, we need to make sure it “it” exists or it’s parent, UINewWindow exists.

We can add such a check in VS by using an assert. So click on the UINewWindow->UINewTitle control and then click on the Exists property. Then click on “Add Assertion”, so you should have something like this:



Click on Ok and the click on “Generate Code” link and type in a name for the assert method, say “CheckNewTemplateWin”:


This will generate the code for our assertion, “CheckNewTemplateWin”.

Now click on the first button to start recording and then move mouse over the new template dialog and click on the Close “X” button.

Then click on “Recorded Steps” and you should see this:


Go ahead and click on the “Generate Code” and then create a method called “CloseNewTemWin”


So at the end of Phase 5, we have created method to start S3D, check for new template dialog, and finally, closes the dialog.

Phase 6

Now within S3D, press the keystrokes CTRL + O which should bring the “File Open” dialog and then click on “Recorded Actions” in UI Builder and you should see this:


In S3D, press ALT + N to focus in the “File name” dialog and type in the path to you saved session file. Press “Enter” to start loading the session file.

At this stage, click on “Recorded Actions” in the UI Builder and you should have something like this. Again if you see an extraneous actions in the recorder, go ahead and delete them:


After your session file loads up, go back to the UI Builder and pause the recording. Then click on “Generate Code” and create a method called “LoadSessionFile” for the set of recorded actions. Make sure to click on “Add and Generate” to have Visual Studio create the method within your project.

Phase 7

So let’s recap what we have now. We have a method to start S3D, one to check the new-template dialog and close it and lastly one to load the session file. So what we need now is to record the actions to refresh the session file and then save it.

So start recording and click on the “Select Command” arrow in S3D and then press F5 key to start refreshing the file.

You should see these set of actions recorded:


Now switch back to S3D after making sure that the recording is in recording state image, and then click on the top-right “X” in S3D to close it. When the save session file dialog comes up, don’t dismiss it by clicking on the “Yes” button.

Click on the “Show Recorded Steps”, icon in UI Builder and you should see the following, again delete any extraneous actions:


At this stage, let’s generate a method for these set of actions which is – clicking on the top-right “X” or close button in S3D window. So click on “Recorded Actions” first, then fill in the method name and next click on “Add and Generate” to generate code for the method.


Phase 8

Now we need to code the method to click on “Yes” button to save the refreshed session file. So we need to capture the “Yes” button in VS UI Map. So like before, first pause the recording, then hover your mouse over the “Yes” button and press CTRL + I to get something like this:


Since we want the “Yes” button to be visible before we click on it, let’s add an assertion for that check. So click on the “Exists” property and then click on “Add Assertion”


Then close the UI Map and click on “Generate Code” button and finally generate the code for the assertion.


Now we will add the code for the click action on the “Yes” button. Make sure recording is on and then click on the “Yes” button which will cause S3D to save the session file and exit.

Now click on “Recorded Actions” and you should see this:


At this stage go ahead and click on “Generate Code”, then give the method a name and create it within your project:


This should complete our action recording for this use case. You can now close the UI Builder and switch to Visual Studio.

Phase 9

If you switch to Visual Studio, you should see something like this. The generated “TestMethod” lists all the asserts and methods that we created during this exercise.


The file, CodedUITest1.cs, which contains these methods is also highlighted in the Solution Explorer. There is another file called, UIMap.uitest, in the Solution Explorer that contains the UI Map that we worked with so far.

Do this, double-click on UIMap.uitest and you should see something like this:


On the left you can see all the actions and asserts and on the right you can see the UI Map that you worked with.

Go ahead and start S3DHost by double-clicking on the session file. Then press Alt+F4, to bring the “Save Session File” dialog. At this stage, shift focus to Visual Studio, right-click on “UIYesButton” and choose the menu, “Locate UI Control”.


You should see focus shift to S3DHost window with the “Yes” button highlighted as shown below:


So this confirms that our UI Map is correct and we are able to locate the control that we stored a reference to during our recording session.

Phase 10

Let’s now try to play or test our recorded actions. So double-click on CodedUITest1.cs to bring the code window to front. We now need to build the solution, so click on Build->Build Solution. Alternatively, you can use the F6 key which is the shortcut key to build your solution.

Once that’s done and you get a message in the status bar saying “Build succeeded”, click on the menu Test->Windows->Test Explorer, to show the test window.


You should now see something like this:


You should now be able to, either click on one of the “Run” menus, or right-click on your test method and choose “Run Selected Tests” to invoke the actions that you recorded.

So go ahead and choose one of the options and you see S3DHost start up and go through the recorded actions. If all the actions succeed, you should get a green check next to your test as shown below.


To make sure that the actions do work, you can try to place some objects in S3D, run the tests and finally open the session file to see if the new objects do show up.

Testing From Command Line

You can use MSTest.exe and VSTest.Console.exe (for VS 2012 plus) to run coded ui test. These are located under Visual Studio installation folder under the following path:


So go ahead type in cmd in the windows start dialog. Then press the Shift key and right-click on cmd to choose “Run as administrator”


Navigate to the MSTest.exe folder location and then type in MSTest /? ,to list the options available with the tool.

We will use: /testcontainer:path_to_our_dll /resultsfile:path_to_results_file

The dll file is the Dll created by your project and resides in the Debug (or Release) folder. You can see that location by right-clicking the project in Visual Studio and choosing “Open Folder in File Explorer”


Then you can navigate to the bin\Debug folder and copy the path to the directory.


Once you run the test, it will create the results file and output the end result onto the DOS window, as shown below.



If you stayed with me and made it this far Smile, then you should have a fairly good idea about the capabilities of Coded-UI Test. Hopefully you can leverage it’s abilities and use in creating automated test for your functional testing workflows.


Read Full Post »