好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用Visual Studio 2010进行UI自动化测试

使用Visual Studio 2010进行UI自动化测试

1. 被测试功能说明。

  非常简单的一个功能:在文本框中输入文字,点击Go按钮,下面则会显示文本框中输入的文字。

  

  接下来我们就为这个小程序创建UI自动化测试:

2. 创建测试项目。

  打开VS2010,选择 文件 - 新建 -项目,模板列表中选择 测试 - 测试项目,输入测试项目名称 UITestProject。

  

3. 添加UI自动化测试。

  1. 右键 UITestProject,选择 添加 - 编码的 UI 测试。

  

  2. VS2010将弹出如下窗体:单击 确定 按钮。

  

  3. 进而VS2010自动最小化,右下角弹出 UI 自动化测试生成器:

  

4. UI自动化测试基本步骤。

  1. 单击红色按钮开始录制。

  

  2. 操作一遍功能。

  

  3. 暂停录制。

  

  4. 查看录制的操作。

  

  5. 生成测试代码。

  

  测试代码:

         [TestMethod]
          public   void   CodedUITestMethod1()
        {

              this  .UIMap.TestMyApplication();

        } 

  查看 TestMyApplication 方法,转到定义:

         ///   <summary> 
         ///   TestMyApplication - 使用“TestMyApplicationParams”将参数传递到此方法中。
          ///   </summary> 
         public   void   TestMyApplication()
        {
              #region  Variable Declarations 
            WpfEdit uITbContentEdit  =  this  .UIMainWindowWindow.UITbContentEdit;
            WpfButton uIGOButton  =  this  .UIMainWindowWindow.UIGOButton;
            WpfButton uI关闭Button  =  this  .UIMainWindowWindow.UIMainWindowTitleBar.UI关闭Button;
              #endregion 

             //   启动“%VisualStudioDir%\Projects\MyUITestWpfSimple\MyUITestWpfSimple\bin\Debug\MyUITestWpfSimple.exe” 
            ApplicationUnderTest uIMainWindowWindow = ApplicationUnderTest.Launch( this .TestMyApplicationParams.UIMainWindowWindowExePath,  this  .TestMyApplicationParams.UIMainWindowWindowAlternateExePath);

              //   在 “tbContent” 文本框 中键入“Hello cnblogs” 
            uITbContentEdit.Text =  this  .TestMyApplicationParams.UITbContentEditText;

              //   单击 “Go” 按钮 
            Mouse.Click(uIGOButton,  new  Point( 66 ,  10  ));

              //   单击 “关闭” 按钮 
            Mouse.Click(uI关闭Button,  new  Point( 24 ,  12  ));
        } 

  TestMyApplication方法也是根据UI测试生成器记录的步骤生成的。

  6. 运行测试,VS将会自动打开窗体,自动在文本框中输入文字Hello cnblogs,自动点击按钮。UI自动化测试的基本操作就是这样了。

6. 为 UI 自动化测试添加断言。

  1. 按住 UI测试生成器 的十字线,拖到要断言的控件上。

  

  2. 松开鼠标,将弹出添加断言窗口。点击添加断言,输入结果值:Hello  cnblogs 。

  

  3. 单击 确定,输入验证方法名:ValidateValue。

  

  4. 查看生成的代码:

         [TestMethod]
          public   void   CodedUITestMethod1()
        {

              this  .UIMap.TestMyApplication();
              this   .UIMap.ValidateValue(); 
        } 

  转到ValiateValue方法的定义:

         ///   <summary> 
         ///   ValidateValue - 使用“ValidateValueExpectedValues”将参数传递到此方法中。
          ///   </summary> 
         public   void   ValidateValue()
        {
              #region  Variable Declarations 
            WpfText uIHellocnblogsText  =  this  .UIMainWindowWindow.UIHellocnblogsText;
              #endregion 

             //   确认 “Hello cnblogs” 标签 的属性“AutomationId”等于“Hello cnblogs” 
            Assert.AreEqual( this  .ValidateValueExpectedValues.UIHellocnblogsTextAutomationId, uIHellocnblogsText.AutomationId);

              //   确认 “Hello cnblogs” 标签 的属性“AutomationId”等于“Hello cnblogs” 
            Assert.AreEqual( this  .ValidateValueExpectedValues.UIHellocnblogsTextAutomationId1, uIHellocnblogsText.AutomationId);
        } 

  生成断言和生成测试步骤的原理是一样的。

7. 为 UI 自动化测试添加测试数据。

  1. 打开 测试视图(测试 - 窗口 - 测试视图),选择当前的测试项目,选择属性。在属性对话框中,选择 数据连接字符串:

  

  2. 选择数据源类型,这里我选择 CSV文件。

  

  3. 编码实现使用CSV文件中的数据进行测试。

        [DataSource( "  Microsoft.VisualStudio.TestTools.DataSource.CSV  " ,  "  |DataDirectory|\\TestSources.csv  "  , 
              "  TestSources#csv  " , DataAccessMethod.Sequential), DeploymentItem( "  UITestProject\\TestSources.csv  "  ), TestMethod]
          public   void   CodedUITestMethod1()
        {

              using  (ApplicationUnderTest.Launch( this  .UIMap.StartMyApplicationParams.UIMainWindowWindowExePath,
                  this  .UIMap.StartMyApplicationParams.UIMainWindowWindowAlternateExePath))
            {
                  this .UIMap.TestMyApplicationParams.UITbContentEditText = 
                    TestContext.DataRow[  "  input  "  ].ToString();

                  this  .UIMap.TestMyApplication();

                  this .UIMap.ValidateDisplayValueExpectedValues.UIHellocnblogsTextDisplayText= 
                  TestContext.DataRow[  "  output  "  ].ToString();
                  this  .UIMap.ValidateDisplayValue();
            }
        } 

  4. 运行测试,程序将自动打开,自动根据CSV文件中的数据进行输入和验证:

  

PS:VS2010 的版本必须是 Premium版 或者 Ultimate版; 文中如有问题,请帮忙指出; 各位大牛如果有更好的方法测试应用程序请赐教!

作者: 孜孜不倦的程序员  
出处: http://sirkevin.cnblogs.com/  
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

分类:  .NET知识 ,  开发工具

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于使用Visual Studio 2010进行UI自动化测试的详细内容...

  阅读:48次