Click here to Skip to main content
15,883,841 members
Articles / Desktop Programming / WPF

Automated Testing in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
6 Apr 2009CPOL2 min read 42.2K   772   15   2
Simulate user interactions in a Silverlight application to create automated tests

Introduction

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. 

Background 

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.

AutomatedSampleApplication

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

AutomatedElement

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.

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

            //click
            Global.BlockInput(true);
            Parent.myManager.ActiveBrowser.Window.SetActive();
            Parent.myManager.ActiveBrowser.Window.SetFocus();
            Thread.Sleep(100);
            Parent.myManager.Desktop.Mouse.Click
		(MouseClickType.LeftClick, elementPoint.X, elementPoint.Y);
            Global.BlockInput(false);
        }

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

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

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

ScriptEntryPoint

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.

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

C#
public string GetUIElementPosition(string ElementName)
{
	var point = GetUIElementPositionPoint(ElementName);
	if (point.X == 0 && point.Y == 0)
		return "ELEMENT NOT FOUND";

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

Tests

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

C#
[Test]
public void SubmitTest()
{
    //Edit text box and click submit
    SampleApp.mainPage.Submit.Click();
    SampleApp.sideView.NameEdit.SetText("Hello from Automated UI Testing!!");
    SampleApp.sideView.SubmitButton.Click();
    Assert.AreEqual("Hello from Automated UI Testing!!", 
          SampleApp.sideView.TextOut.GetTextBlockString(), "Incorrect output text");
}

History

  • 6th April, 2009: Initial post

License

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


Written By
Tester / Quality Assurance
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAutomation testing of 3rd party application. Pin
Rajeev from Patna24-Aug-12 5:31
Rajeev from Patna24-Aug-12 5:31 
Dear Bernhard,

I saw your article and it looks very good for automation testing but I have one question. Can we perform similar automation testing of Silverlight pages when you don't know any class name and control name? I mean that if you are going to perform automation testing of any 3rd party application. You have use application class name and controls name for testing so how we can perform such testing without knowing application class name and control name.

Thanks & Regards,
Rajeev
QuestionHow to run this project Pin
dave424020-Sep-09 7:11
dave424020-Sep-09 7: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.