使用编码的 UI 测试来测试 Windows Phone 应用

使用编码的 UI 测试来测试你的 Windows Phone 应用。

创建简单的 Windows Phone 应用

  1. 使用 Visual C# 或 Visual Basic 模板,为空白的 Windows Phone 应用创建新项目。

    新建 Windows Phone 应用

  2. 在解决方案资源管理器中,打开 MainPage.xaml。从工具箱中,将按钮控件和文本框控件拖动到设计图面。

    向 MainPage.xaml 添加控件

  3. 在“属性”窗口中命名该按钮控件。

    命名按钮控件

  4. 命名该文本框控件。

    命名文本框控件

  5. 在设计图面上,双击该按钮控件并添加以下代码:

    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
    
  6. 按 F5 以在模拟器中运行你的 Windows Phone 应用并且验证其是否工作。

    运行 Windows Phone 应用

  7. 退出模拟器。

部署 Windows Phone 应用

  • 在编码的 UI 测试可以映射应用的控件前,你必须先部署该应用。

    部署 Windows Phone 应用

    模拟器将会启动。该应用现在可用于测试。

    在仿真程序上部署的应用

    在你创建编码的 UI 测试时,请保持模拟器运行。

为 Windows Phone 应用创建编码的 UI 测试

  1. 将新的编码 UI 测试项目添加到包含 Windows Phone 应用的解决方案。

    新建适用于 Windows Phone 的编码的 UI 测试

  2. 选择使用十字线工具编辑 UI 映射。

    使用十字线工具生成编码的 UI 测试。

  3. 使用十字线工具来选择该应用,然后为应用的“AutomationId”属性复制值,该值将在稍后用于启动测试中的应用。

    复制应用的 AutomationId 值

  4. 在模拟器中,启动该应用并使用十字线工具来选择按钮控件。然后将按钮控件添加到 UI 控件图。

    使用十字线工具映射控件

  5. 若要将文本框控件添加到 UI 控件图,请重复上一个步骤。

    使用十字线工具并映射文本框控件

  6. 生成代码以创建用于 UI 控件映射的更改的代码。

    从生成器生成代码

  7. 使用十字线工具选择文本框控件,然后选择**“文本”**属性。

    选择 Text 属性

  8. 添加断言。将在测试中使用它以验证该值是否正确。

    向测试添加断言

  9. 为断言方法添加并生成代码。

    为断言生成代码

  10. Visual C#

    在解决方案资源管理器中,打开 UIMap.Designer.cs 文件以查看你刚为断言方法和控件添加的代码。

    Visual Basic

    在解决方案资源管理器中,打开 CodedUITest1.vb 文件。在 CodedUITestMethod1() 测试方法代码中,右击对断言方法(该方法为自动添加的 Me.UIMap.AssertMethod1())的调用并选择“转到定义”。这将在代码编辑器中打开 UIMap.Designer.vb 文件,因此可以查看你为断言方法和控件添加的代码。

    警告说明警告

    请不要直接修改 UIMap.designer.cs 或 UIMap.Designer.vb 文件。如果执行此操作,则在每次生成测试时都将覆盖对该文件所做的更改。

    断言方法

    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.AssertMethod1ExpectedValues.UITextBoxEditText, uITextBoxEdit.Text);
    }
    
    Public Sub AssertMethod1()
        Dim uITextBoxEdit As XamlEdit = Me.UIApp1Window.UITextBoxEdit
    
        'Verify that the 'Text' property of 'textBox' text box equals 'button'
        Assert.AreEqual(Me.AssertMethod1ExpectedValues.UITextBoxEditText, uITextBoxEdit.Text)
    End Sub
    

    控件

    #region Properties
    public virtual AssertMethod1ExpectedValues AssertMethod1ExpectedValues
    {
        get
        {
            if ((this.mAssertMethod1ExpectedValues == null))
            {
                this.mAssertMethod1ExpectedValues = new AssertMethod1ExpectedValues();
            }
            return this.mAssertMethod1ExpectedValues;
        }
    }
    
    public UIApp1Window UIApp1Window
    {
        get
        {
            if ((this.mUIApp1Window == null))
            {
                this.mUIApp1Window = new UIApp1Window();
            }
            return this.mUIApp1Window;
        }
    }
    #endregion
    
    #region Fields
    private AssertMethod1ExpectedValues mAssertMethod1ExpectedValues;
    
    private UIApp1Window mUIApp1Window;
    #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"
            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"
            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 Phone 应用:

      XamlWindow myAppWindow = XamlWindow.Launch("ed85f6ff-2fd1-4ec5-9eef-696026c3fa7b_cyrqexqw8cc7c!App");
      
      XamlWindow.Launch("ed85f6ff-2fd1-4ec5-9eef-696026c3fa7b_cyrqexqw8cc7c!App");
      
    2. 添加手势以点击按钮控件:

      Gesture.Tap(this.UIMap.UIApp1Window.UIButtonButton);
      
      Gesture.Tap(Me.UIMap.UIApp1Window.UIButtonButton)
      
    3. 验证对已自动生成的断言方法的调用是否发生在启动应用和按钮上的点击手势之后:

      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("ed85f6ff-2fd1-4ec5-9eef-696026c3fa7b_cyrqexqw8cc7c!App");
    
        // Tap the button.
        Gesture.Tap(this.UIMap.UIApp1Window.UIButtonButton);
    
        this.UIMap.AssertMethod1();
    }
    
    <CodedUITest>
    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("ed85f6ff-2fd1-4ec5-9eef-696026c3fa7b_cyrqexqw8cc7c!App")
    
            '// Tap the button.
            Gesture.Tap(Me.UIMap.UIApp1Window.UIButtonButton)
    
            Me.UIMap.AssertMethod1()
        End Sub
    

