FlaUI 操作微信发消息

发布时间 2023-03-27 20:22:35作者: 阿东123

FlaUI 操作微信发消息

 

源代码名称:FlaUI
源代码网址:http://www.github.com/Roemer/FlaUI
FlaUI源代码文档
FlaUI源代码下载
Git URL:
复制代码

git://www.github.com/Roemer/FlaUI.git

 


Git Clone代码到本地:
复制代码

git clone http://www.github.com/Roemer/FlaUI

 

 

简介
FlaUI是一个. NET 库,它帮助自动测试 Windows 应用程序( WIN32,WinForms,WPF,商店应用,。)。
它基于微软的本地UI自动化库,因此它是一种围绕它们的包装。
虽然FlaUI几乎涵盖了UI自动化库,但在遇到特殊需求时还提供了本机对象,这是由所没有的。
从UIAComWrapper项目或者 TestStack.White 复制一些想法,但从头开始重写以得到干净的代码库。

这里有相当多的自动化解决方案。 像 TestComplete。Ranorex。CodedUI这样的商业类只是为了。 而且免费的也是 TestStack.White.

所有这些都是基于微软提供的。 这些是UI自动化库。 它有三个版本:

MSAA
MSAA非常过时,我们将跳过它( 有些类似CodedUI仍然使用它)
UIA2: 用户界面自动化的托管库
UIA2只对 C# 进行管理,这对于很好,但它不再维护,而且不支持新功能,也不支持,也不支持Store应用。
UIA3: UI自动化库
UIA3是最新的版本,但仍然是实际版本( 而且应该保持)。 这种方法适用于 wpf/Windows 商店应用程序,但不幸的是,它可以拥有一些与WinForm应用程序不存在的Bug,在中不存在。
所以,商业解决方案主要基于这些和/或者实现大量解决方案代码来解决这些问题。 TestStack.White 有两个版本,一个用于 UIA2,一个用于 UIA3,但是因为旧的代码库很难使UIA3工作。 为此,它还使用附加库,UIAComWrapper使用与托管UIA2相同的命名,并将 UIA3 com与它们包装成一个库。 FlaUI现在试图为UIA2和UIA3提供一个接口,开发者可以在那里选择它,他想使用哪个版本。 它还应该提供一个非常干净和现代的代码库,以便协作和进一步的开发尽可能容易。

 

用法

安装

要使用 FlaUI,你需要引用适当的程序集。 如果你想使用UIA2或者UIA3并从NuGet安装适当的库,那么你应该决定。 当然,你当然可以下载源代码并自己编译。

在代码中的用法

入口点通常是一个应用程序或者桌面,因此你可以获得一个自动化元素( 就像应用程序的主窗口)。 然后,你可以搜索子元素并与它们进行交互。 有一个 helper 类可以启动,附加或者关闭应用程序。 因为应用程序不与任何UIA库相关,所以需要创建自动化并使用它来获得第一个元素。

复制代码
using FlaUI.UIA3;

var app = FlaUI.Core.Application.Launch("notepad.exe");
using (var automation = new UIA3Automation())
{
    var window = app.GetMainWindow(automation);
    Console.WriteLine(window.Title);
    ...
}
复制代码
复制代码
using FlaUI.Core.AutomationElements;
using FlaUI.UIA3;

// Note: Works only pre-Windows 8 with the legacy calculator
var app = FlaUI.Core.Application.Launch("calc.exe");
using (var automation = new UIA3Automation())
{
    var window = app.GetMainWindow(automation);
    var button1 = window.FindFirstDescendant(cf => cf.ByText("1"))?.AsButton();
    button1?.Invoke();
    ...
}
复制代码

 

正文

 

微信操作发消息。

不要用NUGET安装FlaUI,不然会各种报错。 在Github 下载源码,用visual studio 2019 打开,等待解决方案加载完毕。

 

 

 解决方案上点击右键,生成解决方案,等待项目编译完成。 

查看 目录 FlaUI.UIA3\bin\Debug

 

 

 编译出来对应的.NET 版本.

这里 我们在FlaUI解决方案里面新建一个项目,新建完成后,添加 FlaUI 引用。我选的.NET版本是4.5 就引用 FlaUI.Core.dll,FlaUI.UIA3.dll 两个文件。

 

 

微信群发 

