一.界面的自動化操作
.Ui自動化測試
.軟件外掛
二.Win32基礎知識
a.Win32中一切元素皆窗口,窗口之間有父子關系。整個桌面是一個“根窗口”。
b.進程:
根據進程id拿到進程對象Process process = Process.GetProcessById(processId);
啟動一個進程:Process process = Process.Start(exe路徑);
殺死一個進程process.Kill()
三.UIAutonation基礎
1、需要添加對UIAutomationClient、 UIAutomationProvider、 UIAutomationTypes的引用
2、AutomationElement.RootElement是窗口根元素
AutomationElement.FromHandle(IntPtr hwnd)從窗口句柄拿到AutomationElement對象。
3、遍歷:
mainElement.FindAll(TreeScope.Descendants,
new PropertyCondition(AutomationElement.ClassNameProperty, "TLabeledEdit"));
TreeScope.Descendants代表遞歸從所有子孫元素中遞歸查找;如果是從直接子節點查找,則使用TreeScope.Children。
Condition是過濾條件,可以根據類名等查找,如果是不指定查詢條件則使用Condition.True Condition。
FindFirst是查到第一個。
4、點擊按鈕、設置文本、讀取文本使用Pattern來實現。不是所有Pattern都支持
1)設置控件的值
ValuePattern
valuePattern = (ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern);
valuePattern.SetValue("rupeng.com");
2)得到文本控件的值
TextPattern
valuePattern = (TextPattern)element.GetCurrentPattern(TextPattern.Pattern);
string v= valuePattern.DocumentRange.GetText(-1);
3)調用控件,比如點擊按鈕
var clickPattern = (InvokePattern)element.GetCurrentPattern(InvokePattern.Pattern);
clickPattern.Invoke();