自動(dòng)化測(cè)試平臺(tái)TestComplete使用教程:從.NET程序集調(diào)用函數(shù)(下)
TestComplete是一款具有人工智能的自動(dòng)UI測(cè)試工具,利用自動(dòng)化測(cè)試工具和人工智能支持的混合對(duì)象識(shí)別引擎,輕松檢測(cè)和測(cè)試每個(gè)桌面,Web和移動(dòng)應(yīng)用程序。使用TestComplete,可以提高測(cè)試覆蓋率并幫助提供經(jīng)過(guò)實(shí)戰(zhàn)考驗(yàn)的高質(zhì)量軟件。
在TestComplete中,您可以從腳本中調(diào)用駐留在任何.NET程序集中的例程。該程序集可以與.NET Framework或第三方.NET應(yīng)用程序一起提供。該教程內(nèi)容較多,分為上下兩篇文章,本文是下半部分內(nèi)容,緊接前文內(nèi)容~
使用可選參數(shù)調(diào)用.NET例程
從測(cè)試中調(diào)用.NET函數(shù)時(shí),TestComplete不允許省略可選參數(shù)。也就是說(shuō),您必須在調(diào)用中指定所有參數(shù),包括可選參數(shù)。
這意味著,如果要使用默認(rèn)參數(shù)值,則必須在調(diào)用中顯式指定它們。對(duì)此的部分解決方法是dotNET.System.Type.Missing作為可選參數(shù)值傳遞。例如,如果您有一種通過(guò)以下方式聲明的方法-
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腳本中調(diào)用它,可以使用以下代碼:
Script
dotNET.TestNamespace.MyTestClass.Method1(1, dotNET.System.Type.Missing)
但是請(qǐng)注意,此變通辦法不適用于整數(shù),雙精度和布爾型可選參數(shù)。您可以將其用于字符串、日期、對(duì)象和其他參數(shù)類型。至于整數(shù)、布爾值或十進(jìn)制可選參數(shù),則必須在調(diào)用中指定適當(dāng)?shù)恼麛?shù)、雙精度和布爾值。
使用.NET例程返回的值
在.NET中,所有數(shù)據(jù)類型都是對(duì)象。因此,在測(cè)試中使用從.NET例程返回的值有一些細(xì)節(jié):
1、.NET的整數(shù)、雙精度和布爾值與TestComplete 數(shù)據(jù)類型兼容。您可以直接在測(cè)試中使用它們。
2、通過(guò)OleValue屬性訪問(wèn).NET Decimal和DateTime對(duì)象以及可枚舉成員的值。
例如,要訪問(wèn)System.DataTime.UtcNow保存當(dāng)前UTC時(shí)間的屬性:
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對(duì)象)一起使用。例如,您可能需要使用OleValue將字符串值作為參數(shù)傳遞給用戶定義的腳本函數(shù),或者將該值與另一個(gè)字符串進(jìn)行比較。
3、一維.NET數(shù)組具有額外的OleValue屬性,該屬性返回與Variant兼容的數(shù)組。這樣,您可以對(duì)返回的數(shù)組使用本機(jī)數(shù)組操作和腳本語(yǔ)言功能。
例如,在VBScript中,可以使用LBound和UBound函數(shù)來(lái)確定數(shù)組的上下邊界,并使用括號(hào)語(yǔ)法arr(index)訪問(wèn)數(shù)組元素。
在JScript中,通過(guò)OleValue屬性獲得的С#Script和C ++ Script數(shù)組與本機(jī)數(shù)組格式不兼容。應(yīng)該首先使用toArray()方法將它們轉(zhuǎn)換為本地?cái)?shù)組。
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方法訪問(wèn).NET數(shù)組項(xiàng)。
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數(shù)組不具有OleValue屬性。要獲取數(shù)組元素,請(qǐng)使用其本機(jī)Get(index1, index2, ..., indexN)方法。
5、要使用其他對(duì)象,請(qǐng)使用其內(nèi)部屬性和方法。
樣品
下面的示例演示如何創(chuàng)建System.String對(duì)象(在mscorlib程序集中定義)并在腳本中調(diào)用該對(duì)象的方法:
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還包括一個(gè)示例項(xiàng)目,該項(xiàng)目展示了如何通過(guò)dotNET對(duì)象從.NET程序集調(diào)用函數(shù):
應(yīng)用范圍:
<TestComplete示例> \ Desktop \使用.NET類\ Application \ UserApp
<TestComplete示例> \ Desktop \使用.NET Classes \ Application \ UserClassLib
TestComplete項(xiàng)目套件:
<TestComplete示例> \ Desktop \使用.NET類
本教程內(nèi)容到這里就完結(jié)了,感興趣的朋友可以下載TestComplete試用版免費(fèi)體驗(yàn)~
相關(guān)內(nèi)容推薦:
自動(dòng)化測(cè)試平臺(tái)TestComplete使用教程:從.NET程序集調(diào)用函數(shù)(上)