微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用TestComplete进行Silverlight的单元测试

Silverlight支持ScriptableType ScriptableMember属性,可以让类和成员被外部访问。因此可以用TestComplete进行调用

 

AutomatedQA网站上的这篇文章介绍了具体的测试方法

http://www.automatedqa.com/techpapers/testcomplete/automated-unit-testing-of-silverlight-applications/

 

单元测试还是在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(""," Title is not correct");
    CheckEquals("John"," First Name is not correct");
    CheckEquals(""," Middle Name is not correct");
    CheckEquals("brown"," Last Name is not correct");
    CheckEquals(""," Suffix is not correct");

    nameExtractor.FullName = "Mr.    John brown";
    CheckEquals("Mr"," Suffix is not correct");

    nameExtractor.FullName = "John brown,PhD";
    CheckEquals("John"," First Name is not correct");
    CheckEquals("brown"," Last Name is not correct");
    CheckEquals("PhD"," Suffix is not correct");
}

 

 

为了让这些单元测试代码能够被TC调用,需要做一些额外的准备:

private void Application_Startup(object sender,StartupEventArgs e)
{
    this.RootVisual = new MainPage();
    HtmlPage.RegisterScriptableObject("unitTestDriver",new UnitTestDriver());
}

 

添加ScriptableTypeScriptaleMember属性

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