Summary
In this article you’ll get a glimpse of how the UltiStudio development team is tackleing the functionality tests for the product. If you are a Silverlight developer or are familiar with Silverlight you know that testing Silverlight applications for their functionality is not a trial task. This article will expose you to a young framework developed by the Art of Test people. Its name is WebAii.
Requirements
You should have the latest Visual Studio installed. MbUnit and TestDriven.Net should also be installed. Additionally, you should know how to create a Test Project and how to add Test Fixtures to it.
Who will benefit from this Article?
Anyone that works with Silverlight and is desperately looking for a way of functionally tests their applications.
WebAii, Here we go! ( with Super Mario voice :p )
This framework is currently Beta for Silverlight applications, young as I said earlier, therefore if you are planning to use it be patient and hang on. Let’s jump right into the tests. You will need the following files, download them to a preferred location and be ready to add their assemblies to your test project in Visual Studio.
• MbUnit
Once you have located the DLLs and assuming that you have Visual Studio, MbUnit and TestDrive.NET installed, go ahead and create a "MbUnit Test Project" in Visual Studio. Next, add a new TextFixture to your project and you are now ready to write some tests.
Setting up the Test Fixture
Once we've added the Fixture we should have something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Editor.FunctionalTests { class SilverlightFunctionalTests { } } |
Now, add the MbUnit attribute [TestFixture] to the class, (don’t forget to make it public). Additionally, add a SetUp method. The code should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using MbUnit.Framework; namespace Editor.FunctionalTests { [TestFixture] public class SilverlightFunctionalTests { [TestFixtureSetUp] public void SetUp() { } } } |
Great! that was easy and I’m sure most of you are did this even before you read this. Next, we will add the WebAii to the Fixture. For the purpose of this example I will use a random URL, make sure you write yours correctly and that the URL you point to has the Silverlight application you are trying to test. I have added most of the WebAii set up within the SetUp method I just created, however feel free to put this code where you think it’s better. Go ahead and add the following code and then we’ll go over it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | using MbUnit.Framework; using ArtOfTest.WebAii.Core; using ArtOfTest.WebAii.TestTemplates; using ArtOfTest.WebAii.Silverlight; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Editor.FunctionalTests { [TestFixture] public class SilverlightFunctionalTests : BaseTest { private SilverlightApp _app; [TestFixtureSetUp] public void SetUp() { Settings settings = GetSettings(); settings.DefaultBrowser = BrowserType.InternetExplorer; settings.EnableSilverlight = true; Initialize(settings); Manager.LaunchNewBrowser(); ActiveBrowser.NavigateTo("URL"); SilverlightAppsList apps = ActiveBrowser.SilverlightApps(); _app = apps.ElementAt(0); _app.Connect(); } } } |
public class SilverlightFunctionalTests : BaseTest
private SilverlightApp _app;
…
Settings settings = GetSettings();
settings.DefaultBrowser = BrowserType.InternetExplorer;
settings.EnableSilverlight = true;
Initialize(settings);
…
The first thing you’ll notice is that I’ve added WebAii objects (Green) . Most of this is set up and you will have to copy this code as is. Here, I’ve added inheritance to the BaseTest class, created a SilverlightApp object and initialized the settings by setting the a default browser and enabling silverlight.
Manager.LaunchNewBrowser();
ActiveBrowser.NavigateTo(“URL”);
SilverlightAppsList apps = ActiveBrowser.SilverlightApps();
_app = apps.ElementAt(0);
_app.Connect();
The next thing I wrote (Orange) will launch the browser we set in the settings and it will make it navigate to the URL that we have specified. Then the code will tell the browser to find all the Silverlight applications in the page and return a SilverlightAppsList (list of Silverlight applications). For the purpose of this example we only have one application in the page, hence why I get the element at position 0 within the list. Finally, I set the global SilverlightApp and immediately after, I called Connect() on the SilverlightApp.
At this point we are ready to start writing tests.
Writing Tests, finally!
Here is the fun stuff. I will show you a test that does a simple thing, it will click on the Silverlight application. In this section of this article I will try to give you a very broad understanding of what you should do in order to write your own tests. The framework does offer several methods that will allow you to be creative with your tests. I’ll point some of the most used ones and then I’ll give you a link for you to digest the rest.
To the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [Test] public void ClickingSilverlightApplication_ShouldClickTheApplication_Test() { //Preconditions FrameworkElement control = _app.FindName("PopupBorder1"); control.Wait.ForVisible(); //Actions control.User.Click(); //Postconditions Assert.isTrue(true); } |
//Preconditions
FrameworkElement control = _app.FindName(“PopupBorder1″);
control.Wait.ForVisible();
Fair enough, we have our precious test. It looks easy too\! As you can see, it works exactly as any unit test framework would. Allow me to explain it first. Initially (Green), I use the WebAii framework FindName method. There are a couple of things to say about this. FindName is a method that will look within the XAML of your silverlight application and it will try to find a XAML node with the Name attribute equal to the string you are passing to the method. The other thing I should say is that you can also call FindName like this: FindName<T>(<string>) where T is a specific type; I will show an example later on.
Keep in mind you have to know the Name attribute to find objects in the silverlight application. If the object is not found your test will time out and fail, so you will know. If it doesn’t then you’ll end up with a FrameworkElement or T if you used FindName<T>(<string>). As in the previous code, call Wait.ForVisible(). This will allow our test and the silverlight application to wait for each other.
control.User.Click();
The next piece of code (Orange) used the User property to call the action: Click(), simply to click on the found object. In this Actions section of the test you will usually be calling .User.<Something>, there are a lot of things that WebAii can do to mock the user functionality, here are some included on the version released at the moment I’m writing this article:
1 2 3 4 5 6 7 8 9 10 11 | control.User.DragTo(...) control.User.HoverOver(...) control.User.KeyDown(...) control.User.KeyPress(...) control.User.MouseEnter(...) control.User.TypeText(...) |
You can get all of them by using the IntelliSense feature in Visual Studio. Their documentation is pretty good too.
As promised, here is the example of the FindName<T> overload.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [Test] public void FindEllipse_FindEllipseInApplication_Test() { //Preconditions Ellipse control = app.FindName<Ellipse>("SomeEllipse"); control.Wait.ForVisible(); //Actions //someaction //Postconditions //Asserts } |
Word of advise: The returned Ellipse is not a Silverlight control, is a WebAii control which means they are different types of objects. This means you will need to be creative in order to assert the different properties of the object.
Conclusion
By now you should have a broad understanding of how the WebAii framework works for Silverlight and what it’s needed to create tests based on this framework. Additionally, you should be aware that there are many options when it comes to mocking the User behavior and that in order to find objects in your application and interact with them you will need their XAML Name attribute. If you have any questions or get stuck anywhere while testing with WebAii don’t hesitate to submit a comment or write me an email to my Ultimate Email
If you want to learn more about WebAii and Silverlight head over to WebAii and Silverlight Documentation
Categories: Programming, Technology
Tags: Integration Tests, Microsoft, Silverlight, Technology, Testing, Unit Testing, WebAii
Comments: No Comments.

