自动化测试平台TestComplete使用教程:从.NET程序集调用函数(下)
TestComplete是一款具有人工智能的自动UI测试工具,利用自动化测试工具和人工智能支持的混合对象识别引擎,轻松检测和测试每个桌面,Web和移动应用程序。使用TestComplete,可以提高测试覆盖率并帮助提供经过实战考验的高质量软件。
在TestComplete中,您可以从脚本中调用驻留在任何.NET程序集中的例程。该程序集可以与.NET Framework或第三方.NET应用程序一起提供。该教程内容较多,分为上下两篇文章,本文是下半部分内容,紧接前文内容~
使用可选参数调用.NET例程
从测试中调用.NET函数时,TestComplete不允许省略可选参数。也就是说,您必须在调用中指定所有参数,包括可选参数。
这意味着,如果要使用默认参数值,则必须在调用中显式指定它们。对此的部分解决方法是dotNET.System.Type.Missing作为可选参数值传递。例如,如果您有一种通过以下方式声明的方法-
C#
namespace TestNamespace
{
static public class MyTestClass
{
// This method takes two parameters.
// The Param1 parameter is required, the Param2 parameter is optional.
static public void Method1(int param1, string param2 = "")
{
…
}
}
}
—然后从TestComplete脚本中调用它,可以使用以下代码:
Script
dotNET.TestNamespace.MyTestClass.Method1(1, dotNET.System.Type.Missing)
但是请注意,此变通办法不适用于整数,双精度和布尔型可选参数。您可以将其用于字符串、日期、对象和其他参数类型。至于整数、布尔值或十进制可选参数,则必须在调用中指定适当的整数、双精度和布尔值。
使用.NET例程返回的值
在.NET中,所有数据类型都是对象。因此,在测试中使用从.NET例程返回的值有一些细节:
1、.NET的整数、双精度和布尔值与TestComplete 数据类型兼容。您可以直接在测试中使用它们。
2、通过OleValue属性访问.NET Decimal和DateTime对象以及可枚举成员的值。
例如,要访问System.DataTime.UtcNow保存当前UTC时间的属性:
JavaScript, JScript
dotNET.System.DateTime.UtcNow.OleValuePython
dotNET.System.DateTime.UtcNow.OleValueVBScript
dotNET.System.DateTime.UtcNow.OleValueDelphiScript
dotNET.System.DateTime.UtcNow.OleValueC++Script, C#Script
dotNET["System"]["DateTime"]["UtcNow"]["OleValue"]
在某些情况下,可以将OleValue与.NET字符串(System.String对象)一起使用。例如,您可能需要使用OleValue将字符串值作为参数传递给用户定义的脚本函数,或者将该值与另一个字符串进行比较。
3、一维.NET数组具有额外的OleValue属性,该属性返回与Variant兼容的数组。这样,您可以对返回的数组使用本机数组操作和脚本语言功能。
例如,在VBScript中,可以使用LBound和UBound函数来确定数组的上下边界,并使用括号语法arr(index)访问数组元素。
在JScript中,通过OleValue属性获得的С#Script和C ++ Script数组与本机数组格式不兼容。应该首先使用toArray()方法将它们转换为本地数组。
JavaScript
function newTest()
{
var dotnet_str = dotNET.System.String.Copy("Hello, world!");
var dotnet_array = dotnet_str.ToCharArray();
// Converting to a native array
var array2 = dotnet_array.OleValue;
for(let i = 0; i < array2.length; i++)
Log.Message(array2[i]);
}
JScript
function newTest()
{
var dotnet_str = dotNET.System.String.Copy("Hello, world!");
var dotnet_array = dotnet_str.ToCharArray();
// Converting to a native array
var array2 = dotnet_array.OleValue.toArray();
for(var i = 0; i < array2.length; i++)
Log.Message(array2[i]);
}
Python
def newTest():
dotnet_str = dotNET.System.String.Copy("Hello, world!")
dotnet_array = dotnet_str.ToCharArray()
# Converting to a native array
array2 = dotnet_array.OleValue
for i in range(len(array2)):
Log.Message(array2[i])
VBScript
Sub newTest
Dim dotnet_str, dotnet_array, array2, i
Set dotnet_str = dotNET.System.String.Copy("Hello, world!")
Set dotnet_array = dotnet_str.ToCharArray()
' Converting to a native array
array2 = dotnet_array.OleValue
i = 0
For i = LBound(array2) To UBound(array2)
Log.Message(array2(i))
Next
End Sub
DelphiScript
procedure newTest;
var dotnet_str, dotnet_array, array2, i;
begin
dotnet_str := dotNET.System.String.Copy('Hello, world!');
dotnet_array := dotnet_str.ToCharArray();
// Converting to a native array
array2 := dotnet_array.OleValue;
for i := VarArrayLowBound(array2,1) to VarArrayHighBound(array2,1) do
Log.Message(array2[i]);
end;
C++Script, C#Script
function newTest()
{
dotnet_str = dotNET["System"]["String"]["Copy"]("Hello, world!");
dotnet_array = dotnet_str["ToCharArray"]();
// Converting to a native array
array2 = dotnet_array["OleValue"]["toArray"]();
for(var i = 0; i < array2.length; i++)
Log["Message"](array2[i]);
}
或者,您可以使用该GetValue方法访问.NET数组项。
JavaScript
function arrayTest()
{
var dotnet_str = dotNET.System.String.Copy("Hello, world!");
var dotnet_array = dotnet_str.ToCharArray();
for(let i = 0; i < dotnet_array.Length; i++)
{
var elem = dotnet_array.GetValue(i);
Log.Message(elem);
}
}
JScript
function arrayTest()
{
var dotnet_str = dotNET.System.String.Copy("Hello, world!");
var dotnet_array = dotnet_str.ToCharArray();
for(var i = 0; i < dotnet_array.Length; i++)
{
var elem = dotnet_array.GetValue(i);
Log.Message(elem);
}
}
Python
def arrayTest():
dotnet_str = dotNET.System.String.Copy("Hello, world!")
dotnet_array = dotnet_str.ToCharArray()
for i in range(dotnet_array.Length):
Log.Message(dotnet_array.GetValue(i))
VBScript
Sub arrayTest
Dim dotnet_str, dotnet_array, array2, i, elem
Set dotnet_str = dotNET.System.String.Copy("Hello, world!")
Set dotnet_array = dotnet_str.ToCharArray()
For i = dotnet_array.GetLowerBound(0) To dotnet_array.GetUpperBound(0)
Log.Message(dotnet_array.GetValue(i))
Next
End Sub
DelphiScript
procedure arrayTest;
var dotnet_str, dotnet_array, array2, i, elem;
begin
dotnet_str := dotNET.System.String.Copy('Hello, world!');
dotnet_array := dotnet_str.ToCharArray();
for i := dotnet_array.GetLowerBound(0) to dotnet_array.GetUpperBound(0) do
Log.Message(dotnet_array.GetValue(i));
end;
C++Script, C#Script
function arrayTest()
{
dotnet_str = dotNET["System"]["String"]["Copy"]("Hello, world!");
dotnet_array = dotnet_str["ToCharArray"]();
for(var i = 0; i < dotnet_array.Length; i++)
{
var elem = dotnet_array["GetValue"](i);
Log["Message"](elem);
}
}
4、二维和多维.NET数组不具有OleValue属性。要获取数组元素,请使用其本机Get(index1, index2, ..., indexN)方法。
5、要使用其他对象,请使用其内部属性和方法。
样品
下面的示例演示如何创建System.String对象(在mscorlib程序集中定义)并在脚本中调用该对象的方法:
JavaScript,JScript
function Test()
{
var str, i;
// Calling a class instance constructor
str = dotNET.System.String.zctor_8(0x41, 3);
Log.Message(str.OleValue); // Using the OleValue property
// Calling a static method
str = dotNET.System.String.Copy("Hello, world!");
// Calling a class instance (non-static) method
i = str.IndexOf("w");
Log.Message(i);
}
Python
def Test():
# Calling a class instance constructor
str = dotNET.System.String.zctor_8(0x41, 3)
Log.Message(str.OleValue) # Using the OleValue property
# Calling a static method
str = dotNET.System.String.Copy("Hello, world!")
# Calling a class instance (non-static) method
i = str.IndexOf("w")
Log.Message(i)
VBScript
Sub Test
Dim str, i
' Calling a class instance constructor
Set str = dotNET.System.String.zctor_8(&H41, 3)
Log.Message str.OleValue ' Using the OleValue property
' Calling a static method
Set str = dotNET.System.String.Copy("Hello, world!")
' Calling a class instance (non-static) method
i = str.IndexOf("w")
Log.Message i
End Sub
DelphiScript
procedure Test;
var str, i;
begin
// Calling a class instance constructor
str := dotNET.System.String.zctor_8($41, 3);
Log.Message(str.OleValue); // Using the OleValue property
// Calling a static method
str := dotNET.System.String.Copy('Hello, world!');
// Calling a class instance (non-static) method
i := str.IndexOf('w');
Log.Message(i);
end;
C++Script, C#Script
function Test()
{
var str, i;
// Calling a class instance constructor
str = dotNET["System"]["String"]["zctor_8"](0x41, 3);
Log["Message"](str["OleValue"]); // Using the OleValue property
// Calling a static method
str = dotNET["System"]["String"]["Copy"]("Hello, world!");
// Calling a class instance (non-static) method
i = str["IndexOf"]("w");
Log["Message"](i);
}
TestComplete还包括一个示例项目,该项目展示了如何通过dotNET对象从.NET程序集调用函数:
应用范围:
<TestComplete示例> \ Desktop \使用.NET类\ Application \ UserApp
<TestComplete示例> \ Desktop \使用.NET Classes \ Application \ UserClassLib
TestComplete项目套件:
<TestComplete示例> \ Desktop \使用.NET类
本教程内容到这里就完结了,感兴趣的朋友可以下载TestComplete试用版免费体验~
相关内容推荐:
自动化测试平台TestComplete使用教程:从.NET程序集调用函数(上)


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线