zoukankan      html  css  js  c++  java
  • 使用UI Automation实现自动化测试--2

    1. 首先建立一个待测试的winform程序,即UI Automation的服务端。

    下面是button事件处理程序。

     private void CalculateButton_Click(object sender, EventArgs e)
            {
                int num1 = 0;
                int num2 = 0;
                if (!int.TryParse(textBox1.Text.Trim(), out num1) || !int.TryParse(textBox2.Text.Trim(), out num2))
                {
                    MessageBox.Show("Please input a correct number!");
                    return;
                }
                textBox3.Text = (num1 + num2).ToString();
            }

      2. 建立一个测试程序,做UI Automaion的客户端。

      添加引用:UIAutomationClient.dll 和 UIAutomationTypes.dll

    public static void TestWinFrom(int num1, int num2)
            {
                string winfromPath = @"C:Usersv-lixDocumentsVisual Studio 2010ProjectsUIAutomationDemoWinformForTestinginDebugWinformForTesting.exe";
                Console.WriteLine("Begin WinForm UIAutomation test runn");
                Console.WriteLine("Launching WinFormTest application");
                Process p = Process.Start(winfromPath);
                Thread.Sleep(TimeSpan.FromSeconds(2));
                Console.WriteLine("Get desktop");
                var desktop = AutomationElement.RootElement;
    
                // Method 1 by windowhandle
                AutomationElement testForm = AutomationElement.FromHandle(p.MainWindowHandle);
    
                // Method 2 by name property
                //PropertyCondition proCon = new PropertyCondition(AutomationElement.NameProperty,"TestForm1");
                //testForm = desktop.FindFirst(TreeScope.Children,proCon);
    
                if (testForm == null)
                {
                    Console.WriteLine("Could find testform!");
                    return;
                }
    
                Console.WriteLine("Try to find the button control in testForm");
                AutomationElement button = testForm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Calculate"));
    
                Console.WriteLine("Try to find all the textbox in testform!");
                AutomationElementCollection aeAllTextBoxes = testForm.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
    
                // 控件初始化的顺序是先初始化后添加到控件
                // this.Controls.Add(this.textBox3);                  
                // this.Controls.Add(this.textBox2);
                // this.Controls.Add(this.textBox1);
    
                AutomationElement aeTextBox1 = aeAllTextBoxes[2];
                AutomationElement aeTextBox2 = aeAllTextBoxes[1];
                AutomationElement aeTextBox3 = aeAllTextBoxes[0];
    
                Console.WriteLine("Settiing input to '30'");
                //set textboxs' value by ValuePattern
                ValuePattern vpTextBox1 = (ValuePattern)aeTextBox1.GetCurrentPattern(ValuePattern.Pattern);
                vpTextBox1.SetValue(num1.ToString());
                ValuePattern vpTextBox2 = (ValuePattern)aeTextBox2.GetCurrentPattern(ValuePattern.Pattern);
                vpTextBox2.SetValue(num2.ToString());
                Thread.Sleep(150);
                Console.WriteLine("Clickinig on button1 Button.");
                InvokePattern ipButton = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);
                ipButton.Invoke();
                Thread.Sleep(1000);
    
                ValuePattern vpTextBox3 = (ValuePattern)aeTextBox3.GetCurrentPattern(ValuePattern.Pattern);
                if (int.Parse(vpTextBox3.Current.Value.Trim()) != (num1 + num2))
                {
                    Console.WriteLine("Failed");
                }
                else
                {
                    Console.WriteLine("Pass");
                }
    
                Thread.Sleep(5000);
                //close testform
                WindowPattern wpCloseForm = (WindowPattern)testForm.GetCurrentPattern(WindowPattern.Pattern);
                wpCloseForm.Close();
            }
  • 相关阅读:
    Oracle GoldenGate for Sql Server连接ODBC失败的处理方法
    cordova 打包出现transformClassesWithDexForDebug一类错误的解决办法
    mac下cordova的ios-deploy安装问题
    c#截取后台窗口的图片
    命令行--调用ORACLE的SQL语句
    SQL-SERVER 数据库备份 ftp 传到远程服务器 加 restore 数据库 一条龙 附源码
    google map
    怎么根据所给url进行屏幕截屏并保存为图片
    jquery mobile
    研究领域总结(二):稀疏——矩阵补全
  • 原文地址:https://www.cnblogs.com/Linford-Xu/p/3191537.html
Copyright © 2011-2022 走看看