您的位置:首页 > 其它

自动化测试小实例

2015-12-17 16:13 369 查看
注:查看窗口句柄工具下载地址:http://download.csdn.net/detail/chao88552828/9364845

<span style="white-space:pre">	</span>private void button3_Click(object sender, EventArgs e)
{
Process.Start(@"calc.exe");

//获取跟节点
AutomationElement rootElement = AutomationElement.RootElement;
//获取计算机窗体节点
AndCondition conCalc = new AndCondition(
new PropertyCondition(AutomationElement.ClassNameProperty, "CalcFrame"),
new PropertyCondition(AutomationElement.NameProperty, "计算器"));
AutomationElement calcElement = rootElement.FindFirst(TreeScope.Children,conCalc);

if (calcElement == null)
{
MessageBox.Show("计算器未启用");
return;
}

//找按钮3
var btn3 = GetButtonElementByName(calcElement, "3");
InvokeButton(btn3);
System.Threading.Thread.Sleep(1000);
//找按钮+
var btnPlus = GetButtonElementByName(calcElement, "乘");
InvokeButton(btnPlus);
System.Threading.Thread.Sleep(1000);
//找按钮2
var btn2 = GetButtonElementByName(calcElement, "2");
InvokeButton(btn2);
System.Threading.Thread.Sleep(1000);
//找按钮=
var btnEqual = GetButtonElementByName(calcElement, "等于");
InvokeButton(btnEqual);
System.Threading.Thread.Sleep(1000);
//找结果
var findElements = calcElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "Static"));
AutomationElement calcResult = findElements[3];
MessageBox.Show("计算结果:"+GetStaticText(calcResult));
}

/// <summary>
/// 根据名称查找按钮
/// </summary>
/// <param name="parentElement">父窗体句柄</param>
/// <param name="elName"></param>
/// <returns></returns>
private AutomationElement GetButtonElementByName(AutomationElement parentElement,string elName)
{
AndCondition con =new AndCondition(
new PropertyCondition(AutomationElement.ClassNameProperty, "Button"),
new PropertyCondition(AutomationElement.NameProperty, elName));

AutomationElement findElement = parentElement.FindFirst(TreeScope.Descendants, con);
if (findElement == null)
throw new Exception("未找到按钮"+elName);
return findElement;
}

/// <summary>
/// 触发按钮事件
/// </summary>
/// <param name="btnElement"></param>
private void InvokeButton(AutomationElement btnElement)
{
if (btnElement == null)
return;
InvokePattern btnInvoke = (InvokePattern)btnElement.GetCurrentPattern(InvokePattern.Pattern);
btnInvoke.Invoke();
}

/// <summary>
/// 设置文本的值
/// </summary>
/// <param name="txtElement"></param>
/// <param name="setValue"></param>
private void SetValue(AutomationElement txtElement,string setValue)
{
if (txtElement == null)
return;
ValuePattern txtValue = (ValuePattern)txtElement.GetCurrentPattern(ValuePattern.Pattern);
txtValue.SetValue(setValue);
}

/// <summary>
/// 获取文本的值
/// </summary>
/// <param name="txtElement"></param>
/// <returns></returns>
private string GetText(AutomationElement txtElement)
{
if (txtElement == null)
return "";
TextPattern txtText = (TextPattern)txtElement.GetCurrentPattern(TextPattern.Pattern);
string strText = txtText.DocumentRange.GetText(-1);
return strText;
}

/// <summary>
/// 获取静态类的值
/// </summary>
/// <param name="staticElement"></param>
/// <returns></returns>
private string GetStaticText(AutomationElement staticElement)
{
if (staticElement == null)
return "";
return staticElement.Current.Name;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: