自动化测试小实例

注:查看窗口句柄工具下载地址:http://download.csdn.net/detail/chao88552828/9364845

 <span style="white-space:pre">	</span>private void button3_Click(object sender, EventArgs e)
        {
            Process.Start(@"calc.exe");

            //获取跟节点
            AutomationElement rootElement = AutomationElement.RootElement;
            //获取计算机窗体节点
            AndCondition conCalc = new AndCondition(
                new PropertyCondition(AutomationElement.ClassNameProperty, "CalcFrame"),
                new PropertyCondition(AutomationElement.NameProperty, "计算器"));
            AutomationElement calcElement = rootElement.FindFirst(TreeScope.Children,conCalc);

            if (calcElement == null)
            {
                MessageBox.Show("计算器未启用");
                return;
            }
            

            //找按钮3
            var btn3 = GetButtonElementByName(calcElement, "3");
            InvokeButton(btn3);
            System.Threading.Thread.Sleep(1000);
            //找按钮+
            var btnPlus = GetButtonElementByName(calcElement, "乘");
            InvokeButton(btnPlus);
            System.Threading.Thread.Sleep(1000);
            //找按钮2
            var btn2 = GetButtonElementByName(calcElement, "2");
            InvokeButton(btn2);
            System.Threading.Thread.Sleep(1000);
            //找按钮=
            var btnEqual = GetButtonElementByName(calcElement, "等于");
            InvokeButton(btnEqual);
            System.Threading.Thread.Sleep(1000);
            //找结果
            var findElements = calcElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "Static"));
            AutomationElement calcResult = findElements[3];
            MessageBox.Show("计算结果:"+GetStaticText(calcResult));
        }

        /// <summary>
        /// 根据名称查找按钮
        /// </summary>
        /// <param name="parentElement">父窗体句柄</param>
        /// <param name="elName"></param>
        /// <returns></returns>
        private AutomationElement GetButtonElementByName(AutomationElement parentElement,string elName)
        {
            AndCondition con =new AndCondition(
                new PropertyCondition(AutomationElement.ClassNameProperty, "Button"),
                new PropertyCondition(AutomationElement.NameProperty, elName));

            AutomationElement findElement = parentElement.FindFirst(TreeScope.Descendants, con);
            if (findElement == null)
                throw new Exception("未找到按钮"+elName);
            return findElement;
        }

        /// <summary>
        /// 触发按钮事件
        /// </summary>
        /// <param name="btnElement"></param>
        private void InvokeButton(AutomationElement btnElement)
        {
            if (btnElement == null)
                return;
            InvokePattern btnInvoke = (InvokePattern)btnElement.GetCurrentPattern(InvokePattern.Pattern);
            btnInvoke.Invoke();
        }

        /// <summary>
        /// 设置文本的值
        /// </summary>
        /// <param name="txtElement"></param>
        /// <param name="setValue"></param>
        private void SetValue(AutomationElement txtElement,string setValue)
        {
            if (txtElement == null)
                return;
            ValuePattern txtValue = (ValuePattern)txtElement.GetCurrentPattern(ValuePattern.Pattern);
            txtValue.SetValue(setValue);
        }
        
        /// <summary>
        /// 获取文本的值
        /// </summary>
        /// <param name="txtElement"></param>
        /// <returns></returns>
        private string GetText(AutomationElement txtElement)
        {
            if (txtElement == null)
                return "";
            TextPattern txtText = (TextPattern)txtElement.GetCurrentPattern(TextPattern.Pattern);
            string strText = txtText.DocumentRange.GetText(-1);
            return strText;
        }

        /// <summary>
        /// 获取静态类的值
        /// </summary>
        /// <param name="staticElement"></param>
        /// <returns></returns>
        private string GetStaticText(AutomationElement staticElement)
        {
            if (staticElement == null)
                return "";
            return staticElement.Current.Name;
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值