Click here to Skip to main content
13,860,769 members
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 6 Apr 2009
Licenced CPOL

Automated Testing in Silverlight

, 6 Apr 2009
Rate this:
Please Sign up or sign in to vote.
Simulate user interactions in a Silverlight application to create automated tests


The code in this article facilitates out of process UI automation with Silverlight applications. Mouse and Keyboard commands are simulated to create broad scenario based automated tests. 


In September 2008, I was tasked with writing automated UI tests for a Silverlight application. The client was pushing the immature technology to its limits with a complex and fully featured enterprise application. I was brought into the project late and had a slim choice in terms of existing tools to get the job done.

Microsoft’s UI Automation framework proved to be unreliable when used with the application. This was reflected by ‘UI Spy’ often not finding UIElements it had previously been able to locate or if they were located it couldn't deal with the 3rd party Telerik controls. The ‘White’ project uses the UI Automation framework and therefore had the same issues.

Art of test supports Silverlight with its excellent Internet Explorer automation tool WebAii. To date, none of the releases have worked with the application I needed to test. I'm sure in the future these problems will be solved but until then these are the techniques I used to allow automated testing to go ahead with the project in question.

Using the Code 

The ultimate aim was that any automated tests written would be easy to follow for non-technical testers and completely abstract any of the underlying implementation.


This class represents the Silverlight application being tested and is the entry point for any automated interaction with the application.


Objects of this type are used to represent any UI Element and provide UI interactions that may be performed against them. The information required to perform the UI tasks is requested from the application itself via JavaScript.

For example, the Click function requests that the application scroll to the element, retrieve its screen co-ordinates and then uses WebAii to ensure the window has focus and sends the click event to the screen position.

public void Click()
            Parent.RunJScript(EntryPointAccessor + ".ScrollToElement
	   (\"" + ElementName + "\")");  //ensure element is on screen
            var elementPoint = GetUIElementPosition();  //get element coordinates

		(MouseClickType.LeftClick, elementPoint.X, elementPoint.Y);

A reference to the Silverlight plug-in is obtained for the RunJScript function using the onLoad event in the HTML page:  

var plugin = null;
function pluginLoaded(sender, args) {
   plugin = sender.getHost();

<param name="onLoad" value="pluginLoaded" /> 


Within the Silverlight application, the ScriptEntryPoint class registers entry point objects that represent the different views in an application. This serves as a bridge between AutomatedElement and ElementAccessor.


This class actually implements the actions that have been requested from the AutomatedElement class.

Some of the code runs within the Silverlight application by the Click function in AutomatedElement

public string GetUIElementPosition(string ElementName)
	var point = GetUIElementPositionPoint(ElementName);
	if (point.X == 0 && point.Y == 0)

	return string.Format("{0},{1}", point.X, point.Y);


The resulting tests make it obvious what part of the application we are sending UI actions to and are relatively easy to understand.

public void SubmitTest()
    //Edit text box and click submit
    SampleApp.sideView.NameEdit.SetText("Hello from Automated UI Testing!!");
    Assert.AreEqual("Hello from Automated UI Testing!!", 
          SampleApp.sideView.TextOut.GetTextBlockString(), "Incorrect output text");


  • 6th April, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bernhard Deininger
Tester / Quality Assurance
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionAutomation testing of 3rd party application. Pin
Rajeev from Patna24-Aug-12 6:31
memberRajeev from Patna24-Aug-12 6:31 
QuestionHow to run this project Pin
dave424020-Sep-09 8:11
memberdave424020-Sep-09 8:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190214.1 | Last Updated 6 Apr 2009
Article Copyright 2009 by Bernhard Deininger
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid