我是C#的新手。我必须编写一个代码,使应用程序使用C#自动运行(完全UI自动化)。以下是我编写的代码。
string file = textBox1.Text;
string plnt_name = "Testing_Kabir_Plant";
string sspe = "1CK1S01";
string srev = "0";
string spe = sspe + ", Rev:" + srev;
ProcessStartInfo pro = new ProcessStartInfo(file);
Process.Start(pro);
Thread.Sleep(5000);
AutomationElement RestoreTool = AutomationElement.RootElement.FindFirst(TreeScope.Descendants
, new PropertyCondition(AutomationElement.AutomationIdProperty, "Form1"));
Thread.Sleep(5000);
AutomationElement selPlant = RestoreTool.FindFirst(TreeScope.Descendants
, new PropertyCondition(AutomationElement.AutomationIdProperty, "comboBox1"));
Thread.Sleep(5000);
AutomationElementCollection ciPlants = selPlant.FindAll(TreeScope.Descendants
, new PropertyCondition(AutomationElement.IsSelectionItemPatternAvailableProperty, true));
foreach (AutomationElement aeplant in ciPlants)
{
SelectionItemPattern plant = (SelectionItemPattern)aeplant.GetCurrentPattern(SelectionItemPattern.Pattern);
if (aeplant.Current.Name == plnt_name)
{
try
{
plant.Select();
}
catch (TimeoutException)
{
}
break;
}
}
AutomationElement getbut = RestoreTool.FindFirst(TreeScope.Descendants
, new PropertyCondition(AutomationElement.AutomationIdProperty, "button5"));
((InvokePattern)getbut.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
Thread.Sleep(5000);
AutomationElement speclist = RestoreTool.FindFirst(TreeScope.Descendants
, new PropertyCondition(AutomationElement.AutomationIdProperty, "checkedListBox1"));
AutomationElementCollection list = speclist.FindAll(TreeScope.Descendants
, new PropertyCondition(AutomationElement.IsSelectionItemPatternAvailableProperty, true));
foreach (AutomationElement each in list)
{
if (each.Current.Name == spe)
{
((TogglePattern)each.GetCurrentPattern(TogglePattern.Pattern)).Toggle();
}
}
AutomationElement log_fol = RestoreTool.FindFirst(TreeScope.Descendants
, new PropertyCondition(AutomationElement.AutomationIdProperty, "textBox4"));
((ValuePattern)log_fol.GetCurrentPattern(ValuePattern.Pattern)).SetValue("E:" + @"\sample" + @"\sample" + @"\testing");
Thread.Sleep(2000);
AutomationElement finbut = RestoreTool.FindFirst(TreeScope.Descendants
, new PropertyCondition(AutomationElement.AutomationIdProperty, "button1"));
((InvokePattern)finbut.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
我的工具要求执行应用程序的exe文件,并开始自动给出值的过程。 我面临的问题是,调用时的最后一个按钮“button1”抛出异常“不支持指定的方法”。这个我没有面对“button5”。我不明白为什么。请帮我解决这个问题。
感谢您的帮助。