赞
踩
群发消息
微信UI自动化测试的意义主要体现在以下几个方面:
1.降低人为错误:由于自动化测试是由代码驱动的,因此可以避免人为错误,如遗漏、误操作等。
2.节省人力资源:通过自动化消息发送,特别是在夜间或非工作时间进行测试时,自动化发送消息可以替代部分人力。
总的来说,微信UI自动化测试能够大大提升效率和准确性,作为 Microsoft 支持的库,FlaUIAutomation 与 Windows 和其他 Microsoft 技术紧密集成,提供了更好的稳定性和支持。我通过近几周的研究,最终选择了FlaUI.UIA3来开发。
FlaUI.UIA3 是一个用于自动化测试 Windows 应用程序的库,基于微软的本地 UI 自动化库。以下是 FlaUI.UIA3 的主要特性:
首先,其中包含了对Windows API函数的声明和导入。Windows API是一组用于与Windows操作系统交互的函数和数据结构。这些API函数在"user32.dll"等动态链接库中定义。
下面是每个导入函数的简单解释:
SwitchToThisWindow
: 这个函数用于将一个窗口带到前台。第一个参数hWnd是要带到前台的窗口的句柄,第二个参数fAltTab决定是否通过按Alt+Tab键来切换到这个窗口。
OpenClipboard
: 这个函数用于打开剪贴板并获取所有权。
CloseClipboard
: 这个函数用于关闭剪贴板并释放其所有权。
EmptyClipboard
: 这个函数用于清空剪贴板的内容。
IsClipboardFormatAvailabl
: 这个函数用于检查指定的剪贴板格式是否可用。
GetClipboardData
: 这个函数用于检索指定格式的剪贴板数据。
SetClipboardData
: 这个函数用于在剪贴板上设置指定格式的数据。
这些函数通常用于与剪贴板进行交互,例如读取和写入数据,以及处理窗口的焦点等。
[DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern bool SwitchToThisWindow(IntPtr hWnd, bool fAltTab); [DllImport("User32")] public static extern bool OpenClipboard(IntPtr hWndNewOwner); [DllImport("User32")] public static extern bool CloseClipboard(); [DllImport("User32")] public static extern bool EmptyClipboard(); [DllImport("User32")] public static extern bool IsClipboardFormatAvailable(int format); [DllImport("User32")] public static extern IntPtr GetClipboardData(int uFormat); [DllImport("User32", CharSet = CharSet.Unicode)] public static extern IntPtr SetClipboardData(int uFormat, IntPtr hMem);
使用 Application.Attach 方法将程序附加到微信进程。这里假设 processes 是一个包含微信进程信息的列表,First() 方法获取该列表中的第一个元素(即微信进程),Id 属性获取该进程的ID。
private static Process[] processes = Process.GetProcessesByName("WeChat");
public static Window WeChatWindow;
static FlaUIAutomation()
{
using (var app = Application.Attach(processes.First().Id))
{
using (var automation = new UIA3Automation())
{
WeChatWindow = app.GetMainWindow(automation);
}
}
}
public void method(string name,string xiaoxi) { //微信主界面窗口从左向右分3分窗口,可以使用inspect.exe 程序来看实际窗口,控件排列 string target = name; string sendMsg = xiaoxi; Process[] processes = Process.GetProcessesByName("WeChat"); if (processes.Count() != 1) { Console.WriteLine("微信未启动或启动多个微信"); } else { //1.附加到微信进程 using (var app = Application.Attach(processes.First().Id)) { using (var automation = new UIA3Automation()) { //2.获取主界面 var mainWindow = app.GetMainWindow(automation); //窗口置顶显示 避免其它窗口遮挡影响后续操作 IntPtr handle = processes.First().MainWindowHandle; SwitchToThisWindow(handle, true); // 激活,显示在最 Console.WriteLine("获取主界面"); //3.切换到通讯录 var childWind = mainWindow.FindChildAt(1).FindChildAt(0); childWind.DrawHighlight(System.Drawing.Color.Red); //导航窗口 var navWind = childWind.FindChildAt(0); //窗口第一部分 navWind.DrawHighlight(System.Drawing.Color.Red); var addressBook = navWind.FindFirstDescendant(cf => cf.ByName("通讯录")); addressBook.DrawHighlight(System.Drawing.Color.Red); Console.WriteLine("点击通讯录"); addressBook.Click(); //4.搜索 var secondChild = childWind.FindChildAt(1); //窗口第二部分 secondChild.DrawHighlight(System.Drawing.Color.Red); var searchTextBox = secondChild.FindFirstDescendant(cf => cf.ByName("搜索")).AsTextBox(); searchTextBox.DrawHighlight(System.Drawing.Color.Red); searchTextBox.Click(); Keyboard.Type(target); Keyboard.Type(VirtualKeyShort.RETURN); Console.WriteLine($"搜索目标对象:{target}"); //5.找到搜索到的联系人,切换到对话框 Thread.Sleep(10); var tempList = secondChild.FindChildAt(1).FindAllDescendants(cf => cf.ByControlType(ControlType.List)); tempList[1].DrawHighlight(System.Drawing.Color.Red); var searchItem = tempList[1].FindAllDescendants(cf => cf.ByControlType(ControlType.ListItem)).FirstOrDefault(cf => cf.Name == target); if (searchItem == null) { Console.WriteLine($"未找到联系人:{target}"); Console.WriteLine("发送消息失败"); } else { searchItem.DrawHighlight(System.Drawing.Color.Red); searchItem.Click(); Thread.Sleep(10); //6.输入文本 var lastChild = childWind.FindChildAt(2); //窗口第三部分 lastChild.DrawHighlight(System.Drawing.Color.Red); var msgInput = lastChild.FindAllDescendants(cf => cf.ByControlType(ControlType.Edit)).First(); msgInput.DrawHighlight(System.Drawing.Color.Red); msgInput?.Click(); SetText(sendMsg); Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V }); var sendBtn = lastChild.FindFirstDescendant(cf => cf.ByName("发送(S)")); sendBtn?.DrawHighlight(System.Drawing.Color.Red); sendBtn?.Click(); Console.WriteLine("发送完成"); } } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。