C#UI自动化实现微信自动搜索聊天文件并发送

用到了两个程序包,FlaUI .Core,FlaUI.UIA3,在VS的扩展里可以下载安装这两个程序包,UI自动化是用来实现自动测试程序流程的,减少人工测试的成本。有需要源程序的私信我。

获取微信窗口句柄 

void GetWxHandle()
        {
            var process = Process.GetProcessesByName("Wechat").FirstOrDefault();
            if (process != null)
            {
                ProcessId = process.Id;
            }

        }

检测微信是否登录,并激活微信聊天窗 

 

bool InitWechat()
{
            //根据微信进程ID绑定FLAUI
            Application = FlaUI.Core.Application.Attach(ProcessId);
            if (Application.HasExited)
            {
                MessageBox.Show("请登录微信!");
                return false;
            }
            var automation = new UIA3Automation();
            var windows= Application.GetAllTopLevelWindows(automation);
            //获取微信window自动化操作对象
          
            if (windows.Count()== 0)
            {
                MessageBox.Show("请打开聊天窗口!");
                return false;  
            }
            else
            {
                wxWindow = Application.GetMainWindow(automation);
                if (wxWindow.AsWindow().Patterns.Window.PatternOrDefault != null)
                {
                    //将微信窗体设置为默认焦点状态
                    wxWindow.AsWindow().Patterns.Window.Pattern.SetWindowVisualState(FlaUI.Core.Definitions.WindowVisualState.Normal);
                }
                return true;
            }
         
 }

点击发送按钮,自动搜索聊天记录,并转发给最新发送的人 

  private async void  button1_Click(object sender, EventArgs e)
        {
            var path = textBox1.Text;
            var sendMsg = path.Substring(path.LastIndexOf("\\") + 1, path.Length-path.LastIndexOf("\\")-1);
            var itemName = sendMsg;
            if( !InitWechat())
            {
               return;
            }
                
            if (wxWindow != null)
            {
                if (wxWindow.AsWindow().Patterns.Window.PatternOrDefault != null)
                {
                    //将微信窗体设置为默认焦点状态
                    wxWindow.AsWindow().Patterns.Window.Pattern.SetWindowVisualState(FlaUI.Core.Definitions.WindowVisualState.Normal);
                }
            }
            //await Task.Delay(100);
            var search = wxWindow.FindAllDescendants().FirstOrDefault(s => s.Name == "搜索");
            var sync = SynchronizationContext.Current;
            search.FocusNative();
            search.Focus();
            search.Click();
        
            var text = wxWindow.FindAllDescendants().FirstOrDefault(s => s.Name == "搜索").Parent;
            if (text != null)
            {
                //await Task.Delay(100);
                var txt = text.FindAllChildren().FirstOrDefault(s => s.ControlType == ControlType.Text).AsTextBox();
                txt.Text = itemName;
                await Task.Delay(500);
                var item = wxWindow.FindAllDescendants().ToList();
                wxWindow.FocusNative();
                if (item != null && item.Count > 0 && !string.IsNullOrWhiteSpace(sendMsg))
                {
                    var first =item.FirstOrDefault(s=>s.Parent != null && s.Parent.Name.Contains("@str:IDS_FAV_SEARCH_RESULT")&&!s.Name.Contains("搜索网络结果"));
                    first.FocusNative();
                    first.Focus();
                    first.Click();
                    var automation = new UIA3Automation();
                    var firstWindow= Application.GetAllTopLevelWindows(automation).FirstOrDefault();
                    var childen= firstWindow.FindAllDescendants().Where(t=>t.ControlType==ControlType.Button&&t.Name=="进入聊天").FirstOrDefault();
                    childen.Click();

                    var msgBox = wxWindow.FindFirstDescendant(x => x.ByControlType(FlaUI.Core.Definitions.ControlType.Text)).AsTextBox();
                    msgBox.Text = "";

                    var button = wxWindow.FindAllDescendants().Where(s => s.ControlType == ControlType.Button && s.Name == "发送文件").FirstOrDefault();
                    button?.Click();
                    var fileWindow = Application.GetAllTopLevelWindows(automation).FirstOrDefault();
                    var fileChileWindows = fileWindow.FindAllDescendants();
                    var fileText = fileChileWindows.Where(t => t.ControlType == ControlType.Edit&&t.Name=="文件名(N):" ).FirstOrDefault();
                    var fileButton = fileChileWindows.Where(t=>t.ControlType==ControlType.SplitButton&&t.Name=="打开(O)").FirstOrDefault();
                    fileText.AsComboBox().Value=  path.Substring(0,path.LastIndexOf("."));
                    fileButton?.Click();
                    await Task.Delay(500);
                    var mainChileWindow = Application.GetAllTopLevelWindows(automation).FirstOrDefault();

                   var sendWindow= mainChileWindow.FindAllChildren().FirstOrDefault();
                    var sendButton= sendWindow.FindAllDescendants().Where(t=>t.ControlType==ControlType.Button&&t.Name.Contains("发送")).FirstOrDefault();
                    sendButton?.Click();
                    firstWindow.Close();

              
                }
            }
            ChatListTokenSource.Cancel();
        }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曹大师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值