运行编码的 UI 测试

  • 生成你的测试,然后使用测试资源管理器运行该测试。

    使用测试资源管理器构建并运行测试

    Windows Phone 应用启动,点按按钮的操作完成,然后填充文本框的 Text 属性并且使用断言方法验证。

    运行 Winodws Phone 测试

    测试完成后,测试资源管理器确认该测试已通过。

    测试资源管理器结果

在 Windows Phone 应用上使用数据驱动的编码的 UI 测试

若要测试不同的条件,编码的 UI 测试可以使用不同的数据集多次运行。

适用于 Windows Phone 的数据驱动的编码 UI 测试使用测试方法中的 DataRow 特性定义。在以下示例中,x 和 y 为测试的第一次迭代使用值 1 和 2,为第二次迭代使用值 -1 和 -2。

[DataRow(1, 2, DisplayName = "Add positive numbers")]
[DataRow(-1, -2, DisplayName = "Add negative numbers")]
[TestMethod]
public void DataDrivingDemo_MyTestMethod(int x, int y)

问题解答

问:为什么我无法找到用于创建 Windows Phone 编码的 UI 测试项目的模板?

:请确保你已安装 Visual Studio 2013 Update 2 或更高版本。

问:为了映射 UI 控件,我是否必须在模拟器中部署 Windows Phone 应用?

:是的,编码的 UI 测试生成器需要运行模拟器并且将应用部署到该模拟器。否则,它会引发一条错误消息,说明未能找到运行的模拟器。

问:测试是否只能在模拟器上执行,或者我还可以使用物理设备?

:支持任一选项。通过更改模拟器类型或选择设备工具栏中的设备,选择测试执行的目标。如果选择设备,则 Phone Blue 设备需要连接到计算机的 USB 端口之一。

选择仿真程序版本或物理设备

问:为什么在“生成编码的 UI 测试的代码”对话框中看不到用于记录我的编码的 UI 测试的选项?

:Windows Phone 应用不支持录制选项。

问:我是否能为基于 WinJS、Silverlight 或 HTML5 的 Windows Phone 应用创建编码的 UI 测试?

:否,仅支持基于 XAML 的应用。

问:我是否能在不运行 Windows 8.1 的系统上为 Windows Phone 应用创建编码的 UI 测试?

:否,编码的 UI 测试项目(Windows Phone 应用)模板仅适用于 Windows 8.1。

问:我是否可以选择模拟器外部的控件?

:否,生成器将不会检测它们。

问:我是否可以使用编码的 UI 测试生成器来映射使用物理手机设备的控件?

:否,如果应用已部署到模拟器,生成器只能映射 UI 元素。

问:为什么我无法修改 UIMap.Designer 文件中的代码?

:每次使用“UIMap - 编码的 UI 测试生成器”生成代码时,都将覆盖在 UIMapDesigner.cs 文件中所做的任何代码更改。如果必须修改录制的方法,则必须将其复制到 UIMap.cs 文件并对其重命名。 UIMap.cs 文件可用于重写 UIMapDesigner.cs 文件中的方法和属性。必须在 Coded UITest.cs 文件中删除对原始方法的引用,并将其替换为重命名的方法名称。

问:我是否可以从命令行对 Windows Phone 应用运行编码的 UI 测试?

:是,你使用 runsettings 文件来指定用于测试执行的目标设备。例如:

vstest.console.exe “pathToYourCodedUITestDll” /settings:devicetarget.runsettings

示例 runsettings 文件:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<MSPhoneTest>
<!--to specify test execution on device, use a TargetDevice option as follows-->
<TargetDevice>Device</TargetDevice>
<!--to specify an emulator instead, use a TargetDevice option like below-->
<!--<TargetDevice>Emulator 8.1 WVGA 4 inch 512MB</TargetDevice>-->
</MSPhoneTest>
</RunSettings>

问:针对基于 XAML 的 Windows 应用商店应用和 Windows Phone 应用的编码的 UI 测试有什么区别?

:以下是一些主要区别:

功能

Windows 应用商店应用程序

Windows Phone 应用

运行测试的目标

本地或远程计算机。当你使用自动测试用例运行测试时,可以指定远程计算机。请参阅在 Microsoft 测试管理器中自动化测试用例

模拟器或设备。请参阅本主题中的问:是否测试只能在模拟器上执行,或者我还可以使用物理设备?。

从命令行执行

指定目标无需设置文件。

指定目标需要 Runsettings 文件。

针对 Shell 控件的专用类

DirectUIControl

UITestControl

在 XAML 应用中的 WebView 控件

如果你使用 Html* 专用类与 HTML 元素进行交互则受支持。请参阅Microsoft.VisualStudio.TestTools.UITesting.HtmlControls

不支持。

从 MTM 执行自动测试

支持。

不支持。

数据驱动的测试

有关使用外部数据源和使用测试方法中 DataSource 特性的信息,请参阅数据驱动的测试

使用测试方法中的 DataRow 特性将数据指定为内联。请参阅本主题中的对 Windows Phone 应用使用数据驱动的编码 UI 测试。

有关针对 Windows 应用商店应用的编码的 UI 测试,请参阅使用编码的 UI 测试来测试 Windows 应用商店应用

外部资源

“Microsoft Visual Studio 应用程序生命周期管理”博客:使用编码的 UI 测试基于 XAML 的 Windows Phone 应用

请参见

概念

使用 UI 自动化验证代码