柚子快報(bào)激活碼778899分享:C#實(shí)現(xiàn)微信消息自動(dòng)發(fā)送
柚子快報(bào)激活碼778899分享:C#實(shí)現(xiàn)微信消息自動(dòng)發(fā)送
群發(fā)消息
C#實(shí)現(xiàn)微信消息自動(dòng)發(fā)送
微信UI自動(dòng)化測(cè)試的意義主要體現(xiàn)在以下幾個(gè)方面: 1.降低人為錯(cuò)誤:由于自動(dòng)化測(cè)試是由代碼驅(qū)動(dòng)的,因此可以避免人為錯(cuò)誤,如遺漏、誤操作等。 2.節(jié)省人力資源:通過(guò)自動(dòng)化消息發(fā)送,特別是在夜間或非工作時(shí)間進(jìn)行測(cè)試時(shí),自動(dòng)化發(fā)送消息可以替代部分人力。 總的來(lái)說(shuō),微信UI自動(dòng)化測(cè)試能夠大大提升效率和準(zhǔn)確性,作為 Microsoft 支持的庫(kù),F(xiàn)laUIAutomation 與 Windows 和其他 Microsoft 技術(shù)緊密集成,提供了更好的穩(wěn)定性和支持。我通過(guò)近幾周的研究,最終選擇了FlaUI.UIA3來(lái)開(kāi)發(fā)。
使用FlaUI
FlaUI.UIA3 是一個(gè)用于自動(dòng)化測(cè)試 Windows 應(yīng)用程序的庫(kù),基于微軟的本地 UI 自動(dòng)化庫(kù)。以下是 FlaUI.UIA3 的主要特性:
廣泛的適用性 :FlaUI.UIA3 支持多種 Windows 應(yīng)用程序,包括 Win32、WinForms、WPF 和 Windows 商店應(yīng)用。;基于微軟的本地 UI 自動(dòng)化庫(kù): FlaUI.UIA3 是基于微軟的本地 UI 自動(dòng)化庫(kù)構(gòu)建的,因此它提供了一種圍繞這些庫(kù)的包裝;最新的版本: UIA3 是 UI 自動(dòng)化庫(kù)的最新版本,專(zhuān)門(mén)適用于 WPF 和 Windows 商店應(yīng)用程序; 4.可擴(kuò)展性:雖然 FlaUI.UIA3 提供了大量的自動(dòng)化功能,但在遇到特殊需求時(shí),它還提供了本機(jī)對(duì)象來(lái)滿(mǎn)足更復(fù)雜的需求;提供干凈的代碼庫(kù):FlaUI.UIA3 從頭開(kāi)始重寫(xiě),旨在提供一個(gè)干凈的代碼庫(kù),以便于協(xié)作和進(jìn)一步的開(kāi)發(fā);易于使用:開(kāi)發(fā)者可以在 FlaUI 和 UIA3 之間進(jìn)行選擇,以適應(yīng)他們的測(cè)試需求;增加了 焦點(diǎn)寫(xiě)作模式、預(yù)覽模式、簡(jiǎn)潔寫(xiě)作模式、左右區(qū)域同步滾輪設(shè)置 等功能,功能按鈕位于編輯區(qū)域與預(yù)覽區(qū)域中間。
先看截圖
系統(tǒng)邏輯
首先,其中包含了對(duì)Windows API函數(shù)的聲明和導(dǎo)入。Windows API是一組用于與Windows操作系統(tǒng)交互的函數(shù)和數(shù)據(jù)結(jié)構(gòu)。這些API函數(shù)在"user32.dll"等動(dòng)態(tài)鏈接庫(kù)中定義。
下面是每個(gè)導(dǎo)入函數(shù)的簡(jiǎn)單解釋?zhuān)?/p>
SwitchToThisWindow : 這個(gè)函數(shù)用于將一個(gè)窗口帶到前臺(tái)。第一個(gè)參數(shù)hWnd是要帶到前臺(tái)的窗口的句柄,第二個(gè)參數(shù)fAltTab決定是否通過(guò)按Alt+Tab鍵來(lái)切換到這個(gè)窗口。 OpenClipboard : 這個(gè)函數(shù)用于打開(kāi)剪貼板并獲取所有權(quán)。 CloseClipboard : 這個(gè)函數(shù)用于關(guān)閉剪貼板并釋放其所有權(quán)。 EmptyClipboard : 這個(gè)函數(shù)用于清空剪貼板的內(nèi)容。 IsClipboardFormatAvailabl : 這個(gè)函數(shù)用于檢查指定的剪貼板格式是否可用。 GetClipboardData : 這個(gè)函數(shù)用于檢索指定格式的剪貼板數(shù)據(jù)。 SetClipboardData : 這個(gè)函數(shù)用于在剪貼板上設(shè)置指定格式的數(shù)據(jù)。 這些函數(shù)通常用于與剪貼板進(jìn)行交互,例如讀取和寫(xiě)入數(shù)據(jù),以及處理窗口的焦點(diǎn)等。
[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 方法將程序附加到微信進(jìn)程。這里假設(shè) processes 是一個(gè)包含微信進(jìn)程信息的列表,F(xiàn)irst() 方法獲取該列表中的第一個(gè)元素(即微信進(jìn)程),Id 屬性獲取該進(jìn)程的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 程序來(lái)看實(shí)際窗口,控件排列
string target = name;
string sendMsg = xiaoxi;
Process[] processes = Process.GetProcessesByName("WeChat");
if (processes.Count() != 1)
{
Console.WriteLine("微信未啟動(dòng)或啟動(dòng)多個(gè)微信");
}
else
{
//1.附加到微信進(jìn)程
using (var app = Application.Attach(processes.First().Id))
{
using (var automation = new UIA3Automation())
{
//2.獲取主界面
var mainWindow = app.GetMainWindow(automation);
//窗口置頂顯示 避免其它窗口遮擋影響后續(xù)操作
IntPtr handle = processes.First().MainWindowHandle;
SwitchToThisWindow(handle, true); // 激活,顯示在最
Console.WriteLine("獲取主界面");
//3.切換到通訊錄
var childWind = mainWindow.FindChildAt(1).FindChildAt(0);
childWind.DrawHighlight(System.Drawing.Color.Red);
//導(dǎo)航窗口
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("點(diǎn)擊通訊錄");
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($"搜索目標(biāo)對(duì)象:{target}");
//5.找到搜索到的聯(lián)系人,切換到對(duì)話(huà)框
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($"未找到聯(lián)系人:{target}");
Console.WriteLine("發(fā)送消息失敗");
}
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("發(fā)送(S)"));
sendBtn?.DrawHighlight(System.Drawing.Color.Red);
sendBtn?.Click();
Console.WriteLine("發(fā)送完成");
}
}
}
}
}
注意:群聊需要保存到通訊錄,才能被獲取。 簡(jiǎn)單錄制了個(gè)屏幕:QQ群發(fā) 微信:微信群發(fā)
柚子快報(bào)激活碼778899分享:C#實(shí)現(xiàn)微信消息自動(dòng)發(fā)送
推薦文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。