public WebTestResult RunTest(Uri testUri, WebBrowser browser, TimeSpan timeout) { if (_server == null) { throw new InvalidOperationException("The server has not been started."); } if (testUri == null) { throw new ArgumentNullException("testUri"); } if (testUri.IsAbsoluteUri == false) { throw new ArgumentException("URI must be absolute.", "testUri"); } if (browser == null) { throw new ArgumentNullException("browser"); } if (String.IsNullOrEmpty(browser.ExecutablePath)) { throw new InvalidOperationException("The specified browser could not be located."); } if (timeout.TotalMilliseconds == 0) { timeout = TimeSpan.FromMinutes(1); } AutoResetEvent waitHandle = new AutoResetEvent(/* initialSignaledState */ false); WebTestResult result = null; Process browserProcess = null; EventHandler<WebTestLogEventArgs> logEventHandler = null; logEventHandler = delegate(object sender, WebTestLogEventArgs e) { _server.Log -= logEventHandler; result = new WebTestResult(e.Succeeded, e.Log); waitHandle.Set(); }; try { string arguments = browser.Arguments + testUri.AbsoluteUri; ProcessStartInfo psi = new ProcessStartInfo(browser.ExecutablePath, arguments); psi.UseShellExecute = true; psi.WindowStyle = ProcessWindowStyle.Minimized; _server.Log += logEventHandler; browserProcess = Process.Start(psi); } catch (Exception) { _server.Log -= logEventHandler; return new WebTestResult(/* succeeded */ false, String.Empty); } bool signaled = waitHandle.WaitOne(timeout); if (browserProcess != null) { try { browserProcess.CloseMainWindow(); browserProcess.Kill(); browserProcess.WaitForExit(1000); } catch { } finally { browserProcess.Close(); } browserProcess = null; } if (signaled == false) { return new WebTestResult(); } else { return result; } }
public WebTestResult RunTest(Uri testUri, WebBrowser browser) { return RunTest(testUri, browser, TimeSpan.FromMinutes(1)); }