Udostępnij za pośrednictwem


From the MVPs: “Getting started with Coded UI Testing for your Windows 8.1 Store Apps”

This is the 30th in our series of guest posts by Microsoft Most Valued Professionals (MVPs). You can click the “MVPs” tag in the right column of our blog to see all the articles.

Since the early 1990s, Microsoft has recognized technology champions around the world with the MVP Award . MVPs freely share their knowledge, real-world experience, and impartial and objective feedback to help people enhance the way they use technology. Of the millions of individuals who participate in technology communities, around 4,000 are recognized as Microsoft MVPs. You can read more original MVP-authored content on the Microsoft MVP Award Program Blog .

This post is by Visual Studio ALM MVP Mike Douglas . Thanks, Mike!

I recently created and published my first Windows 8 Store application to the store.  I created it using XAML/C# and followed the MVVM pattern.  As you can imagine I learned a number of things throughout the process.  Now that it is done, I want to rewrite the underlying codebase using those lessons learned.  So how can you rewrite the code but ensure that none of the functionality breaks?  Unit tests provide a great way to regression test your application.  However, adding dependency injection and better unit tests is one of the things I need to do as part of the rewrite, so this won’t provide me the test coverage to ensure that my application is functioning properly.  In addition, the unit tests will only test the individual components and not be able to test all of the layers and ensure the UI is functioning properly.   Coded UI Testing has traditionally provided the mechanism for creating functional tests that tests all of the layers the can be used for regression testing.  With Windows 8 store apps however, Coded UI Tests were not supported and I wouldn’t be able to verify the gestures in my application.

 

Now with the release of Windows 8.1 and Visual Studio 2013, Coded UI Tests can provide automated functional testing against the UI including not only the mouse and keyboard inputs, but also to verify that the gestures react properly. 

How do I get started?

The first two things you need are Windows 8.1 and Visual Studio 2013.  Using only one of these will not work.  Also, Coded UI Tests for Windows 8.1 only supports XAML based applications.  If your application was written for Windows 8.0, you need to retarget it to Windows 8.1.  This is done very easily by opening the project in Visual Studio 2013, right clicking on the project, and selecting Retarget to Windows 8.1.  For more information on retargeting your application, see the MSDN article Retarget your Windows Store app to Windows 8.1.

 

image

 

Next add a Coded UI Tests project to your Windows 8.1 store app project.  Notice that there is a new project type found under Windows Store.  If you use the traditional Coded UI Tests project under Test, it will not work with Windows 8.1 Store apps.

image

 

Creating the Coded UI Tests are very similar to other Coded UI Tests, however the primary thing you will see missing is the record a test option.  Recording Windows 8.1 store app is not available in this release.  Personally I don’t miss it much because I believe manually coding the tests are more robust and easier to maintain even though they take a little longer to create.

 

To launch a Windows 8.1 Store app from your Coded UI test, use the XamlWindow.Launch() method as shown in the snippet below.

 

XamlWindow myapp = XamlWindow.Launch("DeliveronConsultingServic.TeamPlanningPoker_s9dp6hmz44fsa!App");

To find the name of the application, use the Coded UI Test Builder’s cross hair tool and select the application to test.  Use the value of the AutomationId for the application name to launch as shown in the figure below.

 

image

Gesture examples

As I mentioned earlier, Coded UI Tests for Windows 8.1 not only support traditional input such as the mouse clicks and keystrokes but it also supports the Windows 8.1 gestures.  The following list are two examples of common gestures used in Coded UI Tests.

 

Tap

I use the Tap gesture to represent selecting any item as if I were clicking the mouse button.  In this example I am tapping the back button.

 

Gesture.Tap(UIMap.UITFSAgilePokerWindow.UIBackButton);

Slide

I’m using the slide to scroll through the different backlog queries.  I also created an example that will perform the slide to open the AppBar (not shown).

 

Point startPoint = new Point(UIMap.UITFSAgilePokerWindow.BoundingRectangle.X + UIMap.UITFSAgilePokerWindow.Width / 2,

                            UIMap.UITFSAgilePokerWindow.BoundingRectangle.Bottom - 1);

Point endPoint = new Point(startPoint.X, startPoint.Y - 50);

 

Gesture.Slide(startPoint, endPoint);

 

Additional gestures are covered in this great MSDN post, Coded UI Test Gesture support in Visual Studio 2013

Shortcut Keys

Windows 8 and 8.1 have introduced a number of shortcut keys that are critical for testing your application.  To open the Settings Charm or AppBar, use the examples below.

 

// Open Settings Charm

Keyboard.SendKeys("I", ModifierKeys.Windows);

 

// Open AppBar

Keyboard.SendKeys("Z", ModifierKeys.Windows);

In addition to using SendKeys to open the Appbar and Charms during the execution of the tests, if you are building your UIMap, some keyboard shortcut keys will be important to know.  If you want to add any control from the AppBar or any Charm, you need to use CTRL+SHFT+I to select control and add it to the UI Map.  If you try to use the mouse, the menus will retract as soon as you click on the builder tool.

 

image

 

Other important shortcut keys

 

· CTRL+I – This will select an item to view the properties but not add it to the UI Map.

· CTRL+U – This will open the Add Assertions window with the control selected that you hovering over.

Summary

If the lack of automated functional UI testing has been holding you back from building your Windows Store app or if you are like me and have a Windows 8 Store app in the market but want to do some major refactoring, start using Visual Studio 2013 with Windows 8.1 to take advantage of this amazing feature.  There are only a couple features that I wish Coded UI Tests for Windows 8.1 Store apps supported, and I feel that it includes everything you need to create a full regression suite of automated UI functional tests.  Microsoft is listening to your feedback and feature requests, post them on the Visual Studio UserVoice site to be heard.