Getting Started

Bootstrapping Silverlight is non-trivial (as far as I can tell, let me know if I missed something really simple), have a read of How it works to understand the problems around running coded UI Tests against a silverlight application (locally, as well as on a build server)

1. Run in NuGet Console: Install-Package MSTestContrib.UITesting.Silverlight
2. Add Reference to the Web Project that hosts your Silverlight Application from your Test Project
3. Set the Copy to Output Directory option on ClientBin\MyApplication.xap file to Copy Always
4. Create a new class in your test project called <<MyProject>>Application.cs (for example SilverlightTodoApplication) and make it inherit from SilverlightApplicationBase. Then implement the abstract method MainPageTypeName, returning the name of the type of the initial silverlight page.
public class SilverlightTodoApplication : SilverlightApplicationBase
    public override string MainPageTypeName
        get { return "MainPage"; }

5. Create another class called CodedUITestBase, it should look something like (the generic type argument is the previous class you just created)
public class CodedUITestBase : SilverlightCodedUITestBase<SilverlightTodoApplication>
    protected MainScreen MainScreen()
        return new MainScreen(Application, Application.MainPage);
6. Write some coded UI Tests!

Example Test

    public class CanAddTaskTest : CodedUITestBase
        public void CodedUITestMethod1()
            var mainScreen = MainScreen();
            var newTaskScreen = mainScreen.NewTask();

            const string title = "Write some tests";
            newTaskScreen.Title = title;
            newTaskScreen.Description = "for MSTestContrib";
            newTaskScreen.DueDate = DateTime.Now.AddDays(3);


            var tasks = mainScreen.Tasks.ToList();
            Assert.AreEqual(1, tasks.Count);
            Assert.AreEqual(title, tasks[0].Title);

How it works

As far as I know, coded UI for silverlight assumes you have deployed your silverlight application to some URL, then you run your tests against that. I wanted a simpler solution which gives a similar experience to testing WPF where MSTestContrib.UITesting will take care of launching the app and cleaning it up after.

There is a fair bit going on behind the scenes to make this happen, so the small amount of setup above is't too bad I don't think.
  1. Test Project is build, which builds web project, Xap is copied into the bin\ClientBin folder of the test project
  2. *.xap files are included as a deployment item on the test base class, which means the .xap file is copied into the current test run's folder
  3. A Default.aspx and Silverlight.js are also copied as deployment items
  4. The test baseclass has a static constructor, which starts up a very lightweight web server inside the test process (listening on http://localhost:9999), this web server is pointing at the current test's directory (now with your *.xap, and our Default.aspx and Silverlight.js).
  5. The static ctor of the test base class then fires up the browser, navigates to the default.aspx page, which is hosted inside the test process, then proceeds to get a reference to the page, then the silverlight control that matches the type name specified in your <YourProject>Application class.
  6. The silverlight control which is the main control of your app is now available for use in all tests. Each test will open the browser, run the test, then close the browser. When the test run is complete, the webserver will be shutdown.

Last edited Aug 16, 2011 at 7:18 AM by JakeGinnivan, version 3


No comments yet.