Silverlight支持ScriptableType 和 ScriptableMember属性,可以让类和成员被外部访问。因此可以用TestComplete进行调用。
AutomatedQA网站上的这篇文章介绍了具体的测试方法:
单元测试还是在Silverlight程序中创建的,例如:
namespace NameExtractor
{
public class UnitTestDriver
{
//an object of the tested class NameExtractor
private NameExtractor nameExtractor;
public UnitTestDriver()
{
nameExtractor = new NameExtractor();
}
private void CheckEquals(String Expected, String Actual, String Msg)
{
if (Expected != Actual)
{
String exceptionMsg = String.Format("{0} : expected {1}, but was: {2}", Msg, Expected, Actual);
Exception exception = new Exception(exceptionMsg);
throw exception;
}
}
}
}
//tests
public void Test1()
{
nameExtractor.FullName = "Mr John Greench Brown, PhD";
CheckEquals("Mr", nameExtractor.Title, "Title is not correct");
CheckEquals("John", nameExtractor.FirstName, "First Name is not correct");
CheckEquals("Greench", nameExtractor.MiddleName, "Middle Name is not correct");
CheckEquals("Brown", nameExtractor.LastName, "Last Name is not correct");
CheckEquals("PhD", nameExtractor.Suffix, "Suffix is not correct");
}
public void Test2()
{
nameExtractor.FullName = "John Brown";
CheckEquals("", nameExtractor.Title, " Title is not correct");
CheckEquals("John", nameExtractor.FirstName, " First Name is not correct");
CheckEquals("", nameExtractor.MiddleName, " Middle Name is not correct");
CheckEquals("Brown", nameExtractor.LastName, " Last Name is not correct");
CheckEquals("", nameExtractor.Suffix, " Suffix is not correct");
nameExtractor.FullName = "Mr. John Brown";
CheckEquals("Mr", nameExtractor.Title, " Title is not correct");
CheckEquals("John", nameExtractor.FirstName, " First Name is not correct");
CheckEquals("", nameExtractor.MiddleName, " Middle Name is not correct");
CheckEquals("Brown", nameExtractor.LastName, " Last Name is not correct");
CheckEquals("", nameExtractor.Suffix, " Suffix is not correct");
nameExtractor.FullName = "John Brown, PhD";
CheckEquals("John", nameExtractor.FirstName, " First Name is not correct");
CheckEquals("Brown", nameExtractor.LastName, " Last Name is not correct");
CheckEquals("PhD", nameExtractor.Suffix, " Suffix is not correct");
}
为了让这些单元测试代码能够被TC所调用,需要做一些额外的准备:
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
HtmlPage.RegisterScriptableObject("unitTestDriver", new UnitTestDriver());
}
添加ScriptableType和ScriptaleMember属性:
using namespace System.Windows.Browser;
namespace NameExtractor
{
[ScriptableType] //Provides scriptable access to the entire class
public class UnitTestDriver
{
…
}
}
using namespace System.Windows.Browser;
namespace NameExtractor
{
public class UnitTestDriver
{
…
//tests
[ScriptableMember] //Provides scriptable access to Test1
public void Test1()
{
…
}
[ScriptableMember] //Provides scriptable access to Test2
public void Test2()
{
…
}
}
}
接下来就可以在TC的脚本中调用Silverlight的单元测试代码:
// JScript
function GetSlObject()
{
return Sys.Process("IEXPLORE", 2).Page("file:///C:/MyProjects/NameExtractor/Bin/Debug/TestPage.html").Form("form1").Panel("silverlightControlHost").Object(0);
}
function Main()
{
var slObject = GetSlObject();
var unitTestDriver = slObject.Content.unitTestDriver;
Test1(unitTestDriver);
Test2(unitTestDriver);
}
function Test1(unitTestDriver)
{
try
{
unitTestDriver.Test1();
Log.Message("Test1 passed.");
}
catch(ex)
{
Log.Error("Test1 failed.", ex.description);
}
}
function Test2(unitTestDriver)
{
try
{
unitTestDriver.Test2();
Log.Message("Test2 passed.");
}
catch(ex)
{
Log.Error("Test2 failed.", ex.description);
}
}