NUnit を使用してロードされるSelenium Test .DLLがあります。
テストの実行時に、必要なSelenium Java Serverをプロセスに静かに隠して実行します。
ただし、現在、テストが開始されたときにサーバーを起動し、テストが停止したときにサーバーを起動します。Kill()
これにより、テストごとにセレンサーバーが開始/停止します。
私が望むのは、Selenium Server プロセスが次のことを行うことです。
- DLL のロード/初期化時、または最初のテストの開始時に開始
- DLLの死または
Kill()
ガベージコレクションに対処する
C# は DLL の初期化のキャッチとコードの呼び出しをサポートしていないことを読みました。(私が間違っている?)
私の考えは、シングルトン クラスで Selenium サーバーをホストし、最初のテストの実行時に初期化することでした。その後、ガベージ コレクションに任せて、デコンストラクターを介して Dispose メソッドを呼び出します。現在、セレンサーバーをホストする次のコードがあります。
namespace Tests.Server
{
using System;
using System.Diagnostics;
using System.IO;
using System.Security;
using System.Windows.Forms;
using Microsoft.Win32;
/// <summary>
/// A singleton class to host and control the selenium server.
/// </summary>
public class SeleniumServer : IDisposable
{
#region Fields
/// <summary>
/// The singleton instance
/// </summary>
private static volatile SeleniumServer instance;
/// <summary>
/// An object to perform double-check locking upon instance creation to
/// avoid dead-locks.
/// See <see href="SeleniumServer.Instance"/> for more information.
/// </summary>
private static object syncRoot = new Object();
/// <summary>
/// The current selenium server.
/// </summary>
private Process seleniumServer = null;
/// <summary>
/// A flag for the disposal of the class.
/// </summary>
private bool isDisposed = false;
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the SeleniumServer class. Starts the
/// Selenium Java server in a background hidden thread.
/// </summary>
private SeleniumServer()
{
// Get the java install folder.
string javaFileLocation = this.GetJavaFileLocation();
// Get the selenium server java executable
string jarFileLocation = '"' + Directory.GetCurrentDirectory() + @"\SeleniumServer\selenium-server.jar""";
// Start the selenium server
seleniumServer = new Process();
seleniumServer.StartInfo.FileName = javaFileLocation;
seleniumServer.StartInfo.Arguments = " -jar " + jarFileLocation + " -browserSessionReuse -trustAllSSLCertification";
seleniumServer.StartInfo.WorkingDirectory = jarFileLocation.Substring(0, jarFileLocation.LastIndexOf("\\"));
seleniumServer.StartInfo.UseShellExecute = true;
seleniumServer.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
seleniumServer.Start();
}
#endregion
#region Deconstructor
~SeleniumServer()
{
Dispose(false);
}
#endregion
#region Properties
/// <summary>
/// A thread safe
/// </summary>
public static SeleniumServer Instance
{
get
{
// This approach ensures that only one instance is created and
// only when the instance is needed. Also, the variable is
// declared to be volatile to ensure that assignment to the
// instance variable completes before the instance variable can
// be accessed. Lastly, this approach uses a syncRoot instance
// to lock on, rather than locking on the type itself, to avoid
// deadlocks.
// This double-check locking approach solves the thread
// concurrency problems while avoiding an exclusive lock in
// every call to the Instance property method. It also allows
// you to delay instantiation until the object is first
// accessed. In practice, an application rarely requires this
// type of implementation. In most cases, the static
// initialization approach is sufficient.
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
instance = new SeleniumServer();
}
}
}
return instance;
}
}
#endregion
#region Methods
/// <summary>
/// Stops the process.
/// </summary>
protected void Dispose(bool disposing)
{
if (disposing)
{
// Code to dispose the managed resources of the class
// Not needed right now.
}
// Code to dispose the un-managed resources of the class
// TODO: Handle the exceptions
Instance.seleniumServer.Kill();
// All done!
isDisposed = true;
}
/// <summary>
/// Dispose of the class and stop the Selenium server process.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Stops the Selenium Server.
/// </summary>
public void Stop()
{
this.Dispose();
}
/// <summary>
/// Attempts to get the Java installation folder from the registry. If
/// it cannot be found the default installation folder is checked before
/// failing.
/// </summary>
/// <returns>The Java executable file path.</returns>
/// <exception cref="System.Exception">
/// Thrown when the user does not have permission to access the
/// registry.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the registry key object is disposed of.
/// </exception>
/// <exception cref="System.IOException">
/// Thrown if the registry key object is marked for deletion.
/// </exception>
private string GetJavaFileLocation()
{
string javaFileLocation = string.Empty;
RegistryKey regKey = Registry.LocalMachine;
RegistryKey subKey = null;
try
{
// Check for Java in the native bitness
string javaRegistryLocation = @"SOFTWARE\JavaSoft\Java Runtime Environment\";
subKey = regKey.OpenSubKey(javaRegistryLocation);
// Check if we are running in WOW64 and only 32 bit Java is installed.
if (subKey == null)
{
javaRegistryLocation = @"SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment\";
subKey = regKey.OpenSubKey(javaRegistryLocation);
}
// If Java was not found in either of these location the user
// needs to install it. Note that Java 32 bit is a prerequisite
// for the installer so should always be installed.
if (subKey == null)
{
MessageBox.Show(
"You must have Java installed to run the commands server. This allows browser commands to be routed to the browser window.\n\nPlease visit: http://www.java.com/ to download.",
"Please Install Java",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
throw new Exception("No installation of Java was detected to run the selenium server.");
}
// Get all the sub keys (could be multiple versions of Java
// installed) and get the most recent (last one)
string[] subKeyNames = subKey.GetSubKeyNames();
subKey = regKey.OpenSubKey(javaRegistryLocation + subKeyNames[subKeyNames.Length - 1]);
// Specify the java executable location
javaFileLocation = subKey.GetValue("JavaHome").ToString() + @"\bin\java.exe";
}
catch (SecurityException e)
{
// Attempt to find the java executable at the default location.
javaFileLocation = @"C:\Program Files\Java\jre6\bin\java.exe";
if (!File.Exists(javaFileLocation))
{
MessageBox.Show(
"The program did not have permission to access the registry to obtain the installation folder of Java.\n\nThe default location (" + javaFileLocation + ") of Java was used and the Java executable was not found.\n\nPlease install Java in this folder or see the help under the RegistryKey.OpenSubKey method on MSDN.",
"Java Executable Not Found",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
throw new Exception("The program did not have permission to access the registry to obtain the installation folder of Java.", e);
}
}
catch (UnauthorizedAccessException e)
{
// Attempt to find the java executable at the default location.
javaFileLocation = @"C:\Program Files\Java\jre6\bin\java.exe";
if (!File.Exists(javaFileLocation))
{
MessageBox.Show(
"The user does not have the necessary registry rights to obtain the installation folder of Java.\n\nThe default location (" + javaFileLocation + ") of Java was used and the Java executable was not found.\n\nPlease install Java in this folder or see the help under the RegistryKey.OpenSubKey method on MSDN.",
"Java Executable Not Found",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
throw new Exception("The user does not have the necessary registry rights to obtain the installation folder of Java.", e);
}
}
catch (ObjectDisposedException e)
{
// This hopefully shouldn't happen so ask the user to report it.
MessageBox.Show(
"The Java registry object was closed, resulting in the Java server not being started. Please report this error.",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
throw new ObjectDisposedException("The Java registry object was closed. Please report this error.", e);
}
catch (IOException e)
{
// This hopefully shouldn't happen so ask the user to report it.
MessageBox.Show(
"The Java registry object was marked for deletion, resulting in the Java server not being started. Please report this error.",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
throw new ObjectDisposedException("The Java registry object was marked for deletion. Please report this error.", e);
}
return javaFileLocation;
}
#endregion
}
}
私の質問は次のとおりです。
- これは、DLL の有効期間中プロセスをホストする賢明な方法ですか?
- コードからこのクラスを設定するにはどうすればよいですか? 静的/グローバルにする必要がありますか? あなたが知っているシングルトンを使用する良い例はありますか?
- ガベージ コレクションは、DLL のアンロード時にこのシングルトンを破棄しますか?
- NUnit は個別のタイムポイントでのみ DLL 内のテストを実行するため、DLL が継続的にアクティブになるわけではありませんが、プロセスを DLL の子プロセスとしてホストできますか?