Automation、昨日の続き

http://d.hatena.ne.jp/kcha/20080604/1212569893の続きです。
とりあえず、適当にVB2005で叩いてみた。NUnitをリンクしていないので、SetUpとTearDownも一連の動作としてつっこんであります。

Imports System.Windows.Automation

Module Module1

    Sub Main()
        Dim path As String = "..\..\..\UITest01\bin\Debug\UITest01.exe"
        Dim app As Process = Process.Start(path)

        System.Threading.Thread.Sleep(1000)

        Dim aeForm As AutomationElement = AutomationElement.FromHandle(app.MainWindowHandle)

        Dim textInput As AutomationElement = FindElement(aeForm, "TextBoxInput")
        Dim buttonAdd As AutomationElement = FindElement(aeForm, "ButtonAdd")
        Dim listbox As AutomationElement = FindElement(aeForm, "ListBoxOutput")

        '操作対象のコントロール・パターンを取得
        Dim vpTxtInput As ValuePattern = _
            CType(textInput.GetCurrentPattern(ValuePattern.Pattern), ValuePattern)
        Dim ipBtnAdd As InvokePattern = _
            CType(buttonAdd.GetCurrentPattern(InvokePattern.Pattern), InvokePattern)

        '一つ目の値を入力、追加ボタンを押下
        vpTxtInput.SetValue("わっち")
        ipBtnAdd.Invoke()

        '二つ目の値を入力、追加ボタンを押下
        vpTxtInput.SetValue("おおかみ")
        ipBtnAdd.Invoke()

        '三つ目の値を入力、追加ボタンを押下
        vpTxtInput.SetValue("萌え")
        ipBtnAdd.Invoke()

        'リストボックスの値を取得するため
        'AutomationElementCollectionオブジェクトを取得
        Dim aecListItems As AutomationElementCollection = _
            listbox.FindAll(TreeScope.Children, _
                            New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem))

        If Not "わっち" = aecListItems(0).Current.Name Then
            Console.WriteLine("一つ目の値")
        End If

        If Not "おおかみ" = aecListItems(1).Current.Name Then
            Console.WriteLine("二つ目の値")
        End If

        If Not "萌え" = aecListItems(2).Current.Name Then
            Console.WriteLine("三つ目の値")
        End If

        If Not 3 = aecListItems.Count Then
            Console.WriteLine("追加されたアイテム数")
        End If

        'teardown
        Dim wpForm As WindowPattern = CType(aeForm.GetCurrentPattern(WindowPattern.Pattern), WindowPattern)
        wpForm.Close()
    End Sub

    Public Function FindElement(ByRef rootElement As AutomationElement, _
                                ByVal automationID As String) As AutomationElement
        Dim element As AutomationElement = _
            rootElement.FindFirst(TreeScope.Element + TreeScope.Descendants, _
                                  New PropertyCondition(AutomationElement.AutomationIdProperty, automationID))

        Return element
    End Function

End Module

狼と香辛料積ん読が残ってるんだよな・・・(意味不明)