对于一个项目,我必须在C语言中启动一个应用程序,去掉与流程相关的AutomationElement树,然后关闭该应用程序并输出该树。我通过使用process.start打开应用程序来完成此操作。然后,我将找到与生成过程相关的自动化元素,并使用TreeWalker和AutomationElement的findFirst和findall方法的组合来遍历树。
这在我的电脑上运行很好,在本地使用nunit可以正常运行。它也运行在我组计算机中的其他人上。问题是它从未在运行Hudson的中央测试服务器上运行。经过几个小时的调试,我在Hudson上做了一个测试,启动应用程序,然后打印第一级的AutomationContree。在我的电脑上,这会打印我桌面上所有的窗口。在哈德逊,这只打印桌面。
考虑到可能有多个桌面,我尝试在rootelement上使用TreeWalker的getNextSibling函数。它仍然只报告一个桌面。
这是我用来启动进程的代码。
public bool connect(string[] args)
{
if (this.process != null) {
Console.WriteLine("ERROR: Process already connected");
return false;
}
if (!File.Exists(sApplicationPath)) {
Console.WriteLine(sApplicationPath + " does not exist");
return false;
}
// Turn the command arguments into a single string
string arguments = "";
foreach (string arg in args) {
arguments += arg + " ";
}
try {
// Start the application
ProcessStartInfo processStartInfo =
new ProcessStartInfo(sApplicationPath);
processStartInfo.Arguments = arguments;
this.process = Process.Start(processStartInfo);
// Must be a positive integer (non-zero)
if ( !( iInitialDelay > 0 ) ) {
Console.WriteLine("Invalid initial delay. " +
"Defaulting to 5 seconds.");
this.iInitialDelay = 5000;
}
Thread.Sleep(this.iInitialDelay);
} catch (Exception ex) {
Console.WriteLine("WGApplication.connect: " + ex.Message);
return false;
}
// Check if the process still exists
try {
/** This part does not return an error, so I think that means the process exists and is started */
Process check = Process.GetProcessById(process.Id);
} catch (ArgumentException ex) {
Console.WriteLine("The process expired before connection was complete");
Console.WriteLine("Make sure the process is not open anywhere else");
Console.WriteLine("and that it is able to execute on the host machine.");
return false;
}
// Check if the base automation element exists to verify open
AutomationElement rootWindow =
AutomationElement.RootElement.FindChildProcessById(process.Id);
/** This part returns null, so it can't find the window associated with this process id */
if (this.process == null) {
return false;
} else if (rootWindow == null) {
// A root window with this process id has not been found
Console.WriteLine("Cannot find the root window of the created " +
"process. Unknown error.");
return false;
} else {
// Everything is good to go
return true;
}
}
SApplicationPath设置为可执行文件的绝对路径。iInitialDelay是确保应用程序有时间启动的延迟。我在Windows Vista SP2的“c:\windows\system32\notepad.exe”上运行这个程序,并用v3.5 c编译器编译它。
findchildProcessByID定义如下:
public static AutomationElement FindChildProcessById(
this AutomationElement element, int processId)
{
var result = element.FindChildByCondition(
new PropertyCondition(AutomationElement.ProcessIdProperty,
processId));
return result;
}
请记住,这在我的计算机上编译并工作。我在哈德逊的测试程序说,根元素根本没有孩子。
因此,我启动应用程序,确认它存在,然后找不到任何与该进程相关联的窗口。除了桌面,我找不到任何与之相关联的窗口。
这是哈德逊的问题吗?哈德逊是不是以某种特定的方式工作,而这段代码对它不起作用?我的代码有问题吗?Hudson服务器正在Windows Server 2003计算机上运行。任何帮助都将不胜感激。我知道这是一个非常具体的问题,这也是我在网上找不到任何解决方案的原因。