C# 从控制台应用程序启动Selenium进程时出现问题
让我澄清一下: -我的path环境变量中有Java.exe -因此,如果我想运行“selenium服务器”,我将执行以下操作:C# 从控制台应用程序启动Selenium进程时出现问题,c#,process,C#,Process,让我澄清一下: -我的path环境变量中有Java.exe -因此,如果我想运行“selenium服务器”,我将执行以下操作: 1. Start cmd.exe Microsoft Windows [Version 5.2.3790] (C) Copyright 1985-2003 Microsoft Corp. C:\Documents and Settings\cnguyen> 2. Then: C:\Documents and Settings\cnguyen>cd C:\Se
1. Start cmd.exe
Microsoft Windows [Version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.
C:\Documents and Settings\cnguyen>
2. Then:
C:\Documents and Settings\cnguyen>cd C:\Selenium RC 0.9.2\selenium-server-0.9.2
3. Next, I'm in the directory that I want so I run:
C:\Documents and Settings\cnguyen>cd C:\Selenium RC 0.9.2\selenium-server-0.9.2
C:\Selenium RC 0.9.2\selenium-server-0.9.2>java -jar selenium-server.jar
09:26:18.586 INFO - Java: Sun Microsystems Inc. 16.3-b01
09:26:18.586 INFO - OS: Windows 2003 5.2 x86
09:26:18.586 INFO - v0.9.2 [2006], with Core v0.8.3 [1879]
09:26:18.633 INFO - Version Jetty/5.1.x
09:26:18.633 INFO - Started HttpContext[/selenium-server/driver,/selenium-server
/driver]
09:26:18.633 INFO - Started HttpContext[/selenium-server,/selenium-server]
09:26:18.633 INFO - Started HttpContext[/,/]
09:26:18.648 INFO - Started SocketListener on 0.0.0.0:4444
09:26:18.648 INFO - Started org.mortbay.jetty.Server@16a55fa
这是我到目前为止得到的,它被编译了,但没有显示任何内容:(
编辑:如果您打算隐藏 selenium服务器输出窗口,您可以 我得做点什么 异步调用。我可以进入 如果这确实是您的意图,请提供详细信息
class LaunchJava
{
private static Process myProcessProcess;
private static StreamWriter myProcessStandardInput;
private static Thread thist = Thread.CurrentThread;
public static void DoJava()
{
// Initialize the process and its StartInfo properties.
// The sort command is a console application that
// reads and sorts text input.
myProcess= new Process();
myProcess.StartInfo.Arguments = "-jar selenium-server.jar";
myProcess.StartInfo.FileName = @"C:\Documents and Settings\cnguyen\java.exe";
// Set UseShellExecute to false for redirection.
myProcess.StartInfo.UseShellExecute = false;
// Redirect the standard output of the sort command.
// This stream is read asynchronously using an event handler.
myProcess.StartInfo.RedirectStandardOutput = true;
myProcessOutput = new StringBuilder("");
// Set our event handler to asynchronously read the sort output.
myProcess.OutputDataReceived += new DataReceivedEventHandler(myProcessOutputHandler);
// Redirect standard input as well. This stream
// is used synchronously.
myProcess.StartInfo.RedirectStandardInput = true;
Console.WriteLine("Start.");
// Start the process.
myProcess.Start();
// Use a stream writer to synchronously write the sort input.
myProcessStandardInput = myProcess.StandardInput;
// Start the asynchronous read of the sort output stream.
myProcess.BeginOutputReadLine();
// Wait for the process to end on its own.
// as an alternative issue some kind of quit command in myProcessOutputHandler
myProcess.WaitForExit();
// End the input stream to the sort command.
myProcessInput.Close();
myProcessProcess.Close();
}
private static void myProcessOutputHandler(object sendingProcess, DataReceivedEventArgs Output)
{
// do interactive work here if needed...
if (!String.IsNullOrEmpty(Output.Data))
{ myProcess.StandardInput.BaseStream.Write(bytee,0,bytee.GetLength);
}
}
我很想看看这个。你介意告诉我怎么做吗?非常感谢你的建议;)我会先将进程的代码改为这个,看看它是否启动java.exe
pro = new Process();
pro.StartInfo.FileName = @"C:\Selenium RC 0.9.2\selenium-server-0.9.2\java.exe";
pro.StartInfo.Arguments = " -jar selenium-server.jar";
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.UseShellExecute = false;
pro.Start();
您的程序很可能在调用
pro.StandardOutput.ReadToEnd()
时被阻塞。考虑使用非阻塞<代码> RealOutPuxRealDeLoad()/Case>方法(多AT和)。 < P>这对我起作用…
/// <summary>
/// Creates new process to run and executable file, and return the output
/// </summary>
/// <param name="program">The name of the executable to run</param>
/// <param name="arguments">Any parameters that are required by the executable</param>
/// <param name="silent">Determines whether or not we output execution details</param>
/// <param name="workingDirectory">The directory to run the application process from</param>
/// <param name="standardErr">The standard error from the executable. String.Empty if none returned.</param>
/// <param name="standardOut">The standard output from the executable. String.Empty if none returned, or silent = true</param>
/// <returns>The application's exit code.</returns>
public static int Execute(string program, string arguments, bool silent, string workingDirectory, out string standardOut, out string standardErr)
{
standardErr = String.Empty;
standardOut = String.Empty;
//sometimes it is not advisable to output the arguments e.g. passwords etc
if (!silent)
{
Console.WriteLine(program + " " + arguments);
}
Process proc = Process.GetCurrentProcess();
if (!string.IsNullOrEmpty(workingDirectory))
{
//execute from the specific working directory if specified
proc.StartInfo.WorkingDirectory = workingDirectory;
}
proc.EnableRaisingEvents = true;
proc.StartInfo.FileName = program;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.Start();
proc.WaitForExit();
//only display the console output if not operating silently
if (!silent)
{
if (proc.StandardOutput != null)
{
standardOut = proc.StandardOutput.ReadToEnd();
Console.WriteLine(standardOut);
}
}
if (proc.StandardError != null)
{
standardErr = proc.StandardError.ReadToEnd();
Console.WriteLine(standardErr);
}
proc.StandardOutput.Close();
proc.StandardError.Close();
return proc.ExitCode;
}
//
///创建要运行的新进程和可执行文件,并返回输出
///
///要运行的可执行文件的名称
///可执行文件所需的任何参数
///确定是否输出执行详细信息
///从中运行应用程序进程的目录
///可执行文件中的标准错误。字符串。如果未返回,则为空。
///可执行文件的标准输出。String.Empty(如果未返回),或silent=true
///应用程序的退出代码。
公共静态int-Execute(字符串程序、字符串参数、bool-silent、字符串工作目录、out-string-standardOut、out-string-standardErr)
{
standardErr=String.Empty;
standardOut=String.Empty;
//有时不建议输出参数,例如密码等
如果(!静默)
{
Console.WriteLine(程序+“”+参数);
}
Process proc=Process.GetCurrentProcess();
如果(!string.IsNullOrEmpty(工作目录))
{
//如果指定,则从特定的工作目录执行
proc.StartInfo.WorkingDirectory=WorkingDirectory;
}
proc.EnableRaisingEvents=true;
proc.StartInfo.FileName=程序;
proc.StartInfo.Arguments=参数;
proc.StartInfo.CreateNoWindow=true;
proc.StartInfo.UseShellExecute=false;
proc.StartInfo.RedirectStandardOutput=true;
proc.StartInfo.RedirectStandardError=true;
proc.Start();
进程WaitForExit();
//仅在不静默操作时显示控制台输出
如果(!静默)
{
if(proc.StandardOutput!=null)
{
standardOut=proc.StandardOutput.ReadToEnd();
控制台写入线(标准输出);
}
}
如果(proc.StandardError!=null)
{
standardErr=proc.StandardError.ReadToEnd();
控制台写入线(标准错误);
}
proc.StandardOutput.Close();
过程StandardError.Close();
返回过程ExitCode;
}
您的pro.StandardOutput.ReadToEnd()
调用将被阻止,直到可执行文件终止。因为您正在启动一个服务器,该服务器将启动并等待输出,所以您永远不会得到任何东西
如果您只想查看服务器的输出,请将UseShellExecute
设置为true,将RedirectStandardOutput
和RedirectStandardError
设置为false。(或者只删除这三行)这将导致打开一个新的控制台窗口并显示selenium服务器的输出
编辑:如果您想要隐藏selenium服务器输出窗口,则必须进行一些异步调用。如果这确实是您的意图,我可以深入了解细节。它也不起作用:(!我在谷歌上搜索了一个小时,但没有一个示例对我的案例起作用。我刚刚刷新,看到一个答案已经发布…arg:(
class LaunchJava
{
private static Process myProcessProcess;
private static StreamWriter myProcessStandardInput;
private static Thread thist = Thread.CurrentThread;
public static void DoJava()
{
// Initialize the process and its StartInfo properties.
// The sort command is a console application that
// reads and sorts text input.
myProcess= new Process();
myProcess.StartInfo.Arguments = "-jar selenium-server.jar";
myProcess.StartInfo.FileName = @"C:\Documents and Settings\cnguyen\java.exe";
// Set UseShellExecute to false for redirection.
myProcess.StartInfo.UseShellExecute = false;
// Redirect the standard output of the sort command.
// This stream is read asynchronously using an event handler.
myProcess.StartInfo.RedirectStandardOutput = true;
myProcessOutput = new StringBuilder("");
// Set our event handler to asynchronously read the sort output.
myProcess.OutputDataReceived += new DataReceivedEventHandler(myProcessOutputHandler);
// Redirect standard input as well. This stream
// is used synchronously.
myProcess.StartInfo.RedirectStandardInput = true;
Console.WriteLine("Start.");
// Start the process.
myProcess.Start();
// Use a stream writer to synchronously write the sort input.
myProcessStandardInput = myProcess.StandardInput;
// Start the asynchronous read of the sort output stream.
myProcess.BeginOutputReadLine();
// Wait for the process to end on its own.
// as an alternative issue some kind of quit command in myProcessOutputHandler
myProcess.WaitForExit();
// End the input stream to the sort command.
myProcessInput.Close();
myProcessProcess.Close();
}
private static void myProcessOutputHandler(object sendingProcess, DataReceivedEventArgs Output)
{
// do interactive work here if needed...
if (!String.IsNullOrEmpty(Output.Data))
{ myProcess.StandardInput.BaseStream.Write(bytee,0,bytee.GetLength);
}
}