Andrew Connell [MVP SharePoint]
1741 Posts |  46 Articles |  5310 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
Andrew Connell's RSS Feed    
AC's Blog Quick Links
SharePoint Quick Links
Article Categories
Archives
May, 2012 (3)
April, 2012 (4)
March, 2012 (4)
February, 2012 (4)
January, 2012 (12)
December, 2011 (6)
November, 2011 (3)
October, 2011 (17)
September, 2011 (8)
August, 2011 (6)
July, 2011 (7)
June, 2011 (13)
May, 2011 (9)
April, 2011 (15)
March, 2011 (1)
February, 2011 (6)
January, 2011 (5)
December, 2010 (11)
November, 2010 (6)
October, 2010 (12)
September, 2010 (5)
August, 2010 (4)
July, 2010 (5)
June, 2010 (6)
May, 2010 (11)
April, 2010 (11)
March, 2010 (9)
February, 2010 (9)
January, 2010 (3)
December, 2009 (10)
November, 2009 (15)
October, 2009 (15)
September, 2009 (7)
August, 2009 (4)
July, 2009 (10)
June, 2009 (8)
May, 2009 (2)
April, 2009 (9)
March, 2009 (6)
February, 2009 (16)
January, 2009 (6)
December, 2008 (12)
November, 2008 (12)
October, 2008 (27)
September, 2008 (13)
August, 2008 (14)
July, 2008 (14)
June, 2008 (12)
May, 2008 (23)
April, 2008 (12)
March, 2008 (15)
February, 2008 (13)
January, 2008 (12)
December, 2007 (10)
November, 2007 (8)
October, 2007 (15)
September, 2007 (20)
August, 2007 (21)
July, 2007 (16)
June, 2007 (8)
May, 2007 (25)
April, 2007 (16)
March, 2007 (18)
February, 2007 (18)
January, 2007 (12)
December, 2006 (16)
November, 2006 (13)
October, 2006 (18)
September, 2006 (22)
August, 2006 (27)
July, 2006 (23)
June, 2006 (23)
May, 2006 (23)
April, 2006 (9)
March, 2006 (17)
February, 2006 (15)
January, 2006 (23)
December, 2005 (31)
November, 2005 (32)
October, 2005 (38)
September, 2005 (53)
August, 2005 (30)
July, 2005 (63)
June, 2005 (30)
May, 2005 (59)
April, 2005 (29)
March, 2005 (74)
February, 2005 (27)
January, 2005 (22)
December, 2004 (32)
November, 2004 (42)
October, 2004 (39)
September, 2004 (20)
August, 2004 (14)
July, 2004 (27)
June, 2004 (40)
May, 2004 (5)
April, 2004 (6)
March, 2004 (16)
February, 2004 (26)
January, 2004 (23)
December, 2003 (7)
November, 2003 (14)
October, 2003 (20)
September, 2003 (4)
Post Categories



Managed Windows Shared Hosting

In this post I’ll talk about testing the user interface part of the Silverlight app. The other posts in this series are as follows (I’ll update this list as they are published):

In the previous post in this series I discussed testing the business logic. But there's another important and critical piece to your application that you can test… the user interface! This is a major piece of the application, so why not test it!

Thankfully Visual Studio 2010 has a cool new capability called Coded UI Tests. Think of these as automating a user clicking stuff, but the framework can look at specific objects on the page and perform assertions against them.

You'll need Visual Studio 2010 Feature Pack 2 which, unfortunately, is only available for MSDN subscribers. It contains a Coded UI Test editor & support for Silverlight 4 tests aside from some other things. For more info I'd recommend checking out the MSDN library Testing the User Interface with Automated UI Tests section or this hands-on-lab on MSDN: Introduction to Coded UI Tests with Visual Studio 2010 Ultimate. This is also a great page discussing coded UI tests with Silverlight on MSDN: Testing Silverlight Applications with Coded UI Tests or Action Recordings.

There are a few things you need to in order to use coded UI tests in Silverlight.

  1. Your app must be a Silverlight 4 application.
  2. You need to add a reference to the Microsoft.VisualStudio.TestTools.UITest.Extension.SilverlightUIAutomationHelper.dll assembly in your Silverlight project.
  3. Each UI control you want to test has a unique identification property (see this for more info).

