This second article in my series on Silverlight Testing with WebAii UI Automation Fx assumes you have read the first article and installed the tools it listed. In this article I am going to talk you through the steps of setting up your project and getting the WebAii Fx to load the target application into a browser. In the interest of keeping articles short I will be assuming you are familiar with Visual Studio and C#. All of my samples are in C# as this is my preferred .NET language, I am sure there are VB.NET fans who will want to try this out too, but due to time constraints I am going to stick to one language, although I will do my best to answer any VB.NET related questions.
Setting up the project
To get started complete the following tasks with Visual Studio
- Create a new Class Library project called WebAii.Samples
- Why not a test project? Test projects in VS are designed for MS Test and add references that are of no use to us.
- Add a reference to nunit.framework
- NUnit Framework is registered in the GAC so you should just be able to select it from the .NET tab
- Add a reference to nunit.core.dll
- NUnit Core is not registered in the GAC so you will need to use the Browse tab
- The default location is C:\Program Files\NUnit 2.5.7\bin\net-2.0\lib
- Add a reference to the WebAii UI Automation Framework
- WebAii does not get registered in the GAC during install so you will need to use the Browse tab.
- The default install location is C:\Program Files\Telerik\WebAii Testing Framework 2010.2\Bin
- Include the following assemblies
- Delete the default Class1.cs file so we are starting from scratch
You should now have a solution that looks something like this in Solution Explorer:
Creating a base class for tests
I only ended up with my base class after creating a few tests and re-factoring once I realised I would be repeating some of this code for every test, but I am going with the base class from the start because I know you will go the same route very quickly. Incidentally when you install WebAii new Item templates are registered with Visual Studio for several of the common testing frameworks, but these are all targeted at testing HTML pages rather than Silverlight, and although it doesn’t take long to convert them I prefer to start with a clean slate and explain everything from scratch. So here we go:
- Add a new class called TestBase to the project
- Edit the file that is opened to look like this as a nice clean starting point
1: using System;
3: namespace WebAii.Samples
5: public class TestBase
WebAii provides a base class that all of your test classes will need to inherit from to access the core features required to hook into the framework and automate UI elements in your application.
- Add : BaseTest to the end of your class declaration. You will need a using statement to import the ArtOfTest.WebAii.TestTemplates namespace, but hopefully if you are not using something like CodeRush or Resharper you will have at least discovered Ctrl+. for accessing smart tags
- While you are at it edit the declaration to make TestBase abstract so it can’t be instantiated by mistake.
Next we need to set our base class up to use NUnit:
- Decorate the class decoration with the TestFixture attribute. You will need a using to import the NUnit.Framework namespace but again hopefully your tools will help make this easy.
- Add a public void method named TestBaseSetUp within the body of the class
- Decorate the new method with the NUnit SetUp attribute
- Add a public void method named TestBaseTearDown below the previous one
- Decorate the new method with the NUnit TearDown attribute
- Add a public void method named TestBaseFixtureTearDown below the previous one
- Decorate the new method with the NUnit TestFixtureTearDown attribute
Your TestBase class should now look something like this:
1: using System;
2: using ArtOfTest.WebAii.TestTemplates;
3: using NUnit.Framework;
5: namespace WebAii.Samples
8: public abstract class TestBase: BaseTest
11: public void TestBaseSetup()
16: public void TestBaseTearDown()
21: public void TestBaseFixtureTearDown()
Now we have a class that will be recognised by the NUnit framework as a test class courtesy of the TestFixture attribute.
We have a method that will be run before each test method in any class that derives from our base class courtesy of the SetUp attribute. A neat feature of NUnit 2.5 or later is support for multiple setup and tear down methods without resorting to virtual methods and overrides. Check it out here
We have a method that will be run after each test method in any class that derives from our base class courtesy of the TearDown attribute.
Finally we have a method that will be run after all test methods in any class that derives from our base class have been executed courtesy of the TestFixtureTearDown attribute.
Our two tear down methods are simply going to delegate to methods of the WebAii base class so let’s get them out of the way.
Edit the bodies of the TestBaseTearDown and TestBaseFixtureTearDown methods to look like this:
3: public void TestBaseTearDown()
9: public void TestBaseFixtureTearDown()
With these changes our TestBaseTearDown method now delegates to an instance method of the WebAii base class to shutdown the WebAii Manager and close any browser windows that were launched by a test method. Our TestBaseFixtureTearDown delegates to a static method of the WebAii base class to shut down all browser windows opened by test methods if the recycleBrowser setting is enabled (more about this setting later).
Now we can focus on the setup method that will initialise and configure our tests and launch our test application ready for what ever test scenario we are running.
The first thing any WebAii test needs to do is intialise the framework, which is done by calling the Initialize instance method of BaseTest. There are nine overloads of this method most of which are designed to support configuration based intialisation via a single statement. However for now we are going to stick to code and none of these overloads provides a simple option to enable a key feature for us, that is support for Silverlight. So we are left with two overloads that allow us to pass a Settings object.
An additional factor in our case is the use of NUnit. We want to ensure that output from assertions and errors is output for NUnit to display so for now we are reduced to a single useful overload of Initialize that has the following signature:
void Initialize(Settings settings, TestContextWriteLine vsWriteLineDelegate)
We’ll cover the Settings object in brief shortly and my next article in this series will cover the entire set of possible settings and how they affect your tests. It will also cover the use of a .config file to set defaults for these settings.
For the TestContextWriteLine parameter we are going to pass a delagate to the NUnit.Core.TestContext.Out.WriteLine method, which ensurs output is sent to both WebAii and NUnit targets.
So on to the settings. There are many settings that control the WebAii framework and these can all be set in code via an instance of the Settings class or via a config file. As with many things in .NET the recommended approach is to set the defaults via configuration and override these when necessary in code. Before you can change any settings you must get a reference to an instance of the Settings class. Whilst you can new one of these up with a choice of constructors the recommend practice is to call the static GetSettings method of the WebAii base class. This ensures your Settings object has already been configured with values from any .config file ready for you to override.
Once you have have your Settings object you can set properties as appropriate then pass it to Initialize.
Add the following code to the body of the TestBaseSetUp method:
1: var settings = GetSettings();
2: settings.EnableSilverlight = true;
4: this.Initialize(settings, NUnit.Core.TestContext.Out.WriteLine);
Note: I am using the method group reference to NUnit.Core.TestContext.Out.WriteLine because I find it more readable but feel free to use the explicit equivalent new TestContextWriteLine(NUnit.Core.TestContext.Out.WriteLine);
Our base class is almost ready, all we need to do now is get it to launch our target Silverlight application and hook into it ready for our test scenario. As this article is being written WebAii supports three actual browsers Internet Explorer, FireFox and Safari for now we are just going to use Internet Explorer but when we look at using .config files you will see that we can actually set the browser to use in configuration so the same tests can simply target a different browser.
When we inherit from the WebAii BaseTest we have a Manager instance as a property, which provides a number of utility objects and methods one of which is the LaunchBrowser method. LaunchBrowser provides seven overloads in total that allow you to pass all or some of the following:
- browserToLaunch – a value from the BrowserType enum that indicates the browser to be launched
- waitForBrowserToConnect – a boolean flag indicating whether the method should block until the browser is loaded and the relevant browser helper is connected and ready to go. The default is true so this parameter need only be set if you don’t want to wait for the connection. For Silverlight you always want to wait.
- windowStyle – a value from the System.Diagnostics.ProcessWindowStyle that controls whether the browser window is Hidden, Minimized, Maximized or Normal on opening. The default is Normal.
- arguments – a string value that is only used when testing Silverlight applications
We are going accept the defaults for everything except the browser at this point so add the following statement following the Initialize call
This will launch a new instance of Internet Explorer with a blank page loaded and populate the ActiveBrowser property of the test class, which allows us to access and control the new browser.
If all or most of our test methods were going to start from a different address such as might be the case in a web application our test base class would be done for now. However as will be typical for a Silverlight application all of our test methods will need to start from the same point so we are going to build this into our TestBase
To get the browser to load our application we need to call the NavigateTo method of the ActiveBrowser object. There are two overloads of NavigateTo, both take a single argument representing the address to navigate to. The first overload takes a string the second a Uri instance.
Navigating to the web page hosting the Silverlight application will load the page and application but before we do anything useful we need a reference to an instance of the WebAii SilverlightApp class. However it is possible to embed multiple Silverlight applications in a web page and these are made available as a specialised collection of SilverlightApp instances via the SilverlightApps extension method of the Browser type, which returns a an instance of SilverlightAppsList. We will be referencing the SilverlightApp instance for our application several times so we are going to capture it into a property of base class so that is it is readily available. Add the following to the top of the class implementation:
1: protected internal SilverlightApp App
4: private set;
You will need a using to import the ArtOfTest.WebAii.Silverlight namespace.
Finally to complete our base class add the following to the end of the TestBaseSetup method:
3: this.App = ActiveBrowser.SilverlightApps();
The complete TestBase class should now look like this:
1: using System;
2: using ArtOfTest.WebAii.Core;
3: using ArtOfTest.WebAii.Silverlight;
4: using ArtOfTest.WebAii.TestTemplates;
5: using NUnit.Framework;
7: namespace WebAii.Samples
10: public abstract class TestBase: BaseTest
12: protected internal SilverlightApp App
15: private set;
19: public void TestBaseSetup()
21: var settings = GetSettings();
22: settings.EnableSilverlight = true;
32: this.App = ActiveBrowser.SilverlightApps();
36: public void TestBaseTearDown()
42: public void TestBaseFixtureTearDown()
Testing the base class
To round of this article we will create a simple test that uses our TestBase class. We will simple test method that asserts our Silverlight application is available to keep things simple and show that our base class works.
Add a new class to the project, the name is not important at this stage but I called mine TestBaseTests. Edit the class so that it contains the following:
1: using System;
2: using NUnit.Framework;
4: namespace WebAii.Samples
7: public class TestBaseTests: TestBase
10: public void The_base_loads_application()
With this in place you can use your preferred NUnit test runner if you are new to this then this article
will help you with the NUnit GUI. What you should see is a new browser window open and after a short time the MSCUI Primary Care demonstrator application should load, fairly quickly the browser window will close and you should see the test result indicate a pass.
We are not ready to start testing, but before we do my next article will cover the the various settings and configuration options offered by WebAii.