Click here to Skip to main content
15,886,857 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  
Simulate user interactions in a Silverlight application to create automated tests
using System;
using System.Threading;
using ArtOfTest.WebAii.Core;
using NUnit.Framework;

namespace AutomatedTests
{
    public class AutomatedElement
    {
        readonly string ElementName;
        readonly string EntryPointAccessor;
        readonly AutomatedSampleApp Parent;
        public struct Point
        {
            public int X;
            public int Y;
        }


        public AutomatedElement(AutomatedSampleApp parent, string entryPointName, string elementName)
        {
            Parent = parent;
            ElementName = elementName;
            EntryPointAccessor = entryPointName + ".ElementAccessor()";
        }


        public Point GetUIElementPosition()
        {
            //get element coordinates
            var scoord = Parent.RunJScript(EntryPointAccessor + ".GetUIElementPosition(\"" + ElementName + "\")");
            Assert.AreNotEqual("", scoord, "Element coordinates not found");

            //convert to ints
            Point point;
            var split = scoord.Split(',');
            point.X = Convert.ToInt32(split[0]);
            point.Y = Convert.ToInt32(split[1]);

            return point;
        }


        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);
        }


        public string GetText()
        {
            string text = Parent.RunJScript(EntryPointAccessor + ".GetTextBoxString(\"" + ElementName + "\")");
            Assert.AreNotEqual("ELEMENT NOT FOUND", text, "Element not found");
            return text;
        }

        public string SetText(string setValue)
        {
            string text = Parent.RunJScript(EntryPointAccessor + ".SetTextBoxString(\"" + ElementName + "\",\"" + setValue + "\")");
            return text;
        }

        public string GetTextBlockString()
        {
            string text = Parent.RunJScript(EntryPointAccessor + ".GetTextBlockString(\"" + ElementName + "\")");
            Assert.AreNotEqual("ELEMENT NOT FOUND", text, "Element not found");
            return text;
        }

        public string GetCheckBoxValue()
        {
            string text = Parent.RunJScript(EntryPointAccessor + ".GetCheckBoxValue(\"" + ElementName + "\")");
            text = (text == "1") ? "True" : "False";
            return text;
        }

        public void SetCheckBoxValue(string setValue)
        {
            var currValue = GetCheckBoxValue();
            if (currValue != setValue)
                Click();
        }

        public string GetRadioButtonValue()
        {
            string text = Parent.RunJScript(EntryPointAccessor + ".GetRadioButtonValue(\"" + ElementName + "\")");
            text = (text == "1") ? "True" : "False";
            return text;
        }

        public string SetRadioButtonValue(string setValue)
        {
            string text = Parent.RunJScript(EntryPointAccessor + ".SetRadioButtonValue(\"" + ElementName + "\"," + setValue.ToLower() + ")");
            return text;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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