下載app免費(fèi)領(lǐng)取會(huì)員
網(wǎng)友投稿
更多最近研究自動(dòng)化測(cè)試,看了一下UI Automation的微軟例子,表示太老了,遇到各種問(wèn)題,
UI Spy 好像已經(jīng)被放棄了,可以用inspect.exe來(lái)代替,win10 的路徑為:"C:\Program Files (x86)\Windows Kits\8.1\bin\x64\inspect.exe"
這個(gè)用來(lái)查詢automationId,
官網(wǎng)是以計(jì)算器例子,下面是在win10 修改后能運(yùn)行版本
class CalcAutomationClient
{
AutomationElement calWindow = null;//計(jì)算器窗口主窗口元素
string resultTextAutoID = "CalculatorResults";
string btn5AutoID = "num5Button";
string btn3AutoID = "num3Button";
string btn2AutoID = "num2Button";
string btnPlusAutoID = "plusButton";
string btnSubAutoId = "94";
string btnEqualAutoID = "equalButton";
static void Main(string[] args)
{
CalcAutomationClient autoClient = new CalcAutomationClient();
AutomationEventHandler eventHandler = new AutomationEventHandler(autoClient.OnWindowOpenOrClose);
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, eventHandler);
Process.Start("calc.exe");
Console.ReadLine();
}
private void OnWindowOpenOrClose(object sender, AutomationEventArgs e)
{
if (calWindow != null)
return;
if(e.EventId!=WindowPattern.WindowOpenedEvent)
{
return;
}
if(sender ==null)
{
Console.WriteLine("sender is null");
return;
}
Thread.Sleep(1000);//此處必須等待一下,應(yīng)該是計(jì)算器的等待計(jì)算器完全加載,不然控件 找不到
AutomationElement sourceElement = null;
sourceElement = sender as AutomationElement;
Console.WriteLine(sourceElement.Current.Name);
try
{
sourceElement = sender as AutomationElement;
Console.WriteLine(sourceElement.Current.Name);
if (sourceElement.Current.Name=="計(jì)算器")
{
calWindow = sourceElement;
}
}
catch(Exception ex)
{
Console.WriteLine("ex:" + ex.Message);
return;
}
if(calWindow == null)
{
return;
}
ExcuteTest();
}
private void ExcuteTest()
{
ExcuteButtonInvoke(btn2AutoID);
ExcuteButtonInvoke(btnPlusAutoID);
ExcuteButtonInvoke(btn3AutoID);
ExcuteButtonInvoke(btnEqualAutoID);
string rs = GetCurrentResult();
Console.WriteLine(rs);
}
private void ExcuteButtonInvoke(string automationId)
{
Condition conditions = new AndCondition(
new PropertyCondition(AutomationElement.AutomationIdProperty,automationId),
new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.Button));
if (calWindow == null)
return;
AutomationElementCollection collection = calWindow.FindAll(TreeScope.Descendants, conditions);
if (collection == null || collection.Count == 0)
return;
AutomationElement btn = collection[0];
if (btn != null)
{
InvokePattern invokeptn = (InvokePattern)btn.GetCurrentPattern(InvokePattern.Pattern);
invokeptn.Invoke();
}
Thread.Sleep(1000);
}
private string GetCurrentResult()
{
Condition conditions = new AndCondition(
new PropertyCondition(AutomationElement.AutomationIdProperty, resultTextAutoID),
new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.Text));
AutomationElement text = calWindow.FindAll(TreeScope.Descendants, conditions)[0];
return text.Current.Name;
}
}
本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。
上一篇:二次開(kāi)發(fā)教程:C# 動(dòng)態(tài)生成程序集
推薦專題