今日は .NET UI オートメーション フレームワークについて学習しています。ということで、これまでやってきたこと(いろいろな記事を参考に)
Listbox、PictureBox、TextBox、および Button コントロールを含む WinForm を用意します。写真を参照してください:
すべてのUIオートメーションテストスクリプトまたはwinform UIテストを自動化するコードを持つconsoleappがあります。
作業: リストボックスからアイテムを選択すると、画像ボックスは画像をロードして表示します (ロードするコードはリストボックスの SelectedIndexChanged イベントにあります)。
以下は、フォームの listBox コントロールのコードです。
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.BackColor = Color.White;
pictureBox1.Image = imageCollection.ElementAtOrDefault(listBox1.SelectedIndex);
textBox1.Text = pictureBox1.Image.GetHashCode().ToString();
this.Refresh();
}
これで、私の UIAutomation テスト スクリプト コードは次のようになります: (必要な部分のみが示されています)
AutomationElement listBoxElement = mainFormWindowElement.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty, "listBox1"));
Assert.IsNotNull(listBoxElement, "Cant find the listbox element");
AutomationElementCollection listBoxItems =
listBoxElement.FindAll(TreeScope.Children,new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.ListItem));
AutomationElement itemToSelectInListBox = listBoxItems[new Random().Next(0, listBoxItems.Count - 1)];
Object selectPattern = null;
if (itemToSelectInListBox.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectPattern))
{
(selectPattern as SelectionItemPattern).AddToSelection();
(selectPattern as SelectionItemPattern).Select();
}
コードを実行した後、Select() メソッドは機能し、次のように Form リストボックス項目が選択されます。
画像でわかるように、リストボックス項目は選択されていますが、イベント SelectedIndexChange は発生しておらず、ピクチャボックスは変更を反映していません。
したがって、ポインタは非常に役立ちます:)
ありがとう