動的呼び出しを使用すると、テスト対象アプリケーション内のコントロールの実際のインスタンスに関して、メソッドの呼び出し、プロパティーの取得、またはプロパティーの設定を直接実行できます。また、このコントロールの Silk4NET API で使用できないメソッドおよびプロパティーも呼び出すことができます。動的呼び出しは、作業しているカスタム コントロールを操作するために必要な機能が、Silk4NET API を通して公開されていない場合に特に便利です。
オブジェクトの動的メソッドは Invoke メソッドを使用して呼び出します。コントロールでサポートされている動的メソッドのリストを取得するには、GetDynamicMethodList メソッドを使用します。
オブジェクトの複数の動的メソッドは InvokeMethods メソッドを使用して呼び出します。コントロールでサポートされている動的メソッドのリストを取得するには、GetDynamicMethodList メソッドを使用します。
動的プロパティの取得には GetProperty メソッドを、動的プロパティの設定には SetProperty メソッドを使用します。コントロールでサポートされている動的プロパティのリストを取得するには、GetPropertyList メソッドを使用します。
control.Invoke("SetTitle", "my new title")
Silk4NET の DataGrid 型のオブジェクトでは、MSDN が System.Windows.Forms.DataGrid 型に定義しているすべてのメソッドを呼び出すことができます。
//VB .NET code
Dim isExpanded As Boolean = dataGrid.Invoke("IsExpanded", 3)
//C# code
bool isExpanded = (bool) dataGrid.Invoke("IsExpanded", 3);
//VB .NET code
Dim result as Integer = (Integer) mainWindow.Invoke("System.String.Compare", "a", "b")
//C# code
int result = (int) mainWindow.Invoke("System.String.Compare", "a", "b");
この例では、ユーザーが生成したメソッド GetContents を動的に呼び出す方法を示します。
テスト対象アプリケーション (AUT) のコントロールの操作に使用するコードを作成できます (この例では UltraGrid)。UltraGrid の内容を取得するために、複雑な動的呼び出しを作成するのではなく、新しいメソッド GetContents を生成し、この新しいメソッドを動的に呼び出すことができます。
//C# code, because this is code in the AUT
namespace UltraGridExtensions {
public class UltraGridUtil {
/// <summary>
/// Retrieves the contents of an UltraGrid as nested list
/// </summary>
/// <param name="grid"></param>
/// <returns></returns>
public static List<List<string>> GetContents(Infragistics.Win.UltraWinGrid.UltraGrid grid) {
var result = new List<List<string>>();
foreach (var row in grid.Rows) {
var rowContent = new List<string>();
foreach (var cell in row.Cells) {
rowContent.Add(cell.Text);
}
result.Add(rowContent);
}
return result;
}
}
}
FormsWindow.LoadAssembly(String assemblyFileName)次のようにして、フルパスで指定してアセンブリをロードします。
mainWindow.LoadAssembly("C:/temp/ultraGridExtensions.dll")
//VB.NET code Dim assemblyLocation = GetType(UltraGridExtensions.UltraGridUtil).Assembly.Location mainWindow.LoadAssembly(assemblyLocation)
//C# code string assemblyLocation = typeof(UltraGridExtensions.UltraGridUtil).Assembly.Location; mainWindow.LoadAssembly(assemblyLocation);
var contents = (IList) mainWindow.Invoke("UltraGridExtensions.UltraGridUtil.GetContents", ultraGrid);
Invoke メソッドを呼び出す mainWindow オブジェクトは、AUT を特定しているだけなので、同じ AUT の他のオブジェクトに置き換えてもかまいません。
string cellText = dataGrid.Rows[rowIndex].Cells[columnIndex].Text;
string cellText = dataGrid.Rows[0].Cells[2];
// C# code
var dataGrid = mainWindow.WPFControl("@automationId='Custom Data Grid'");
// Get text contents of third cell in first row.
var rowIndex = 0;
var columnIndex = 2;
var methodNames = new List<string>();
methodNames.Add("Rows"); // Get the list of rows from the grid.
methodNames.Add("get_Item"); // Get a specific row from the list of rows by using the indexer method.
methodNames.Add("Cells"); // Get the list of cells from the the row.
methodNames.Add("get_Item"); // Get a specific cell from the list of cells by using the indexer method.
methodNames.Add("Text"); // Get the text of the cell.
var parameters = new List<List<object>>();
parameters.Add(new List<object>()); // Parameters for the Rows property.
parameters.Add(new List<object>() { rowIndex }); // Parameters for the get_Item method.
parameters.Add(new List<object>()); // Parameters for the Cells property.
parameters.Add(new List<object>() { columnIndex }); // Parameters for the get_Item method.
parameters.Add(new List<object>()); // Parameters for the Text property.
string cellText = (string)dataGrid.InvokeMethods(methodNames, parameters);
' VB .NET code
Dim dataGrid = mainWindow.WPFControl("@automationId='Custom Data Grid'")
' Get text contents of third cell in first row.
Dim rowIndex = 0
Dim column = 2
Dim methodNames = New List(Of String)()
methodNames.Add("Rows") ' Get the list of rows from the grid.
methodNames.Add("get_Item") ' Get a specific row from the list of rows by using the indexer method.
methodNames.Add("Cells") ' Get the list of cells from the the row.
methodNames.Add("get_Item") ' Get a specific cell from the list of cells by using the indexer method.
methodNames.Add("Text") ' Get the text of the cell.
Dim parameters = New List(Of List(Of Object))()
parameters.Add(New List(Of Object)()) ' Parameters for the Rows property.
parameters.Add(New List(Of Object) From {rowIndex}) ' Parameters for the get_Item method.
parameters.Add(New List(Of Object)()) ' Parameters for the Cells property.
parameters.Add(New List(Of Object) From {columnIndex}) ' Parameters for the get_Item method.
parameters.Add(New List(Of Object)()) ' Parameters for the Text property.
Dim cellText As String = dataGrid.InvokeMethods(methodNames, parameters)
// C# code, if the AUT is implemented in C#.
public static string GetCellText(Infragistics.Win.UltraWinGrid.UltraGrid dataGrid, int rowIndex, int columnIndex) {
return dataGrid.Rows[rowIndex].Cells[columnIndex].Text;
' VB code, if the AUT is implemented in VB.
public static string GetCellText(Infragistics.Win.UltraWinGrid.UltraGrid dataGrid, int rowIndex, int columnIndex) {
return dataGrid.Rows[rowIndex].Cells[columnIndex].Text; テスト スクリプトから GetCellText メソッドを動的に呼び出して、セルの内容をテキストで取得します。
// C# code
string cellText = (string) mainWindow.Invoke("GetCellText", dataGrid, rowIndex, columnIndex);
'VB .NET code
Dim cellText As String = mainWindow.Invoke("GetCellText", dataGrid, rowIndex, columnIndex)
詳細については、「テスト対象アプリケーションにコードを追加してカスタム コントロールをテストする」を参照してください。
Silk4NET 型には、プリミティブ型 (boolean、int、string など)、リスト、およびその他の型 (Point や Rect など) が含まれます。
列挙パラメータは文字列として渡す必要があります。文字列は、列挙値の名前と一致しなければなりません。たとえば、メソッドが .NET 列挙型 System.Windows.Visiblity のパラメータを必要とする場合、次の文字列値を使用できます: Visible、Hidden、Collapsed。
.NET 構造体とオブジェクト パラメータはリストとして渡す必要があります。リスト内の要素は、テスト アプリケーションの .NET オブジェクトで定義されているコンストラクタの 1 つと一致しなければなりません。たとえば、メソッドが .NET 型 System.Windows.Vector のパラメータを必要とする場合、2 つの整数値を持つリストを渡すことができます。これが機能するのは、System.Windows.Vector 型が 2 つの整数値を引数に取るコンストラクタを持つためです。
WPF コントロール パラメータは TestObject として渡すことができます。
返された .NET オブジェクトに対して ToString を呼び出せば、文字列表現を取得できます。
public void Reset()
public int Add(int number1, int number2)
public System.Windows.Vector StrechVector(System.Windows.Vector vector, double
factor)
public String Description { get;}テスト担当者は、テスト内からメソッドを直接呼び出すことができます。例:customControl.Invoke("Reset")
Dim sum as Integer = customControl.Invoke("Add", 1, 2)
' the vector can be passed as list of integer
Dim vector = New List(Of Integer)
vector.Add(3)
vector.Add(4)
' returns "6;8" because this is the string representation of the .NET object
Dim strechedVector As String = customControl.Invoke("StrechVector", vector, 2.0)
Dim description As String = customControl.GetProperty("Description")