对于项目,我必须在C#中启动一个应用程序,删除与该进程相关的AutomationElement树,然后关闭应用程序并输出树。我是通过使用Process.Start打开应用程序来完成的。然后我找到与生成的进程相关的AutomationElements,并使用TreeWalker和AutomationElement的FindFirst和FindAll方法组合遍历树。
这在我的计算机上运行良好,并在本地使用NUnit正确运行。它也运行在我的组计算机中的其他人。问题是它永远不会在运行Hudson的中央测试服务器上运行。经过几个小时的调试,我对Hudson进行了测试,启动了应用程序,然后打印了AutomationTree的第一级。在我的计算机上,这将打印我桌面上的所有窗口。在Hudson上,这只会打印桌面。
认为可能有多个桌面,我尝试在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的测试程序说RootElement根本没有孩子。
所以我启动应用程序,确认它存在,然后我找不到与该进程关联的任何窗口。除桌面外,我找不到任何与之相关的窗口。
这是哈德森的问题吗? Hudson是否以某种特定的方式工作,这段代码不适用于它?这是我的代码的问题吗? Hudson服务器正在Windows Server 2003计算机上运行。任何帮助,将不胜感激。我知道这是一个非常具体的问题,这是我无法在网上找到任何解决方案的原因。
答案 0 :(得分:1)
Hudson是作为服务运行的吗?如果是这样,它可能没有显示窗口的必要权利。