Silverlight 中的自动化测试






4.33/5 (3投票s)
模拟 Silverlight 应用程序中的用户交互以创建自动化测试
引言
本文中的代码有助于使用 Silverlight 应用程序进行进程外 UI 自动化。模拟鼠标和键盘命令以创建基于广泛场景的自动化测试。
背景
2008 年 9 月,我受命为 Silverlight 应用程序编写自动化 UI 测试。客户端正在使用复杂且功能齐全的企业应用程序将这项不成熟的技术推向极限。我是在项目后期加入的,在完成这项工作方面,现有工具的选择很少。
事实证明,Microsoft 的 UI 自动化框架在与该应用程序一起使用时不可靠。这反映在“UI Spy”通常无法找到之前能够找到的 UIElements,或者即使它们被找到也无法处理第三方 Telerik 控件。“White”项目使用 UI 自动化框架,因此也存在同样的问题。
Art of test 通过其出色的 Internet Explorer 自动化工具 WebAii 支持 Silverlight。到目前为止,没有任何版本可以与我需要测试的应用程序一起使用。我确信将来这些问题会得到解决,但在那之前,这些是我用来允许自动化测试继续进行的方法。
使用代码
最终目标是任何编写的自动化测试都易于非技术测试人员理解,并且完全抽象出任何底层实现。
AutomatedSampleApplication
此类表示正在测试的 Silverlight 应用程序,并且是与应用程序进行任何自动化交互的入口点。
AutomatedElement
此类型的对象用于表示任何 UI 元素,并提供可以对其执行的 UI 交互。执行 UI 任务所需的信息通过 JavaScript 从应用程序本身请求。
例如,Click
函数请求应用程序滚动到该元素,检索其屏幕坐标,然后使用 WebAii 来确保窗口具有焦点并将单击事件发送到屏幕位置。
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);
}
使用 HTML 页面中的 onLoad
事件,为 RunJScript
函数获取对 Silverlight 插件的引用:
var plugin = null;
function pluginLoaded(sender, args) {
plugin = sender.getHost();
}
<param name="onLoad" value="pluginLoaded" />
ScriptEntryPoint
在 Silverlight 应用程序中,ScriptEntryPoint
类注册表示应用程序中不同视图的入口点对象。这充当 AutomatedElement
和 ElementAccessor
之间的桥梁。
ElementAccessor
此类实际上实现了从 AutomatedElement
类请求的操作。
部分代码通过 AutomatedElement
中的 Click
函数在 Silverlight 应用程序中运行:
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);
}
测试
生成的测试清楚地表明了我们将 UI 操作发送到应用程序的哪个部分,并且相对容易理解。
[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");
}
历史
- 2009 年 4 月 6 日:首次发布