复制代码
                Process[] processes = Process.GetProcessesByName("WeChat");
                if (processes.Count() != 1)
                {
                    Console.WriteLine("微信未启动或启动多个微信");
                }
                else
                {
                    //1.附加到微信进程
                    using (var app = FlaUI.Core.Application.Attach(processes.First().Id))
                    {
                        using (var automation = new UIA3Automation())
                        {
                            //2.获取主界面
                            var mainWindow = app.GetMainWindow(automation);
                            Console.WriteLine("获取主界面");
                            //3.切换到聊天目录
                            var elements = mainWindow.FindAll(TreeScope.Subtree, TrueCondition.Default);
                            var addressBook = mainWindow.FindFirstDescendant(cf => cf.ByName("聊天"));
                            addressBook.DrawHighlight(System.Drawing.Color.Red);
                            var path = FlaUI.Core.Debug.GetXPathToElement(addressBook);
                            addressBook.Click();
                            Console.WriteLine("切换到聊天");
                            Thread.Sleep(2000);
                            //4.获取聊天列表
                            //只发前六个
                            var count = 0;
                            var searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("会话")).AsListBoxItem();
                            while (searchTextBox != null)
                            {
                                var list = searchTextBox.FindAllChildren();
                                foreach (var item in list)
                                {
                                    count++;
                                    var name = item.Name;
                                    item.Click();
                                    var type = item.ControlType;
                                    item.DrawHighlight(System.Drawing.Color.Red);
                                    var MsgSend = mainWindow.FindFirstDescendant(cf => cf.ByName("输入")).AsTextBox();
                                    var MsgSendButton = mainWindow.FindFirstDescendant(cf => cf.ByName("sendBtn"));
                                    if (MsgSend != null && MsgSendButton != null)
                                    {
                                        MsgSend.Click();
                                        System.Windows.Forms.Clipboard.SetText($"群发消息,请忽略:{DateTime.Now}");
                                        Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });
                                        MsgSendButton.Click();
                                        Console.WriteLine($"发送信息:{name}");
                                        Thread.Sleep(500);
                                    }
                                    if (count == 6)
                                    {
                                        break;
                                    }
                                }
                                if (count == 6)
                                {
                                    break;
                                }
                                for (int i = 0; i < list.Length; i++)
                                {
                                    searchTextBox.Focus();
                                    Keyboard.Press(VirtualKeyShort.DOWN);
                                    Thread.Sleep(100);
                                }
                                searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("会话")).AsListBoxItem();
                                Thread.Sleep(2000);
                            }
                        }
                    }
                }
复制代码

 

 

微信单独发送测试(发送到文件助手)

 

复制代码
 Process[] processes = Process.GetProcessesByName("WeChat");
            if (processes.Count() != 1)
            {
                Console.WriteLine("微信未启动或启动多个微信");
            }
            else
            {
                //1.附加到微信进程
                using (var app = FlaUI.Core.Application.Attach(processes.First().Id))
                {
                    using (var automation = new UIA3Automation())
                    {
                        //2.获取主界面
                        var mainWindow = app.GetMainWindow(automation);
                        Console.WriteLine("获取主界面");
                        //3.切换到通讯录
                        var elements = mainWindow.FindAll(FlaUI.Core.Definitions.TreeScope.Subtree, TrueCondition.Default);
                        var addressBook = mainWindow.FindFirstDescendant(cf => cf.ByName("通讯录"));
                        addressBook.DrawHighlight(System.Drawing.Color.Red);
                        var path = FlaUI.Core.Debug.GetXPathToElement(addressBook);
                        Console.WriteLine("点击通讯录");
                        addressBook.Click();

                        //4.搜索
                        string target = "文件传输助手";
                        var searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("搜索")).AsTextBox();
                        searchTextBox.Click();
                        Keyboard.Type(target);
                        Keyboard.Type(VirtualKeyShort.RETURN);
                        Console.WriteLine("搜索目标对象");

                        //5.切换到对话框
                        Thread.Sleep(500);

                        var searchList = mainWindow.FindFirstDescendant(cf => cf.ByName("搜索结果"));
                        if (searchList != null)
                        {
                            var searchItem = searchList.FindAllDescendants().FirstOrDefault(cf => cf.Name == target && cf.ControlType == FlaUI.Core.Definitions.ControlType.ListItem);
                            searchItem?.DrawHighlight(System.Drawing.Color.Red);
                            searchItem?.AsListBoxItem().Click();
                        }
                        else
                        {
                            Console.WriteLine("没有搜索到内容");
                        }
                        Thread.Sleep(500);
                        //6.输入文本
                        string sendMsg = "这个是我微信的输入信息:" + DateTime.Now.ToString();
                        var msgInput = mainWindow.FindFirstDescendant(cf => cf.ByName("输入")).AsTextBox();
                        msgInput?.Click();
                        System.Windows.Forms.Clipboard.SetText(sendMsg);
                        Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });
                        var sendBtn = mainWindow.FindFirstDescendant(cf => cf.ByName("sendBtn"));
                        sendBtn?.DrawHighlight(System.Drawing.Color.Red);
                        sendBtn?.Click();
                    }
                }
            }
复制代码