创建运行时文件

相关项: API 测试

备注: 必须使用 Framework 4.0 的目标框架编译 DLL。

添加 Using 语句

在运行时文件中,提供必需的 using 语句。在解决方案中,还必须添加对 .dll 文件的引用。.dll 文件位于产品安装的 /bin 文件夹中。必须始终添加对 HP.ST.Fwk.RunTimeFWK.dll 的引用。如果您正在使用内部日志记录,还必须添加对 log4net.dll 的引用。

以下示例显示 .cs 示例文件中的 Using 语句。

using HP.ST.Fwk.RunTimeFWK; 
// If you need to implement Internal Logging
using log4net;

返回页首

指定命名空间和类

定义命名空间并提供活动的运行时代码。您为自定义活动定义的类必须继承自 STActivityBase 类。例如:

namespace ReportMessageActivitySample
{
[Serializable]
public class ReportMessageActivitySample : STActivityBase
{

返回页首

设置内部日志记录

使用内置的记录器管理器指示活动在运行时期间创建内部日志。此示例获取输入属性的属性值,并将输出仅发送到运行结果或发送到运行结果和输出窗口。例如:

/// <summary> /// Internal log
/// </summary>
private static readonly ILog log = 
LogManager.GetLogger(typeof(ReportMessageActivitySample)); const string runResults = "Run Results"; const string runResultsAndOutputWindow = "Run Results and Output Window";

有关其他日志记录选项的详细信息,请参见 Assert 对象

返回页首

初始化属性

初始化您在签名文件中定义的自定义输入和输出属性。以下示例初始化三个输入属性: StatusMessageDestination。例如:

/// <summary>
/// Initializes properties.
/// </summary>
/// <param name="ctx">The runtime context</param>
public ReportMessageActivitySample(ISTRunTimeContext ctx, string name)
: base(ctx, name)
{
this.Status = String.Empty; 
this.Message = String.Empty; 
this.Destination = String.Empty; 
}

返回页首

检索属性值

此部分检索或设置输入属性值。例如:

/// <summary>
/// Gets or sets the status of the message to report.
/// </summary>
public string Status { get; set; }
/// <summary>
/// Gets or sets the details of the message to report.
/// </summary>
public string Message { get; set; }
/// <summary>
/// Gets or sets the destination where the message should be reported to.
/// </summary>
public string Destination { get; set; }

如果您具有的数组类型属性不是架构所描述的(例如键/值对),则必须显式地初始化数组的所有成员并指示实际元素数。

以下示例初始化 MyArrayName 属性的 40 个元素。它包含 40 个键和值对。

this.MyArrayName = new MyPair[40]; 
for (int i=0; i<40; i++)
{
this.MyArrayName [i] = new MyPair(); 
}
public MyPair[] MyArrayName; 
public class MyPair
{
string Key; 
string Value; 
}

对于由架构或 WSDL 定义的数组,您可以使用标准的选择链接源对话框 (API 测试)并直接链接到数组元素。

返回页首

定义事件

定义一个或多个您稍后将调用的自定义事件。例如:

public event EventHandler CustomerEvent; 
private void InvokeCustomerEvent(EventArgs MyArg)
{
EventHandler handler = this.CustomerEvent; 
if (handler != null)
{
handler(this, MyArg); 
}
}

返回页首

执行步骤

执行步骤并向日志发送运行时信息。使用在 STActivityBase 类中定义的 STExecutionResult 数据类型及其 ExecuteStep 函数。例如:

protected override STExecutionResult ExecuteStep()
{
string DetailsReport; 
if (this.Destination == runResultsAndOutputWindow)
{
LogInfo("\n" + this.Message.Replace("\\n", "\n")); 
}
/// <summary>
/// Reports message to test results and output window.
/// </summary>
// The line-breaks replacements allow the printing of multiple lines in the report
	DetailsReport = this.Message; 
	DetailsReport = DetailsReport.Replace("\\n", "<BR>"); 	
	DetailsReport = DetailsReport.Replace("\n", "<BR>"); 
	this.Report("Message", DetailsReport);

如果您定义了一个自定义事件,请在调用 ExecuteStep 后调用该事件。

...
protected override STExecutionResult ExecuteStep()
{
InvokeCustomerEvent(); 
}

返回页首

设置状态

设置测试运行的状态。ReportMessageActivitySample.cs 示例文件基于 STExecutionResult 值,使用枚举方法设置状态。例如:

switch (this.Status)
{
case "Done": 
this.Report(ReportKeywords.StatusKeywordTag, ReportKeywords.DoneValueTag); 
return new STExecutionResult(STExecutionStatus.Success); 
case "Pass": 
this.Report(ReportKeywords.StatusKeywordTag, ReportKeywords.SuccessValueTag); 
return new STExecutionResult(STExecutionStatus.Success); 
case "Fail": 
this.Report(ReportKeywords.StatusKeywordTag, ReportKeywords.FailureValueTag); 
return new STExecutionResult(STExecutionStatus.Success); 
default: 
return new STExecutionResult(STExecutionStatus.Success); 
}

返回页首

编译运行时文件

自定义代码后,可以编译 .dll.dll 名称应与插件文件的名称相同。例如,运行时文件 ReportMessageActivitySample.dll 对应于 ReportMessageActivitySample.addin 文件。

在开发环境中创建运行时文件之后,从签名文件引用 .dll,从插件文件引用签名文件。

返回页首