早教吧作业答案频道 -->其他-->
从一记事本中复制某一公式到计算器中完成一个小自动化测试,为什么下面的代码总是报错呢?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);
}
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 =
VERSION 5.00
Begin VB.Form frmMain
Appearance = 0 'Flat
BackColor =
看了 从一记事本中复制某一公式到计...的网友还看了以下:
对读入.txt中,拆出英文单词,输出顺序排列的单词表,输出output.txt中,一个一行,单词后 2020-05-15 …
如何利用利用dir/a-d/b/s从指定目录搜索并拷贝经高手指点完成了如下代码,想从指定的txt中 2020-05-23 …
使用bat删除txt中指定地方的内容在a.txt中只有一个ABC和一个CBA,文本中的内容为“.. 2020-06-18 …
python文本处理问题有一个文本a.txt,中间有10行12345678910现在要求将中间的4 2020-07-17 …
用VB修改TXT文件中的数值A.TXT中写的AtestBtencentCtravelerD1234 2020-07-23 …
关于批处理的for命令,为何完全没法用?假如文件a.txt中有如下内容:第1行第1列第1行第2列第 2020-07-25 …
用c++编写电文加密系统题目说明:从一个文件aa.txt里读取中文,要求将原文按下列规则译成密文,并 2020-11-07 …
java读取txt中的两点坐标计算距离有一txt设为xyz.txt,里面含有A,B的xyz坐标,问要 2020-11-07 …
QT逐行读取txt中的数据,并保存到数组中有txt按行记录部分数据如:12.345.678.9... 2020-12-28 …
Linux题目文件pc.txt中保存了某班学生的数学成绩第一项S*为学生学号试编写一段shell程序 2021-01-02 …