早教吧 育儿知识 作业答案 考试题库 百科 知识分享

从一记事本中复制某一公式到计算器中完成一个小自动化测试,为什么下面的代码总是报错呢?startInfo=newProcessStartInfo(@"C:\Users\zou\Desktop\formula.txt");startInfo.WindowStyle=ProcessWindowStyle.Normal;startInf

题目详情
从一记事本中复制某一公式到计算器中完成一个小自动化测试,为什么下面的代码总是报错呢?
startInfo = new ProcessStartInfo(@"C:\Users\zou\Desktop\formula.txt");
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.UseShellExecute = true;
process = Process.Start(startInfo);
Thread.Sleep(1000);
windowHandle = process.MainWindowHandle;
AutomationElement notepod = AutomationElement.FromHandle(windowHandle);
string EditMenuAutomationId = "Item 2";
propertyCondition = new PropertyCondition(AutomationElement.AutomationIdProperty,EditMenuAutomationId);
editMenu = notepod.FindFirst(TreeScope.Element | TreeScope.Descendants,propertyCondition);
if (editMenu == null)
{
Console.WriteLine(EditMenuAutomationId.ToString() + " could not be found.");
return;
}
if ((bool)editMenu.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
{
Console.WriteLine("Element not enabled.");
return;
}
expandEdit = editMenu.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
expandEdit.Expand();
//select all
AutomationElement automationelementname1 = notepod.FindFirst(TreeScope.Element | TreeScope.Descendants,propertyCondition);
program.ButtonClick(automationelementname1,"Item 25");
//copy
expandEdit = editMenu.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
expandEdit.Expand();
program.ButtonClick(automationelementname1,"Item 769");
process.Close();
调用的方法:
public void ButtonClick(AutomationElement AutomationElementname,string AutomationId)
{
propertyCondition = new PropertyCondition(AutomationElement.AutomationIdProperty,AutomationId);
AutomationElementname = calculator.FindFirst(TreeScope.Element | TreeScope.Descendants,propertyCondition);
if (AutomationElementname == null)
{
Console.WriteLine(AutomationId.ToString() + " could not be found.");
return;
}
if ((bool)AutomationElementname.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
{
Console.WriteLine("Element not enabled.");
return;
}
invokePattern = AutomationElementname.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invokePattern.Invoke();
Thread.Sleep(1000);
}
▼优质解答
答案和解析
用记事本把这段代码复制过去,然后另存为.frm就可以了
VERSION 5.00
Begin VB.Form frmMain
Appearance = 0 'Flat
BackColor =