我尝试使用 C# 进行一些自动化Microsoft UI 自动化,第一步我尝试使用记事本进行一些自动化。我试图实现的是启动记事本并从随机位置打开 txt 文件。
我已经取得的成就:
- 打开记事本
- 调用(展开(文件菜单并调用(单击("打开..."按钮。 将文件名的
- 路径写入弹出的"打开"对话框的"文件名"文本框中(奇怪的是,我无法在地址栏上调用单击操作,但要处理它,这将是下一个任务(。
程序.cs
using System;
using System.Linq;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using System.Windows.Automation;
using System.Runtime.InteropServices;
namespace TestUIAutomation
{
class Program
{
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);
static void Main(string[] args)
{
Process p = Process.Start(@"notepad.exe");
SetForegroundWindow(p.MainWindowHandle);
Thread.Sleep(1000);
var baseSoftware = AutomationElement.RootElement.FindFirst(TreeScope.Children, new
PropertyCondition(AutomationElement.NameProperty, "Untitled - Notepad"));
var fileButton = baseSoftware.FindFirst(TreeScope.Descendants, new
propertyCondition(AutomationElement.NameProperty, "File"));
var fileButtonExpand = Utilitiy.ExCoPattern(fileButton);
if (fileButtonExpand.Current.ExpandCollapseState != ExpandCollapseState.Expanded)
{
fileButtonExpand.Expand();
}
var openButton = baseSoftware.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Open..."));
Utilitiy.GetInvokePattern(openButton).Invoke();
Thread.Sleep(1000);
var addressBar = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "File name:"));
SendKeys.SendWait(@"C:Usersuser1DocumentsTest.txt");
var findOpenButton = new AndCondition(new PropertyCondition(AutomationElement.NameProperty, "Open"), new PropertyCondition(AutomationElement.AutomationIdProperty, "1"));
var openButtonDialog = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, findOpenButton);
Utilitiy.GetInvokePattern(openButtonDialog);
}
}
}
实用程序 cs
using System;
using System.Windows.Automation;
namespace TestUIAutomation
{
class Utilitiy
{
public static void SetCombobValueByUIA(AutomationElement ctrl, string newValue)
{
ExpandCollapsePattern exPat = ctrl.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
if (exPat == null)
{
throw new ApplicationException("Bad Control type...");
}
exPat.Expand();
AutomationElement itemToSelect = ctrl.FindFirst(TreeScope.Descendants, new
PropertyCondition(AutomationElement.NameProperty, newValue));
SelectionItemPattern sPat = itemToSelect.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
sPat.Select();
}
public static InvokePattern GetInvokePattern(AutomationElement element)
{
return element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
}
public static TogglePattern CheckBoxPattern(AutomationElement element)
{
return element.GetCurrentPattern(TogglePattern.Pattern) as TogglePattern;
}
public static SelectionItemPattern RadioButtonPattern(AutomationElement element)
{
return element.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
}
public static ValuePattern TextValuePattern(AutomationElement element)
{
return element.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
}
public static ExpandCollapsePattern ExCoPattern(AutomationElement element)
{
return element.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
}
}
}
目前的问题我不会收到任何错误消息,因此程序实际上调用了打开按钮,但在现实生活中没有任何反应。 我可以使用发送键方法输入输入来打开文件,但如果可能的话,我喜欢调用打开按钮。
好的,我找到了解决方案,我忘了实际调用按钮。
我从这里更改最后一行
Utilitiy.GetInvokePattern(openButtonDialog);
对此
Utilitiy.GetInvokePattern(openButtonDialog).Invoke();