共用方式為


使用自動程式碼 UI 測試來測試 Windows 市集應用程式

使用自動程式碼 UI 測試驗證 Windows 市集應用程式。

建立簡單 Windows 市集應用程式

  1. 如果您想要執行 XAML Windows 市集應用程式的自動程式碼 UI 測試,則必須設定可識別每個控制項的唯一自動化屬性

    在 [工具] 功能表上,指向 [選項],然後依序選擇 [文字編輯器]、[XAML] 和 [其他]。

    選取此核取方塊,以在建立時自動命名互動項目。

    XAML 其他選項

  2. 使用 Visual C# 或 Visual Basic 範本,建立空白 XAML Windows 市集應用程式的新專案。

    建立 Windows 市集空白的應用程式 (XAML)

  3. 在 [方案總管] 中,開啟 MainPage.xaml。從 [工具箱] 中,將按鈕控制項和文字方塊控制項拖曳至設計介面。

    設計 Windows 市集應用程式

  4. 按兩下按鈕控制項,並加入下列程式碼:

    private void button_Click_1(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = this.button.Name;
    }
    
    Public NotInheritable Class MainPage
        Inherits Page
    
        Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles Button.Click
            Me.textBox.Text = Me.button.Name
        End Sub
    End Class
    
  5. 按 F5 鍵執行 Windows 市集應用程式。

為 Windows 市集應用程式建立和執行自動程式碼 UI 測試

  1. 為 Windows 市集應用程式建立新的自動程式碼 UI 測試專案。

    新增自動程式碼 UI 測試專案 (Windows 市集應用程式)

  2. 選擇使用交叉線工具編輯 UI 對應。

    選擇 [編輯 UI 對應或加入判斷提示]

  3. 使用自動程式碼 UI 測試產生器中的交叉線工具選取應用程式磚,並在 [AutomationId] 上按一下滑鼠右鍵,然後選擇 [將值複製到剪貼簿]。剪貼簿中的值稍後將會用來撰寫可啟動應用程式進行測試的動作。

    將 AutomationId 複製到剪貼簿

  4. 在執行中 Windows 市集應用程式中,使用交叉線工具選取按鈕控制項和文字方塊控制項。加入每個控制項之後,請選擇 [自動程式碼 UI 測試產生器] 工具列中的 [將控制項加入至 UI 控制項對應] 按鈕。

    將控制項加入至 UI 對應

  5. 選擇 [自動程式碼 UI 測試產生器] 工具列中的 [產生程式碼] 按鈕,然後選擇 [產生] 建立 UI 控制項對應變更的程式碼。

    產生 UI 對應的程式碼

  6. 選擇此按鈕,以在文字方塊中設定值。

    按一下按鈕控制項以設定 Textbox 值

  7. 使用交叉線工具選取文字方塊控制項,然後選取 [文字] 屬性。

    選取 Text 屬性

  8. 加入判斷提示。測試將使用此判斷提示來驗證值是否正確。

    使用交叉線工具選擇 Textbox 並加入判斷提示

  9. 加入並產生判斷提示的程式碼。

    產生 Textbox 判斷提示的程式碼

  10. Visual C#

    在 [方案總管] 中,開啟 UIMap.Designer.cs 檔案,以檢視針對 assert 方法和控制項所加入的程式碼。

    Visual Basic

    在 [方案總管] 中,開啟 CodedUITest1.vb 檔案,然後在 CodedUITestMethod1() 測試方法程式碼中,於已自動加入 Me.UIMap.AssertMethod1() 的判斷提示方法呼叫上按一下滑鼠右鍵,然後選擇 [移至定義]。這將會在程式碼編輯器中開啟 UIMap.Designer.vb 檔案,讓您可以檢視針對 assert 方法和控制項所加入的程式碼。

    注意事項警告

    請勿直接修改 UIMap.designer.cs 或 UIMap.Designer.vb 檔案。如果您這麼做,則每次產生測試時,都會覆寫此檔案的變更。

    Assert 方法

    public void AssertMethod1()
    {
        #region Variable Declarations
        XamlEdit uITextBoxEdit = this.UIApp1Window.UITextBoxEdit;
        #endregion
    
        // Verify that the 'Text' property of 'textBox' text box equals 'button'
        Assert.AreEqual(this.AssertMethod3ExpectedValues.UITextBoxEditText, uITextBoxEdit.Text);
    }
    
    Public Sub AssertMethod1()
        Dim uITextBoxEdit As XamlEdit = Me.UIApp2Window.UITextBoxEdit
    
        'Verify that the 'Text' property of 'textBox' text box equals 'button'
        Assert.AreEqual(Me.AssertMethod1ExpectedValues.UITextBoxEditText, uITextBoxEdit.Text)
    End Sub
    

    控制項

    #region Properties
    public XamlButton UIButtonButton
    {
        get
        {
            if ((this.mUIButtonButton == null))
            {
                this.mUIButtonButton = new XamlButton(this);
                #region Search Criteria
                this.mUIButtonButton.SearchProperties[XamlButton.PropertyNames.AutomationId] = "button";
                this.mUIButtonButton.WindowTitles.Add("App1");
                #endregion
            }
            return this.mUIButtonButton;
        }
    }
    
    public XamlEdit UITextBoxEdit
    {
        get
        {
            if ((this.mUITextBoxEdit == null))
            {
                this.mUITextBoxEdit = new XamlEdit(this);
                #region Search Criteria
                this.mUITextBoxEdit.SearchProperties[XamlEdit.PropertyNames.AutomationId] = "textBox";
                this.mUITextBoxEdit.WindowTitles.Add("App1");
                #endregion
            }
            return this.mUITextBoxEdit;
        }
    }
    #endregion
    
    #region Fields
    private XamlButton mUIButtonButton;
    
    private XamlEdit mUITextBoxEdit;
    #endregion
    
    #Region "Properties"
    Public ReadOnly Property UIButtonButton() As XamlButton
        Get
            If (Me.mUIButtonButton Is Nothing) Then
                Me.mUIButtonButton = New XamlButton(Me)
                Me.mUIButtonButton.SearchProperties(XamlButton.PropertyNames.AutomationId) = "button"
                Me.mUIButtonButton.WindowTitles.Add("App2")
            End If
            Return Me.mUIButtonButton
        End Get
    End Property
    
    Public ReadOnly Property UITextBoxEdit() As XamlEdit
        Get
            If (Me.mUITextBoxEdit Is Nothing) Then
                Me.mUITextBoxEdit = New XamlEdit(Me)
                Me.mUITextBoxEdit.SearchProperties(XamlEdit.PropertyNames.AutomationId) = "textBox"
                Me.mUITextBoxEdit.WindowTitles.Add("App2")
            End If
            Return Me.mUITextBoxEdit
        End Get
    End Property
    #End Region
    
    #Region "Fields"
    Private mUIButtonButton As XamlButton
    
    Private mUITextBoxEdit As XamlEdit
    #End Region
    
  11. 在 [方案總管] 中,開啟 CodedUITest1.cs 或 CodedUITest1.vb 檔案。您現在可以將程式碼加入至 CodedUTTestMethod1 方法,因為動作需要使用已加入至 UIMap 的控制項執行測試:

    1. 使用您先前複製至剪貼簿的自動化 ID 屬性,以啟動 Windows 市集應用程式:

      XamlWindow.Launch("8ebca7c4-effe-4c86-9998-068daccee452_cyrqexqw8cc7c!App")
      
      XamlWindow myAppWindow = XamlWindow.Launch("7254db3e-20a7-424e-8e05-7c4dabf4f28d_cyrqexqw8cc7c!App");
      
    2. 加入手勢,以點選按鈕控制項:

      Gesture.Tap(this.UIMap.UIApp1Window. UIButtonButton);
      
      Gesture.Tap(Me.UIMap.UIApp2Window. UIButtonButton)
      
    3. 確認已自動產生的 assert 方法呼叫是在啟動應用程式之後進行,並點選按鈕上的手勢:

      this.UIMap.AssertMethod1();
      
      Me.UIMap.AssertMethod1()
      

    加入程式碼之後,CodedUITestMethod1 測試方法應該顯示如下:

    [TestMethod]
    public void CodedUITestMethod1()
    {
        // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
    
        // Launch the app.
        XamlWindow myAppWindow = XamlWindow.Launch("7254db3e-20a7-424e-8e05-7c4dabf4f28d_cyrqexqw8cc7c!App");
    
        // Tap the button.
        Gesture.Tap(this.UIMap.UIApp1Window.UIButtonButton);
    
        this.UIMap.AssertMethod1();
    }
    
    <CodedUITest(CodedUITestType.WindowsStore)>
    Public Class CodedUITest1
    
        <TestMethod()>
        Public Sub CodedUITestMethod1()
            '            
            ' To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
            '
    
            ' Launch the app.
            XamlWindow.Launch("8ebca7c4-effe-4c86-9998-068daccee452_cyrqexqw8cc7c!App")
    
            '// Tap the button.
            Gesture.Tap(Me.UIMap.UIApp2Window.UIButtonButton)
    
            Me.UIMap.AssertMethod1()
        End Sub
    
  12. 建置您的測試,然後使用測試總管執行測試。

    從 [測試總管] 執行自動程式碼 UI 測試

    即會啟動 Windows 市集應用程式、完成點選按鈕的動作,以及填入文字方塊的 Text 屬性並使用 assert 方法進行驗證。

    執行自動程式碼 UI 測試

    測試完成之後,測試總管會顯示已通過測試。

    通過的測試在 [測試總管] 中顯示

