使用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:\Users\v-lix\Documents\Visual Studio 2010\Projects\UIAutomationDemo\WinformForTesting\bin\Debug\WinformForTesting.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();
        }

 

 

转载于:https://www.cnblogs.com/Linford-Xu/p/3191537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值