After you've done that, deploy your application so your Silverlight app is running correctly. Grab the URL of the page that launched it and copy it as you'll need it in a minute.

To create coded UI tests you need to create a regular old Test project in Visual Studio. You can't reuse a Silverlight Test project so you'll likely have to create a second one. In that project, add a new item to the project of type Coded UI Test. When prompted, pick the first option. You get this little widget called the UIMap - Coded UI Test Editor. Hit the Record button… this is going to record the steps it needs to undertake to get to your Silverlight application.

When the recording starts you want to launch an instance of the browser and navigate to the page where the app is, so I use the START » RUN option and type the following: iexplore.exe "[URL OF APP GOES HERE]".

Once the page loads, click the right-most button Generate Code in the UIMap - Coded UI Test Builder widget. It will prompt you for a name of the method which I will call NavigateToAndLaunchSilverlightApp & click the Add and Generate button. This will write out all the code necessary to implement the steps you just recorded.

Now we can add an assertion for something to test. In my app I want to see if categories were loaded and bound to a ListBox. To do this, click & hold the little target button in the UIMap - Coded UI Test Builder. Move the mouse over the first item in the ListBox very carefully and make sure the blue outline only shows you have selected a list item. Now, release the mouse button and let it analyze what it found. Make sure that the ControlType property is a ListItem. If not, try it again.

Let's add an assertion that we got a category back. It isn't going to look at the actual text being rendered as that's part of the template. Instead we want to see the specific object… so scroll down in the Control Specific grouping and find the DisplayText property. This should be the fully qualified name of the type you're binding to (in my case, ProductBrowser.Model.ProductCategory). I'll right click that property and pick Add Assertion. When prompted I want to enter what I want to check for (refer to this animated gif):

After adding the assertion, we have to generate the code for this test by clicking the Generate Code button again in the widget. I call mine Assert_ProductCategoryLoadedInListBox. Finally close the widget.

If you go back to the unit test in Visual Studio you'll see our two test methods we just generated:

Now run the test and see what happens! Make sure you close all instances of the browser and then run the test. Take your hands off the keyboard and you'll see the tests automate the launching of IE, navigating to your Silverlight application and it will check for a specific instance of the product item. Finally your test will pass. Cool stuff!

Obviously you can get quite a bit more complicated with these tests.

posted on Sunday, October 30, 2011 6:33 AM

Feedback

# re: Silverlight, MVVM & SharePoint – Testing the User Interface 11/30/2011 2:49 PM Karen
Gravatar Will the Microsoft.VisualStudio.TestTools.UITest.Extension.SilverlightUIAutomationHelper.dll work with Silverlight 5 apps?

Thanks in advance!
Karen.

# re: Silverlight, MVVM & SharePoint – Testing the User Interface 11/30/2011 3:21 PM AC [MVP SharePoint]
Gravatar @Karen - I have not tried any of the SL5 beta stuff, so I can't speak for certain, but I believe so.

Post Feedback

Title:
Name:
Email:
(email will not be displayed)
Url:
Comments: 
Please add 6 and 2 and type the answer here:    
All Comments Are Filtered & Moderated
Unfortunately comment spammers are just too effecient and are constantly dirtying up blogs with irrelevant and unwanted comments trying to improve their standing on search engines. All comments on this blog are moderated. I do not censor comments, but I don't approve comments with vulger language or those soliciting products. Most of the time comments are approved within a few hours of being submitted with the only exception when I'm traveling.

Why are you asking for my email address?
The only reason I'm asking for your email address, which isn't required to submit a comment, is to provide a gravatar if you've created an account for yourself and associated your email address with a small image. If you have a gravatar created for the email address you submit, it will appear next to your comment. Otherwise nothing will appear.

What is a gravatar?
A gravatar is a "globally recognized avatar." You can get more information about gravatars, as well as create your own for free, at www.gravatar.com. You can also view my gravatar here.


Copyright © 2003 - 2012 Andrew Connell
Creative Commons License 
This work is licensed under a Creative Commons License

 
SharePoint Training
Looking for SharePoint 2010 training for developers, administrators, power users, information workers, end users & web designers? Look no further! My company, Critical Path Training offers the best SharePoint training around! We offer public & private classes both as in-person instructor-loed hands-on classes and online classes. Check out our schedule and course catalog for all the ways we can get you going on your SharePoint path!