問與答

  • 問:為什麼在 [產生自動程式碼 UI 測試的程式碼] 對話方塊中看不到錄製自動程式碼 UI 測試的選項?

    :Windows 市集應用程式不支援錄製的選項。

  • 問:我可以根據 WinJS 建立 Windows 市集應用程式的自動程式碼 UI 測試嗎?

    :不可以,目前只支援 XAML 應用程式。

  • 問:我可以在未執行 Windows 8 的系統上建立 Windows 市集應用程式的自動程式碼 UI 測試嗎?

    :否,只有 Windows 8 上才能使用自動程式碼 UI 測試專案 (Windows 市集應用程式) 範本。

  • 問:為什麼無法修改 UIMap.Designer 檔案中的程式碼?

    :每次您使用 [UIMap - 自動程式碼 UI 測試產生器] 產生程式碼時,對 UIMapDesigner.cs 檔案中的程式碼所做的變更都會被覆寫。如果您需要修改錄製的方法,必須將它複製到 UIMap.cs 檔案並重新命名。UIMap.cs 檔案可用來覆寫 UIMapDesigner.cs 檔案中的方法和屬性。您必須移除 Coded UITest.cs 檔案中原始方法的參考,並將它取代為重新命名的方法名稱。

請參閱

概念

使用 UI 自動化驗證程式碼

為用於測試的 Windows 市集控制項設定唯一自動化屬性