【工作经历:阿里巴巴搜索技术研发中心QA ,百度新产品测试部QA】 【领域:测试分析,自动化测试,性能测试,安全测试 】 【个人定位:高级测试工程师+培训师+领域产品专家】

操纵遨游工具栏的方法,解决spy++无法捕获控件的问题

上一篇 / 下一篇  2008-07-31 17:03:41 / 个人分类:自动化测试

这个问题,相信很多从事自动化测试的人都会遇到。

我初步想到的两个方法。经过实践已经可以了。
这个问题算是可以放下了。
以后开始主攻qtp。

1、利用位置。工具栏的整个子窗口的绝对位置是可以得到的。里面的按钮大小都是相差无几的。所以,可以计算出里面的每个按钮的大体位置,发送一个点击消息,填充消息内容就可以了。


2、利用msaa机制。
利用标题,定位到遨游浏览器,以为工具栏的类名和其他控件的类名一样,所以,不能单独的通过FindWindowEx 来找到子窗口的句柄。(即使提供了标题参数,依然是定位不到,这个很奇怪,我没有深究,可能其他的api可以搞定)(搜索了一下资料,才发现原来中间还有窗口嵌套,FindWindowEx只能搜索子结点,不能搜索孙子节点)。我采用了EnumChildWindows函数。遍历里面的子窗体,根据属性判断出工具栏的窗体句柄。然后利用msaa的api得到msaa对象。枚举他的孩子对象,就可以得到所有的按钮的msaa对象了。

然后利用msaa的机制,就可以操纵工具栏了。

发送消息请参考
http://www.cnblogs.com/karoc/archive/2006/11/29/576253.aspx



以下是查找的关键代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using Accessibility;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int hWnd = FindWindowEx(0, 0, "Maxthon2_Frame", null);
            EnumChildWindows(hWnd, callBackEnumChildWindows, 0);
            Console.ReadKey();
        }

        [DllImport("oleacc", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int AccessibleChildren(IAccessible paccContainer, int iChildStart, int cChildren, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] object[] rgvarChildren, ref int pcObtained);

        [DllImport("oleacc.dll")]
        internal static extern int AccessibleObjectFromWindow(
                      IntPtr hwnd,
                      uint id,
                      ref Guid iid,
                      [In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object ppvObject);

        internal enum OBJID : uint
        {
            WINDOW = 0x00000000,
            SYSMENU = 0xFFFFFFFF,
            TITLEBAR = 0xFFFFFFFE,
            MENU = 0xFFFFFFFD,
            CLIENT = 0xFFFFFFFC,
            VSCROLL = 0xFFFFFFFB,
            HSCROLL = 0xFFFFFFFA,
            SIZEGRIP = 0xFFFFFFF9,
            CARET = 0xFFFFFFF8,
            CURSOR = 0xFFFFFFF7,
            ALERT = 0xFFFFFFF6,
            SOUND = 0xFFFFFFF5,
        }

        /// <summary>
        /// //////////////////////////////////
        [DllImport("user32.dll")]
        public static extern int FindWindowEx(int hwndParent, int hwndChildAfter,
                                                                           string lpszClass, string lpszWindow);
        [DllImport("user32.dll")]
        public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);

        /// <summary>
        /// 回调函数代理
        /// </summary>
        public delegate bool CallBack(int hwnd, int lParam);

        /// <summary>
        /// 子窗口回调处理函数
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        public static bool ChildWindowProcess(int hwnd, int lParam)
        {
            StringBuilder title = new StringBuilder(200);
            int len;
            len = GetWindowText(hwnd, title, 200);
            if (len < 1)
                return true;
            if (title.ToString().Contains("标准工具栏"))
            {
                Console.WriteLine("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                msaa((IntPtr)hwnd);
                Console.WriteLine("ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
            }
            Console.WriteLine(title + " " + hwnd);
            return true;
        }

        /// <summary>
        /// 子窗口回调函数代理
        /// </summary>
        public static CallBack callBackEnumChildWindows = new CallBack(ChildWindowProcess);

        public static void msaa(IntPtr hwnd2)
        {
            object ōbj = new object();
            Guid guid = new Guid("{618736E0-3C3D-11CF-810C-00AA00389B71}");
            AccessibleObjectFromWindow(hwnd2, (uint)OBJID.WINDOW, ref guid, ref obj);
            IAccessible accessible = obj as IAccessible;
            accessible = accessible as IAccessible;
            Object[] childs = new Object[accessible.accChildCount];
            int ōbtained = 0;
            try
            {
                AccessibleChildren(accessible, 0, accessible.accChildCount, childs, ref obtained);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine(accessible.accChildCount);
            Console.WriteLine(obtained.ToString());

            for (int i = 0; i < obtained; i++)
            {
                IAccessible child = childs[i] as IAccessible;
                Console.WriteLine(child.get_accName(0));               
                Object[] accchilds = new Object[child.accChildCount];
                int result = 0;
                AccessibleChildren(child, 0, child.accChildCount - 1, accchilds, ref result);
                for (int j = 0; j < result; j++)
                {
                    IAccessible jchild = accchilds[j] as IAccessible;
                    Console.WriteLine(jchild.get_accName(0));                    
                }
            }
        }
    }
}





TAG: 自动化测试

 

评分:0

我来说两句

Open Toolbar